畏怯的读音:Struts2拦截器总结

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 03:14:23
一、编写拦截器1、  实现接口com.opensymphony.xwork2.Intercepter(或继承com.opensymphony.xwork2.AbstractInterceptor)2、  在interceptor方法中加入如下代码:        public String intercept(ActionInvocation arg0) throws Exception {
           System.out.println("Before");   //在Action之前调用
           String result = arg0.invoke();  //如果此拦截器之后还有拦截器,则调用下个拦截器的intercept方法
                                           //如果之后没有了拦截器,则调用Action的execute方法
           System.out.println("After");
            return result;   
        } 二、在Struts.xml中配置拦截器1、  在struts.xml中声明拦截器和拦截器Stack,拦截器Stack可以包括多个拦截器和其他Stack。                                       


               
               

   
2、  将拦截器配置到单个Action中,只拦截此Action中的execute方法。
           /success.jsp
           /register2.jsp
          
   
3、  将拦截器配置到所有Action中,拦截所有Action中的execute方法。对已经单独配置了拦截器的Action不起作用
 三、拦截Action中指定的方法
1、 继承com.opensymphony.xwork2.interceptor.MethodFilterInterceptor。
2、 因为是针对某个Action的方法,所以只能配置在Action内部

           /success.jsp
           /register2.jsp
                         test,execute
              myfun       
   
 四、struts2拦截器的interceptor方法中,参数ActionInvocation可用来获取页面用户输入的信息。
public String intercept(ActionInvocation arg0) throws Exception {       Map map = arg0.getInvocationContext().getSession();       if(map.get("user") == null) {           return Action.LOGIN;       } else {           return arg0.invoke();       }    }
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sendfeng/archive/2009/06/07/4248120.aspx