struts-拦截器应用

一.总体步骤:

  1. 自定义一个实现Interceptor接口(或者继承自AbstractInterceptor)的类。

  2. 在strutx.xml中注册定义的拦截器。

  3. 在需要使用的Action中引用上述定义的拦截器,为了方便也可将拦截器定义为默认的拦截器,这样在不加特殊声明的情况下所有的Action都被这个拦截器拦截。

二.继承自AbstractInterceptor的类

 1 package com.yztc.struts.inteceptor;
 2 
 3 
 4 import com.opensymphony.xwork2.ActionInvocation;
 5 import com.opensymphony.xwork2.interceptor.Interceptor;
 6 
 7 public class DemoInteceptor implements Interceptor {
 8     public DemoInteceptor() {
 9     }
10 
11     @Override
12     public void destroy() {
13         System.out.println("Interceptor:被消毁");
14     }
15 
16     @Override
17     public void init() {
18         System.out.println("Interceptor:初始化");
19     }
20     @Override
21     public String intercept(ActionInvocation actionInvocation) throws Exception {
22         System.out.println("Interceptor:核心方法执行前");
23         String invoke = actionInvocation.invoke();
24         System.out.println("Interceptor:核心方法执行后");
25         return invoke;
26     }
27 }

三.在struts.xml中注册定义的拦截器

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <!DOCTYPE struts PUBLIC
 4         "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
 5         "http://struts.apache.org/dtds/struts-2.5.dtd">
 6 <struts>
 7     <constant name="struts.devMode" value="true"/>
 8     <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
 9     <package name="default" extends="struts-default" namespace="/">
10         <interceptors>
11             <interceptor name="inteceptor1" class="com.yztc.struts.inteceptor.DemoInteceptor">
12                 <param name="includeMethods" >test</param>
13             </interceptor>
14             <!--<interceptor name="inteceptor2" class="com.yztc.struts.inteceptor.Demo2Inteceptor">-->
15                 <!--<param name="includeMethods" >test</param>-->
16             <!--</interceptor>-->
17             <!--<interceptor name="inteceptor3" class="com.yztc.struts.inteceptor.Demo3Inteceptor">-->
18                 <!--<param name="includeMethods" >test</param>-->
19             <!--</interceptor>-->
20         </interceptors>
21         <action name="*" class="com.yztc.struts.action.InterceptorAction" method="{1}">
22             <result name="success">/Interceptor.jsp</result>
23             <interceptor-ref name="inteceptor1"/>
24             <!--<interceptor-ref name="inteceptor2"/>-->
25             <!--<interceptor-ref name="inteceptor3"/>-->
26             <allowed-methods>test</allowed-methods>
27         </action>
28     </package>
29 </struts>

四.在需要使用的action中引用上述的拦截器

 1 package com.yztc.struts.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 /**
 6  * _ooOoo_
 7  * o8888888o
 8  * 88" . "88
 9  * (| -_- |)
10  * O\ = /O
11  * ___/`---'\____
12  * .   ' \\| |// `.
13  * / \\||| : |||// \
14  * / _||||| -:- |||||- \
15  * | | \\\ - /// | |
16  * | \_| ''\---/'' | |
17  * \ .-\__ `-` ___/-. /
18  * ___`. .' /--.--\ `. . __
19  * ."" '< `.___\_<|>_/___.' >'"".
20  * | | : `- \`.;`\ _ /`;.`/ - ` : | |
21  * \ \ `-. \_ __\ /__ _/ .-` / /
22  * ======`-.____`-.___\_____/___.-`____.-'======
23  * `=---='
24  * .............................................
25  *
26  * @author bindu
27  * @date 2017-10-25 10:39
28  */
29 
30 
31 public class InterceptorAction extends ActionSupport {
32     public String test(){
33         System.out.println("核心方法被执行");
34         return SUCCESS;
35     }
36 }

五.在web.xml 设置过滤器以及annotation初始化参数

 1 <!DOCTYPE web-app PUBLIC
 2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
 4 
 5 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 6          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 7          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
 8                       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 9          version="3.1">
10   <display-name>Archetype Created Web Application</display-name>
11   <filter>
12     <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
13     <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
14   </filter>
15   <filter-mapping>
16     <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
17     <url-pattern>/*</url-pattern>
18   </filter-mapping>
19 </web-app>

 

posted @ 2017-10-25 15:13  友情天  阅读(244)  评论(0编辑  收藏  举报