我正在为 struts2 拦截器创建一个示例。我创建了一个简单的登录页面并使用自定义拦截器类来加密输入。但是拦截器正在从ValueStack
as读取输入值null
。
我不明白我在做什么错。我想struts.xml
拦截器类是足够的数据。如果您需要更多我的代码,请告诉。
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>
拦截器 .java 文件
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
是我定义的一个单独的类,它自己可以正常工作。