Struts2学习笔记(十一)——自定义拦截器

来源:互联网 发布:旅游大数据第一股 编辑:程序博客网 时间:2024/06/06 02:27

Struts2拦截器是基于AOP思想实现的,而AOP的实现是基于动态代理。Struts2拦截器会在访问某个Action之前或者之后进行拦截,并且Struts2拦截器是可插拔的;Struts2拦截器栈就是将拦截器按照顺序连接在一起的链,当满足拦截的要求时,则会按照实现声明的顺序依次执行拦截器。

1、Struts2自定义拦截器介绍

Struts2所有拦截器都必须实现Interceptor接口,Interceptor接口主要有3个方法:

  • init():初始化方法,只在拦截器加载时执行一次
  • intercept(ActionInvocation invocation):拦截器执行方法,每一次请求就执行一次
  • destory():销毁方法,只在拦截器释放时执行一次

AbstractInterceptor抽象类实现了Interceptor 接口。并为init()和destroy()提供了一个空白的实现,所以在实际开发中,自定义拦截器只需要继承AbstractInterceptor类, 并且实现intercept(ActionInvocation invocation)方法就可以了。

2、Struts2自定义拦截器创建步骤

  1).创建一个类实现Interceptor接口或继承AbstractInterceptor类。
  2).重写intercept方法,这个方法是真正拦截操作,如果想要继续向下访问其它拦截器,必须在intercept方法中通过参数ActionInvocation调用invoke方法。
  3).配置拦截器,需要在struts.xml文件中配置,分为两种情况:

  • 为package包下所有<action>配置共用拦截器,需要通过设置<default-interceptor-ref>来设置默认拦截器,需要在<action>之前配置
    • 不配置拦截器栈,只会执行<default-interceptor-ref>设置的拦截器
      复制代码
       1 <package name="default" namespace="/" extends="struts-default"> 2      <interceptors> 3           <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor> 4      </interceptors> 5      <default-interceptor-ref name="login"></default-interceptor-ref> 6      <action name="loginAction" class="com.sunny.action.LoginAction"> 7           <result>/success.jsp</result> 8           <result name="error">/error.jsp</result> 9      </action>10 </package>
      复制代码
    • 配置拦截器栈
      复制代码
       1  <package name="default" namespace="/" extends="struts-default"> 2      <interceptors> 3          <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor> 4          <interceptor-stack name="my"> 5              <interceptor-ref name="login"></interceptor-ref> 6              <interceptor-ref name="defaultStack"></interceptor-ref> 7          </interceptor-stack> 8       </interceptors> 9       <default-interceptor-ref name="my"></default-interceptor-ref>10       <action name="loginAction" class="com.sunny.action.LoginAction">11           <result>/success.jsp</result>12           <result name="error">/error.jsp</result>13       </action>14  </package>
      复制代码
  • 为package包下某个<action>配置拦截器,需要在<action>中通过设置<interceptor-ref>来设置拦截器,如果想要执行defaultStack,则需要在<action>中配置
    • 不配置拦截器栈
      复制代码
       1 <package name="default" namespace="/" extends="struts-default"> 2     <interceptors> 3         <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor> 4     </interceptors> 5     <action name="loginAction" class="com.sunny.action.LoginAction"> 6         <result>/success.jsp</result> 7         <result name="error">/error.jsp</result> 8         <interceptor-ref name="login"></interceptor-ref> 9         <interceptor-ref name="defaultStack"></interceptor-ref>10     </action>11 </package>
      复制代码
    • 配置拦截器栈
      复制代码
       1 <package name="default" namespace="/" extends="struts-default"> 2     <interceptors> 3         <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor> 4         <interceptor-stack name="my"> 5             <interceptor-ref name="login"></interceptor-ref> 6             <interceptor-ref name="defaultStack"></interceptor-ref> 7         </interceptor-stack> 8     </interceptors> 9     <action name="loginAction" class="com.sunny.action.LoginAction">10         <result>/success.jsp</result>11         <result name="error">/error.jsp</result>12         <interceptor-ref name="my"></interceptor-ref>13     </action>14 </package>
      复制代码

2、Struts2自定义拦截器实现示例:判断是否登录

该示例主要是用来验证用户是否登录,如果没登录,就跳转到error.jsp页面,提示需要登录系统。

拦截器类:

复制代码
 1 public class LoginIntercept extends AbstractInterceptor { 2  3     @Override 4     public String intercept(ActionInvocation invocation) throws Exception { 5         Map session = ServletActionContext.getContext().getSession(); 6         if (session.get("user")==null) { 7             return "error"; 8         } else { 9             return invocation.invoke();10         }11     }12 }
复制代码

Action类:

复制代码
 1 public class LoginAction extends ActionSupport { 2     private String name; 3     public String getName() { 4         return name; 5     } 6     public void setName(String name) { 7         this.name = name; 8     } 9     @Override10     public String execute() throws Exception {11         return "success";12     }13 }
复制代码

struts.xml配置文件:

复制代码
 1 <struts> 2     <constant name="struts.devMode" value="true" /> 3      4     <package name="default" namespace="/" extends="struts-default"> 5         <interceptors> 6             <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor> 7             <interceptor-stack name="my"> 8                 <interceptor-ref name="login"></interceptor-ref> 9                 <interceptor-ref name="defaultStack"></interceptor-ref>10             </interceptor-stack>11         </interceptors>12         <action name="loginAction" class="com.sunny.action.LoginAction">13             <result>/success.jsp</result>14             <result name="error">/error.jsp</result>15             <interceptor-ref name="my"></interceptor-ref>16         </action>17     </package>18 </struts>
复制代码

input.jsp页面:

1 <body>2  <form action="${pageContext.servletContext.contextPath}/loginAction.action">3     姓名:<input type="text" name="name"><br>4     <input type="submit" value="提交">5  </form>6 </body>

error.jsp页面:

1 <body>2 请登录系统3 </body>

登录界面:

由于没有登录系统,所以点击提交之后,会显示:

阅读全文
0 0
原创粉丝点击