本文共 3135 字,大约阅读时间需要 10 分钟。
2、LoginAction用户登录
public class LoginAction extends ActionSupport{ private String username; private String password; //...setter/getter.... public String execute()throws Exception{ return SUCCESS;}}3、定义自己的拦截器MyInterceptor
public class MyInterceptor implements Interceptor{ private String test; public void destroy() { System.out.println("MyInterceptor is destory!"); } public void init() { System.out.println("MyInterceptor is init !"); } public String intercept(ActionInvocation arg0) throws Exception { System.out.println("intercept before"); String temp = arg0.invoke(); //该方法为如果下面有烂机器就调用下一个,如果没有就执行Action。 System.out.println("MyInterceptor is intercept:"+temp); System.out.println("intercept after"); return temp; } public String getTest() { return test; } public void setTest(String test) { this.test = test; }}4、配置文件struts.xml
test index.jsp
方法拦截器(可以对指定方法进行拦截):MethodFilterInterceptor
如果既没有指定includeMethods参数,也没有指定execludeMethods参数,那么所有的方法都有会被拦截。
依旧延续以上的案例public class MyMethodInterceptor extends MethodFilterInterceptor{ @Override protected String doIntercept(ActionInvocation arg0) throws Exception { //可以添加监听器 arg0.addPreResultListener(new TheLisener()); System.out.println("method interceptor before"); String s = arg0.invoke(); System.out.println("method interceptor after"); return s; }}2、struts.xml配置文件
四、自定义默认的拦截器栈test index.jsp execute
如上配置好之后,面对登录不需要进行拦截的请求,只能通过在代码中对该情况进行处理。test index.jsp execute
转载地址:http://yexei.baihongyu.com/