Struts2中的MethodFilterInterceptor
来源:互联网 发布:淘宝关键词搜索量查询 编辑:程序博客网 时间:2024/05/29 09:13
这是一个Struts2.1.8.1应用,代码如下
首先是web.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <welcome-file-list>
- <welcome-file>methodFilter.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
然后用于提交方法过滤测试的methodFilter.jsp页面
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <s:form action="methodFilter" theme="simple">
- 您可以直接点击Submit提交请求,然后在控制台查看输出情况<br/>
- <s:submit value="我要测试方法过滤拦截器"/>
- </s:form>
然后是用于显示方法过滤结果的methodFilterResult.jsp页面
- <%@ page pageEncoding="UTF-8"%>
- <h2>请移步控制台查看输出情况</h2>
然后是struts.xml文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
- "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- <package name="methodFilter" extends="struts-default">
- <interceptors>
- <interceptor name="myInterceptor11" class="com.jadyer.interceptor.MyInterceptor11">
- <param name="hello">world</param>
- </interceptor>
- <!-- <param name="hello">world</param>表示为拦截器增加一个名为hello值为world的属性 -->
- <!-- 但是这里也只是为hello赋了初值为world。如若在下面的<action/>中配置使用该拦截器的时候 -->
- <!-- 增加了<param name="hello">welcome</param>,则最后拦截器类中hello属性值即为welcome -->
- <interceptor name="myInterceptor22" class="com.jadyer.interceptor.MyInterceptor22"/>
- <interceptor name="myInterceptor33" class="com.jadyer.interceptor.MyInterceptor33">
- <param name="includeMethods">test,abc</param>
- </interceptor>
- </interceptors>
- <action name="methodFilter" class="com.jadyer.action.MethodFilterAction" method="test">
- <result name="success">/methodFilterResult.jsp</result>
- <interceptor-ref name="defaultStack"/>
- <interceptor-ref name="myInterceptor11"/>
- <interceptor-ref name="myInterceptor22"/>
- <interceptor-ref name="myInterceptor33">
- <param name="includeMethods">abc</param>
- </interceptor-ref>
- </action>
- </package>
- </struts>
- <!-- *********************【方法过滤拦截器的使用】********************************************** -->
- <!-- 默认的情况下,拦截器会拦截Action中的所有的方法,这里不包括setter或getter方法 -->
- <!-- 这时就可以使用方法过滤拦截器来拦截指定的方法,这是一种更加细化的拦截器的配置方式 -->
- <!-- 它可以细化到拦截具体的某个方法。而不是拦截某个Action,因为拦截Action是一种粗粒度的实现方式 -->
- <!-- 使用includeMethods指明拦截器所要拦截的方法。使用excludeMethods指明拦截器不再拦截的方法 -->
- <!-- 这里excludeMethods和includeMethods是在MethodFilterInterceptor类中定义的成员变量 -->
- <!-- 而且只要includeMethods进来的方法就一定会被拦截,而不管是否已经把它excludeMethods在外了 -->
- <!-- 也就是说includeMethods的优先级要高于excludeMethods -->
- <!-- 也可以使用<param name="includeMethods"/>在上面定义拦截器的时候指定全局性过滤的方法 -->
- <!-- 区别就是对方法的过滤有全局性和局部性区分。而当发生冲突时,则依照【就近原则】以局部性的配置为准 -->
- <!-- 所谓的发生冲突,指的是类似于全局中有一个includeMethods配置,而局部中也有一个includeMethods配置 -->
- <!-- 另外,还有一种情况,假设全局性过滤定义为<param name="includeMethods">test</param> -->
- <!-- 而在局部性过滤中定义为<param name="excludeMethods">test</param> -->
- <!-- 这种情况下,<param name="includeMethods">test</param>将生效,即拦截Action中的test()方法 -->
- <!-- 这个时候全局中配置的是拦截,局部中配置的是不拦截,二者并没有发生冲突,所以仍是以includeMethods优先级高 -->
- <!-- 可以认为在局部的配置中,已经隐含的把<param name="includeMethods">test</param>继承过来了 -->
- <!-- 补充:这里衡量的所谓拦截到与否,主要可以通过查看控制台输出的语句,以判断是否执行到该拦截器内部 -->
- <!-- **************************************************************************************** -->
实现了Interceptor接口的自定义拦截器MyInterceptor11.java
- package com.jadyer.interceptor;
- import com.opensymphony.xwork2.ActionInvocation;
- import com.opensymphony.xwork2.interceptor.Interceptor;
- /**
- * 实现Interceptor接口的自定义拦截器
- * @see 【这种方式不太常用】
- */
- @SuppressWarnings("serial")
- public class MyInterceptor11 implements Interceptor {
- //这里的属性名要与struts.xml配置的<param name=""/>中的name值相同
- //然后Struts2会自动将struts.xml中配置的world值赋值到这里的hello属性中
- //当然,前提是要提供setter和getter方法,只要符合JavaBean的要求即可
- private String hello;
- public String getHello() {
- return hello;
- }
- public void setHello(String hello) {
- this.hello = hello;
- }
- /**
- * 初始化时执行的方法
- */
- public void init() {
- System.out.println("------MyInterceptor11 init method invoked------");
- System.out.println(hello);
- }
- /**
- * 销毁时执行的方法
- */
- public void destroy() {
- System.out.println("------MyInterceptor11 destroy method invoked------");
- }
- //每执行一次action请求,这里的intercept()方法都会被执行一次
- public String intercept(ActionInvocation invocation) throws Exception {
- System.out.println("------MyInterceptor11 invoked begin------");
- //调用invoke()方法
- //如果还有下一个拦截器的话,就执行下一个拦截器
- //如果没有下一个拦截器的话,便执行Action中的方法
- String result = invocation.invoke();
- System.out.println("------MyInterceptor11 invoked finish------");
- return result;
- }
- }
- /********************【浅析Struts2的拦截器】***********************************************************/
- //Struts2中的拦截器,实际上就是用来拦截Action的。它就相当于入口和出口一样,把Action的相关方法包裹在中间了
- //过滤器可以组成过滤器链,也就是有多个过滤器来过滤相同的东西。拦截器同样也有拦截器链,在Struts2中称为拦截器栈
- //拦截器栈相当于一串拦截器,用来共同的拦截某一个Action。拦截的顺序是按照配置的顺序执行的
- //假设先配置的是myInterceptor11,后配置的是myInterceptor22
- //所以在执行时,先执行myInterceptor11,后执行myInterceptor22
- //但是在结束时,先执行myInterceptor22,后执行myInterceptor11
- //就好像进入一个有两道门的房间一样,进去的顺序和出来的顺序正好相反
- //也就是说它首先会进入第一个拦截器,出来后再进入第二个拦截器,依此类推,最后进入Action的execute()方法
- //当execute()执行后,再按照相反的顺序,先回到第二个拦截器,再回到第一个拦截器,最后才回到结果视图
- //因此invoke()就是用来判断,若还有下一个拦截器,就调用下一个拦截器。否则,直接跳到Action的execute()方法
- /********************【Struts2的默认拦截器】***********************************************************/
- //实际上可以把Struts2看成是一个空的容器。就是因为里面配置了大量的拦截器,导致了我们的请求需要一层一层的通过这些拦截器
- //然后它会处理我们的请求,并根据我们的配置,把它所感兴趣的东西解析出来。如果出现问题,它会放到错误消息里面去
- /****************************************************************************************************/
继承了AbstractInterceptor类的自定义拦截器MyInterceptor22.java
- package com.jadyer.interceptor;
- import com.opensymphony.xwork2.ActionInvocation;
- import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
- /**
- * 继承AbstractInterceptor类的自定义拦截器
- * @see 【这种方式比较常用】
- * @see 继承AbstractInterceptor之后,就不需要再去实现init()和destroy()方法了
- * @see 所以实际应用中,更多的时候还是继承AbstractInterceptor,而不是实现Interceptor
- */
- @SuppressWarnings("serial")
- public class MyInterceptor22 extends AbstractInterceptor {
- @Override
- public String intercept(ActionInvocation invocation) throws Exception {
- System.out.println("------MyInterceptor22 invoked begin------");
- String result = invocation.invoke();
- System.out.println("------MyInterceptor22 invoked finish------");
- return result;
- }
- }
继承了MethodFilterInterceptor拦截器类的自定义拦截器MyInterceptor33.java
- package com.jadyer.interceptor;
- import com.opensymphony.xwork2.ActionInvocation;
- import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
- /**
- * 继承MethodFilterInterceptor的拦截器,即方法过滤的拦截器
- * @see 查看MethodFilterInterceptor的源码可知,它也是一个拦截器类,它继承了AbstractInterceptor
- * @see 观察它的类名,可以发现,它就是一个拦截某一个具体的方法的方法过滤拦截器
- * @see 而MethodFilterInterceptor类已经自动实现好了Intercept()方法
- * @see 实际上MethodFilterInterceptor类中的Intercept()方法真正执行的是它本身的一个doIntercept()抽象方法
- * @see 因此,我们想要拦截某一个方法的时候,只要继承MethodFilterInterceptor类,然后实现doIntercept()便OK
- */
- @SuppressWarnings("serial")
- public class MyInterceptor33 extends MethodFilterInterceptor {
- @Override
- protected String doIntercept(ActionInvocation invocation) throws Exception {
- System.out.println("------MyInterceptor33 invoked begin------");
- String result = invocation.invoke();
- System.out.println("------MyInterceptor33 invoked finish------");
- return result;
- }
- }
最后是用到的Action类
- package com.jadyer.action;
- import com.opensymphony.xwork2.ActionSupport;
- @SuppressWarnings("serial")
- public class MethodFilterAction extends ActionSupport{
- public String test() throws Exception {
- System.out.println("------test() is invoked------");
- return SUCCESS;
- }
- public String abc() throws Exception {
- System.out.println("------abc() is invoked------");
- return SUCCESS;
- }
- @Override
- public String execute() throws Exception {
- System.out.println("------execute() is invoked------");
- return SUCCESS;
- }
- }
- Struts2中的MethodFilterInterceptor
- Struts2中的MethodFilterInterceptor
- Struts2中的MethodFilterInterceptor
- Struts2-MethodFilterInterceptor
- MethodFilterInterceptor
- struts2中MethodFilterInterceptor类的用法
- Struts2——MethodFilterInterceptor--方法拦截器
- SSH-Struts2简单的自定义拦截器MethodFilterInterceptor
- Struts2使用MethodFilterInterceptor来实现方法拦截器
- Struts2使用MethodFilterInterceptor来实现方法拦截器
- struts2的拦截器(5):继承MethodFilterInterceptor抽象类实现拦截方法的拦截器
- AbstractInterceptor和MethodFilterInterceptor的区别
- STRUTS2中的日期处理
- struts2中的constant
- Struts2中的链接标签
- struts2 中的使用注意
- Struts2 中的Actin配置
- Struts2应用中的国际化
- Hibernate面试题
- Oracle 11g 各软件包用途
- unexpected end of file while looking for precompiled header directive解决方法
- Make money form Windows Phone: Paid or Free app, which strategy to choose?
- android 读取SD卡文件
- Struts2中的MethodFilterInterceptor
- u-boot bootm命令浅析
- 有效控制项目进度的几点技巧
- QML中webview元素的javaScriptWindowObjects的使用示例
- jQuery之$.grep()函数(数据过滤)
- 项目延期原因及应对之道
- Volume Shadow Copy Service(VSS)如何工作
- Oracle数据库的系统性能优化策略浅析
- struts2拦截器简介