问题描述
我正在为 struts2 拦截器创建一个示例.我创建了一个简单的登录页面并使用自定义拦截器类来加密输入.但是拦截器正在将 ValueStack 的输入值读取为 null.
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.
我不明白我做错了什么.我想 struts.xml 和拦截器类是足够的数据.如果您需要更多我的代码,请告诉.
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://www.51sjk.com/Upload/Articles/1/0/332/332846_20221021102844549.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 是我定义的一个单独的类,它自己可以正常工作.
EncryptorDecryptor is the a separate class which I have defined and it is working correctly on it's own.
推荐答案
如果要访问参数,params 拦截器应该先去
If you want to access parameters, the 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>
您的问题是这些参数没有进入 valueStack,您可能应该从操作上下文中获取它们.
Your problem is that those parameters don't go to the valueStack, and you should probably get them from the action context.
Map params = ActionContext.getContext().getParameters();
但是在 params 拦截器之后它们应该在那里.
But after params interceptor they should be there.