i'm creating example struts2 interceptors. created simple login page , used custom interceptor class encrypt input. interceptor reading values of input valuestack
null
.
i don't understand doing wrong. suppose struts.xml
, interceptor class enough data this. if need more of code, please tell.
struts.xml
<?xml version="1.0" encoding="utf-8"?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.0//en" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devmode" value="true" /> <package name="mypackage" extends="struts-default"> <interceptors> <interceptor name="encrypt" class="com.keyur.struts2.interceptors.encryptdecryptinterceptor"/> </interceptors> <action name="validatoraction" class="com.keyur.struts2.actionclasses.validatorclass" method="execute"> <interceptor-ref name="encrypt"></interceptor-ref> <result name="success">/success.jsp</result> <result name="input">/index.jsp</result> </action> </package> </struts>
interceptor .java file
package com.keyur.struts2.interceptors; import com.keyur.struts2.actionclasses.validatorclass; import com.keyur.struts2.beans.encryptordecryptor; import com.opensymphony.xwork2.actioninvocation; import com.opensymphony.xwork2.interceptor.interceptor; import com.opensymphony.xwork2.util.valuestack; public class encryptdecryptinterceptor implements interceptor { encryptordecryptor encdec = new encryptordecryptor(); @override public void destroy() { // todo auto-generated method stub } @override public void init() { // todo auto-generated method stub } @override public string intercept(actioninvocation arg0) throws exception { // todo auto-generated method stub string result = arg0.invoke(); valuestack stack = arg0.getstack(); string username = stack.findstring("username"); string password = stack.findstring("password"); system.out.println("username: "+((validatorclass)stack.peek()).getusername()); system.out.println("password: "+((validatorclass)stack.peek()).getpassword()); //system.out.println(username); //system.out.println(password); //stack.set("username", encdec.encrypttext(username)); //stack.set("password", encdec.encrypttext(password)); return result; } }
encryptordecryptor
separate class have defined , working correctly on it's own.
if want access parameters, params
interceptor should go first
<action name="validatoraction" class="com.keyur.struts2.actionclasses.validatorclass" method="execute"> <interceptor-ref name="params"></interceptor-ref> <interceptor-ref name="encrypt"></interceptor-ref> <interceptor-ref name="defaultstack"></interceptor-ref> <result name="success">/success.jsp</result> <result name="input">/index.jsp</result> </action>
your problem parameters don't go valuestack
, , should them action context.
map params = actioncontext.getcontext().getparameters();
but after params
interceptor should there.
Comments
Post a Comment