Struts2拦截方法拦截器的使用(十三)
2017-12-28 13:30
513 查看
(一)拦截方法拦截器
1.说明
1.配置自定义拦截器拦截某个Action,该拦截器会拦截该Action的所有方法。
2.某些情况下我们并不希望拦截一个Action类的所有方法,那么就需要使用
拦截方法拦截器。
3.Struts2提供MethodFilterInterceptor类,该类是AbstractInterceptor的
子类,可以实现对Action方法的拦截。 重写doIntercept方法。
2.注意点
MethodFilterInterceptor中参数的配置
1.如果不配置这两个参数,Action中的所有方法都会被拦截。
2.如果一个方法同时在excludeMethods和includeMethods中出现,则会被拦截。
3.配置拦截方法拦截器
a.拦截方法拦截器提供两个参数分别是:
1.excludeMethods:不需要拦截的方法,,不需要拦截多个方法逗号隔开。
2.includeMethods:需要拦截的方法,拦截多个方法逗号隔开。
(二)实例演示
1.创建Action类
2.创建拦截器
3.在struts.xml中配置Action和拦截器
4.编写一个JPS页面
5.结果演示
1.说明
1.配置自定义拦截器拦截某个Action,该拦截器会拦截该Action的所有方法。
2.某些情况下我们并不希望拦截一个Action类的所有方法,那么就需要使用
拦截方法拦截器。
3.Struts2提供MethodFilterInterceptor类,该类是AbstractInterceptor的
子类,可以实现对Action方法的拦截。 重写doIntercept方法。
2.注意点
MethodFilterInterceptor中参数的配置
1.如果不配置这两个参数,Action中的所有方法都会被拦截。
2.如果一个方法同时在excludeMethods和includeMethods中出现,则会被拦截。
3.配置拦截方法拦截器
a.拦截方法拦截器提供两个参数分别是:
1.excludeMethods:不需要拦截的方法,,不需要拦截多个方法逗号隔开。
2.includeMethods:需要拦截的方法,拦截多个方法逗号隔开。
(二)实例演示
1.创建Action类
package com.wang; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionSupport; /** * * @author Kaina * 2017-12-28 * */ public class CRUDOperation extends ActionSupport { private static final long serialVersionUID = 1L; public String add(){ System.out.println("添加一些实现添加功能的业务逻辑代码!"); return Action.SUCCESS; } public String query(){ System.out.println("添加一些实现查询功能的业务逻辑代码!"); return Action.SUCCESS; } public String update(){ System.out.println("添加一些实现修改功能的业务逻辑代码!"); return Action.SUCCESS; } public String delete(){ System.out.println("添加一些实现删除功能的业务逻辑代码!"); return Action.SUCCESS; } }
2.创建拦截器
package com.intercepter; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; public class MyFilterIntercepter extends MethodFilterInterceptor{ private static final long serialVersionUID = 1L; @Override //1.在此方法类,编写拦截业务逻辑 protected String doIntercept(ActionInvocation invocation) throws Exception { System.out.println("方法拦截器被执行,无法请求此url"); //return invocation.invoke(); //2.拦截一些方法,并返回到其他页面 return Action.LOGIN; } }
3.在struts.xml中配置Action和拦截器
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC d605 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="wang" namespace="/" extends="struts-default"> <interceptors> <!--1.定义一个拦截器--> <interceptor name="myfilterintercptor" class="com.intercepter.MyFilterIntercepter"> <!--01.不需要被拦截的方法--> <param name="excludeMethods">add,query</param> <!--02.需要被拦截的方法--> <param name="includeMethods">update,delete</param> </interceptor> <!--2.定义一个拦截器栈--> <interceptor-stack name="mydefault"> <interceptor-ref name="myfilterintercptor"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors> <!--3.配置默认拦截器对整个包下的所有action都有用--> <default-interceptor-ref name="mydefault"/> <global-results> <result name="login">/error.jsp</result> </global-results> <!--4.配置一个Action中的四个方法--> <action name="add" class="com.wang.CRUDOperation" method="add"> <result name="success">/success.jsp</result> </action> <action name="query" class="com.wang.CRUDOperation" method="query"> <result name="success">/success.jsp</result> </action> <action name="update" class="com.wang.CRUDOperation" method="update"> <result name="success">/error.jsp</result> </action> <action name="delete" class="com.wang.CRUDOperation" method="delete"> <result name="success">/error.jsp</result> </action> </package> </struts>
4.编写一个JPS页面
<body> <!--1.分别请求一个Action类中的四个方法--> <!--2.通过配置拦截器,拦截一些请求和不拦截一些请求--> <a href="add">添加用户</a> <a href="query">查询用户</a> <a href="delete">删除用户</a> <a href="update">修改用户</a> </body>
5.结果演示
相关文章推荐
- Struts2拦截器使用方法
- struts2拦截器配置;拦截器栈;配置默认拦截器;拦截方法的拦截器MethodFilterInterceptor;完成登录验证
- Struts2拦截器使用方法
- Struts2拦截器的一些使用方法
- Struts2使用MethodFilterInterceptor来实现方法拦截器
- Struts2_拦截器(方法拦截)
- 4.5.8: Struts2的拦截器机制---拦截方法的拦截器
- struts2的拦截器(5):继承MethodFilterInterceptor抽象类实现拦截方法的拦截器
- struts2中自定义拦截器intercept的相关配置及使用方法
- 基于struts2拦截器的权限控制系统1——后台方法的拦截
- Struts2-拦截器的使用方法
- 使用方法拦截器MethodInterceptor和AOP统一处理log (Spring的拦截器 对方法进行拦截 ))
- struts2过滤器与拦截器组合使用,过滤拦截非法请求
- Struts2拦截器,简单的用户登录拦截及静态缓存的使用
- 【引用】Struts2拦截器使用方法
- Struts2拦截指定方法的拦截器
- struts2 拦截器拦截action中指定方法
- struts2关于action拦截器使用方法 现记录如下
- struts2拦截器(拦截指定方法)
- struts2 使用注解、反射、拦截器实现基于方法的权限控制