我是Struts框架的新手。所以寻求一些在线教程,并尝试开发一个非常基本的应用程序。在使用拦截器之前,我可以在动作类中访问用户名和密码值,但在涉及拦截器后,在动作类执行方法中获取用户名和密码为null。如何在动作类中获取用户名和密码的值?
的login.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;
}
}
动作类
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
答案 0 :(得分:1)
在操作配置中,您已覆盖拦截器配置。默认情况下,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>