拦截器

  • 获取作用域对象
    String name="";
    HttpServletRequest request = ServletActionContext.getRequest();
    request.setAttribute("info", name);
    HttpServletResponse response = ServletActionContext.getResponse();   
    HttpSession session = request.getSession();
    ServletContext application = ServletActionContext.getServletContext();
  • 拦截器
    • 编写拦截器
      • 声明一个类,让这个类继承(implements) Interceptor接口;
    • 声明拦截器
      <interceptors>
             <interceptor name="testInt_register" class="action.TestInt"></interceptor> <!-- 声明拦截器 -->
      </interceptors>
    • 映射拦截器
      <interceptor-ref name="testInt_register"></interceptor-ref> <!-- 映射拦截器 -->
    • 多个拦截器
      • 多个拦截器映射的顺序就是调用的顺序;
        <struts>
              <package name="struts02" extends="struts-default">
                   <interceptors>
                         <interceptor name="testInt_register" class="action.TestInt"></interceptor> <!-- 声明拦截器 -->
                         <interceptor name="testInt_reg" class="action.TestInt01"></interceptor>
                   </interceptors>
                   <action name="register" class="action.RegisterAction" method="register">
                   
                         <interceptor-ref name="testInt_reg"></interceptor-ref>
                         <interceptor-ref name="testInt_register"></interceptor-ref> <!-- 映射拦截器 -->
                         
                         <result>registerSuccess.jsp</result>
                   </action>
              </package>
        </struts>
         配置自定义拦截器后,默认拦截器全部失效,需要在action后加入:
        <interceptor-ref name="basicStack"></interceptor-ref>
    • 拦截器的堆栈
      <interceptors>
             <interceptor name="testInt_register" class="action.TestInt"></interceptor> <!-- 声明拦截器 -->
             <interceptor name="testInt_reg" class="action.TestInt01"></interceptor>
             <!-- 拦截器堆栈 -->
             <interceptor-stack name="mystack">
                    <interceptor-ref name="testInt_reg"></interceptor-ref> <!-- 映射拦截器 -->
                    <interceptor-ref name="testInt_register"></interceptor-ref>
                     <interceptor-ref name="basicStack"></interceptor-ref>
             </interceptor-stack>  
      </interceptors>
         
 
 
原文地址:https://www.cnblogs.com/wq-code/p/8360993.html