我是 Struts 框架的新手。因此寻找一些在线教程并尝试开发一个非常基本的应用程序。在使用拦截器之前,我能够访问操作类中的用户名和密码值,但在使用拦截器之后,我在操作类执行方法中将用户名和密码获取为 null。如何获取操作类中的用户名和密码的值?
登录.jsp
<s:form action="login.action">
<s:actionerror cssStyle="color:red"/>
<s:textfield name="username" label="Username"/>
<s:password name="password" label="Password"/>
<s:submit value="Go"/>
</s:form>
拦截器类
public class MyInterceptors extends AbstractInterceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
public String intercept(ActionInvocation invocation)throws Exception{
/* let us do some pre-processing */
String output = "Pre-Processing";
System.out.println(output);
/* let us call action or next interceptor */
String result = invocation.invoke();
/* let us do some post-processing */
output = "Post-Processing";
System.out.println(output);
return result;
}
}
Action 类
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String password;
public String execute() {
System.out.println("Action Result.."+getUsername());
return "success";
}
//getters and setters
}
struts.xml
.....
<interceptors>
<interceptor name="myinterceptor"
class="com.techm.interceptors.MyInterceptors" />
</interceptors>
<action name="login" class="com.techm.actions.LoginAction">
<interceptor-ref name="myinterceptor"></interceptor-ref>
<result name="success">Success.jsp</result>
<result name="error">Login.jsp</result>
</action>
.....
在控制台中执行的结果是:
Pre-Processing
Action Result..null
Post-Processing
最佳答案
在操作配置中,您已覆盖拦截器配置。默认情况下,Struts 配置为使用默认的拦截器堆栈,即使您在操作配置中没有使用任何拦截器。通过重写拦截器,您犯了一个错误。您应该在特定操作配置中添加一个 defaultStack
。
<action name="login" class="com.techm.actions.LoginAction">
<interceptor-ref name="myinterceptor">
<interceptor-ref name="defaultStack"/>
<result name="success">Success.jsp</result>
<result name="error">Login.jsp</result>
</action>
关于java - 使用 myinterceptors 后 Struts 操作类属性变为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37269927/