Struts2的监听器:
在xwork-2.0.7.jar包下,在com.opensymphony.xwork2.interceptor包下有个PreResultListener接口,自定义的监听器需实现此接口。
1,首先写一个自定义的Struts2监听器
MyListener.java
package com.test.listener;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.PreResultListener;
public class MyListener implements PreResultListener {
public void beforeResult(ActionInvocation invocation, String resultCode) {
System.out.println("result ="+resultCode);
}
}
2,再在自定义的方法拦截器中调用此监听器,如下所示
package com.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.test.listener.MyListener;
public class MyMethodInterceptor extends MethodFilterInterceptor {
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("before myMethodFilterInterceptor....");
invocation.addPreResultListener(new MyListener());
String invoked = invocation.invoke();
System.out.println("after myMethodFilterInterceptor....");
return invoked;
}
}
执行的顺序是:
1,先执行拦截器中的方法,如doIntercept。
2,在拦截器中调用invocation.invoke()来执行目标action中的validate验证方法。
3,如果validate验证成功,就执行execute方法或其它自定义的方法。
执行完后,则执行监听器类的beforeResult方法,并且其resultCode参数是success。
如果validate验证失败,则执行监听器类的beforeResult方法,并且其resultCode参数是input。
4,监听器中的方法执行完后,则返回到拦截器中的方法继续完成剩下的部分。
以上就是监听器的简单编写与流程介绍。
-------------------------------------------------------------------------------
验权小例:假设用户需先登录login.jsp页面输入登录用户信息,输入完成后,自动跳转到register.jsp页面填写注册信息,如果用户直接访问register.jsp页面输入信息,则在提交时,就跳转到login.jsp页面要求用户输入登录信息。即不允许用户直接访问register.jsp页面输入注册信息。
1,首先编写拦截器AuthInterceptor.java
package com.interceptor;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class AuthInterceptor implements Interceptor {
public void destroy() {
}
public void init() {
}
public String intercept(ActionInvocation invocation) throws Exception {
//取出session
Map session = invocation.getInvocationContext().getSession();
//判断user值是否为空,如果为空,就跳转到 login.jsp页面
if(session.get("user") == null){
return Action.LOGIN;
}else{
invocation.invoke();
}
return null;
}
}
2,填写struts2.xml配置文件
<interceptor name="authInterceptor" class="com.interceptor.AuthInterceptor"></interceptor> <action name="login" class="com.test.action.LoginAction"> <result name="input">/login.jsp</result> <!--input标签表示,如果action中validate方法的FieldError中有值,就会跳转到input标签指定的JSP页面--> <result name="success">/register.jsp</result> <result name="failer">/login.jsp</result> </action> <global-results> <!-- 定义全局常量,对应的AuthInterceptor.java类的Action.LOGIN常量, type="redirect"表示重定向--> <result name="login" type="redirect">/login.jsp</result> </global-results> <action name="login" class="com.test.action.LoginAction"> <result name="input">/login.jsp</result> <!--login输入成功,就跳转到/register.jsp页面 --> <result name="success">/register.jsp</result> <result name="failer">/login.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction" > <interceptor-ref name="authInterceptor"></interceptor-ref> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> </action>
3,在LoginAction.java类中应设置SESSION的值
public String execute() {
Map map = ActionContext.getContext().getSession();
//随便放置一个valid字符串
map.put("user", "valid");
if("hello".equals(this.getUsername().trim()) && "world".equals(this.getPassword().trim())){
return "success";
}else{
this.addFieldError("username", "username or password error");
return "failer";
}
}
用户在login.jsp页面输入登录信息后,跳转到LoginAction中,在此action中,将valid字符串放入session的user变量中,使user不为空,根据struts.xml的配置,login.jsp页面输入完成后,会自动跳转到register.jsp页面,用户在register.jsp页面输入完成点击提交,会跳转到RegisterAction,在此action中会判断session中的user是否为空,如果不为空,则继续流程,否则重新跳转到login.jsp页面。