Struts2自定义拦截器

简介:

Struts2提供的默认的拦截器已经包含了开发应用项目所用到的大部分拦截功能,开发人员也可以开发自己是拦截器实现类,并在配置文件中使用自定义的拦截器,来完成拦截功能!

Struts2框架为开发自定义拦截器实现类提供了Interceptor接口,我们可以实现该接口来自定义拦截器实现类

Interceptor接口定义如下:

 

 
  1. public interface Interceptor extends Serializable {  
  2.  
  3.     /**  
  4.      * Called to let an interceptor clean up any resources it has allocated.  
  5.      */ 
  6.     void destroy();  
  7.  
  8.     /**  
  9.      * Called after an interceptor is created, but before any requests are processed using  
  10.      * {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving  
  11.      * the Interceptor a chance to initialize any needed resources.  
  12.      */ 
  13.     void init();  
  14.  
  15.     /**  
  16.      * Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the  
  17.      * request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code.  
  18.      *  
  19.      * @param invocation the action invocation  
  20.      * @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself.  
  21.      * @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}.  
  22.      */ 
  23.     String intercept(ActionInvocation invocation) throws Exception;  
  24.  

此接口中定义了三个方法

String intercept(ActionInvocation invocation) 这个方法是实现用户拦截的处理方法,返回一个String的结果,来对应配置文件中定义的逻辑视图名称。如果该方法不调用invoke(),而是直接返回一个字符串如“success”,则会跳转到success对应的逻辑视图,而不会调用被拦截的Action。invoke()方法 在下面的程序的注释中会说到。

 

Struts2除了提供Interceptor接口用于开发自定义拦截器实现类外,还提供了

AbstractInterceptor类,我们只需要继承该类,可以使用简单的方式来实现自己的拦截器类。

AbstractInterceptor类如下:

 
  1. /*  
  2.  * Copyright 2002-2006,2009 The Apache Software Foundation.  
  3.  *   
  4.  * Licensed under the Apache License, Version 2.0 (the "License");  
  5.  * you may not use this file except in compliance with the License.  
  6.  * You may obtain a copy of the License at  
  7.  *   
  8.  *      http://www.apache.org/licenses/LICENSE-2.0  
  9.  *   
  10.  * Unless required by applicable law or agreed to in writing, software  
  11.  * distributed under the License is distributed on an "AS IS" BASIS,  
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13.  * See the License for the specific language governing permissions and  
  14.  * limitations under the License.  
  15.  */ 
  16. package com.opensymphony.xwork2.interceptor;  
  17.  
  18. import com.opensymphony.xwork2.ActionInvocation;  
  19.  
  20. /**  
  21.  * Provides default implementations of optional lifecycle methods  
  22.  */ 
  23. public abstract class AbstractInterceptor implements Interceptor {  
  24.  
  25.     /**  
  26.      * Does nothing  
  27.      */ 
  28.     public void init() {  
  29.     }  
  30.       
  31.     /**  
  32.      * Does nothing  
  33.      */ 
  34.     public void destroy() {  
  35.     }  
  36.  
  37.  
  38.     /**  
  39.      * Override to handle interception  
  40.      */ 
  41.     public abstract String intercept(ActionInvocation invocation) throws Exception;  
  42. }  

我们自定义类的时候,只需要重写intercept(ActionInvocation invocation)方法即可。

自定义拦截器实现类:

①SimpleInterceptor类:

 
  1. package com.yaxing.interceptor;  
  2.  
  3. import java.util.Date;  
  4.  
  5. import com.opensymphony.xwork2.ActionInvocation;  
  6. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
  7.  
  8. public class SimpleInterceptor extends AbstractInterceptor {  
  9.  
  10.     @Override 
  11.     public String intercept(ActionInvocation invocation) throws Exception {  
  12.         // TODO Auto-generated method stub  
  13.         Reg reg = (Reg)invocation.getAction();//获得被拦截的Action的引用,获得Action的控制权限  
  14.         System.out.println("拦截器信息:启动拦截器,拦截Action时间:"+new Date());  
  15.         String result=invocation.invoke();//执行Action或者下一个拦截器  
  16.                 System.out.println("拦截器信息:Action执行完毕时间:!"+new Date());  
  17.         return result;  
  18.             }  
  19.  
  20. }  

 

②struts.xml配置文件:

 
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4. <package name="define" extends="struts-default" namespace="">  
  5.     <interceptors>  
  6.         <interceptor name="Myinterceptor" class="com.yaxing.interceptor.Myinterceptor"></interceptor>  
  7.         <interceptor name="SimpleInterceptor" class="com.yaxing.interceptor.SimpleInterceptor"></interceptor>  
  8.     </interceptors>  
  9.     <action  name="Reg" class="com.yaxing.interceptor.Reg" method="execute">  
  10.            <result name="success">/Success.jsp</result>  
  11.            <result name="input">/Reg.jsp</result>  
  12.            <interceptor-ref name="defaultStack"></interceptor-ref>  
  13.            <interceptor-ref name="SimpleInterceptor"></interceptor-ref>  
  14.     </action>  
  15.       
  16. </package>  
  17.  
  18. </struts>      

定义拦截器:<interceptor.../> 元素在包中定义该拦截器

使用拦截器 在<action></action>中使用<interceptor-ref.../>使用见上面代码。

包中定义两个拦截器 :一个是系统默认的拦截器defaultStack,另一个是自定义拦截器SimpleInterceptor。

执行结果如下:

 

从控制台的信息可以看出:拦截器SimpleInterceptor在Action执行execute()方法之前进行了拦截,并输出了一行提示,之后调用invoke()方法,执行Action 的execute(0方法,执行之后拦截器再次输出控制台信息到控制台。如果将下面代码修改如下:

如果修改如下:

 
  1. package com.yaxing.interceptor;  
  2.  
  3. import java.util.Date;  
  4.  
  5. import com.opensymphony.xwork2.ActionInvocation;  
  6. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
  7.  
  8. public class SimpleInterceptor extends AbstractInterceptor {  
  9.  
  10.     @Override 
  11.     public String intercept(ActionInvocation invocation) throws Exception {  
  12.         // TODO Auto-generated method stub  
  13.         Reg reg = (Reg)invocation.getAction();//获得被拦截的Action的引用,获得Action的控制权限  
  14.         System.out.println("拦截器信息:启动拦截器,拦截Action时间:"+new Date());  
  15.         //String result=invocation.invoke();//执行Action或者下一个拦截器  
  16.         //如果不调用invoke方法则Action不会被执行  
  17.         System.out.println("拦截器信息:Action执行完毕时间:!"+new Date());  
  18.         //return result;  
  19.         return "input";//直接返回  
  20.     }  
  21.  
  22. }  

执行结果如下:

 

可以看到,Action并没有执行!这是因为没有调用invoke()方法!拦截器直接返回一个“input” 字符串,会直接显示配置文件中input对应的视图资源Reg.jsp

本文中的Reg.jsp Success.jsp文件在这篇文章中提及。详解Struts2拦截器实例一

 

如果拦截器中没有添加默认的<interceptor-ref name="defaultStack"></interceptor-ref>这个拦截器

则允许结果会是直接跳转到input视图。因为拦截器defaultStack包含了许多重要的功能,例如参数的读取,session的管理等,因此在Action显示引用自定义拦截器时,必须同时显示的引用defaultStack默认拦截器才行。






 本文转自 w156445045 51CTO博客,原文链接:http://blog.51cto.com/enetq/542559,如需转载请自行联系原作者


相关文章
|
7月前
Struts2的拦截器
Struts2的拦截器
25 0
|
小程序 Java
Struts2【拦截器】(三)
Struts2【拦截器】
157 0
Struts2【拦截器】(三)
|
Java
Struts2【拦截器】(一)
Struts2【拦截器】
137 0
Struts2【拦截器】(一)
|
Java 关系型数据库 MySQL
Struts2【拦截器】(二)
Struts2【拦截器】
128 0
Struts2【拦截器】(二)
|
Java NoSQL Redis
Struts 拦截器
介绍 实现aop的方式用于实现action之前,之后执行一般用于事物操作.一般用于对某些未授权的页面访问的时候,进行拦截操作,拦截非法访问. 开箱即用拦截器 <!-- 拦截器 --> <interceptor-ref name="params"/> ...
1081 0
|
Java 容器 应用服务中间件
12 Struts2 拦截器
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hxdeng/article/details/81941425 拦截器 拦截器在概念上和Servlet过滤器或JDK代理类一样。
1234 0
|
SQL Java 数据库
Struts2【拦截器】就是这么简单
什么是拦截器 拦截器Interceptor.....拦截器是Struts的概念,它与过滤器是类似的...可以近似于看作是过滤器 为什么我们要使用拦截器 前面在介绍Struts的时候已经讲解过了,Struts为我们实现了很多的功能,比如数据自动封装阿..文件上传功能阿....Struts为我们提供的这些功能都是通过拦截器完成的...... 数据自动封装通过这个拦截器。
1100 0
|
移动开发 应用服务中间件 Apache