I'm creating an example for struts2 interceptors. I created a simple login page and used a custom interceptor class to encrypt the input. But the interceptor is reading the values of input from ValueStack
as null
.
I don't understand what am I doing wrong. I suppose struts.xml
and interceptor class are enough data for this. If you need some more of my 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
is the a separate class which I have defined and it is working correctly on it's own.
If you want to access parameters, the
params
interceptor should go firstYour problem is that those parameters don't go to the
valueStack
, and you should probably get them from the action context.But after
params
interceptor they should be there.