在Struts2中,我的Action中不管有execute方法,还有edit() list() add()等方法,我如果只想让拦截器拦截edit方法和add方法,请问可以吗?如果可以请告诉我一下怎么

本文主要是介绍在Struts2中,我的Action中不管有execute方法,还有edit() list() add()等方法,我如果只想让拦截器拦截edit方法和add方法,请问可以吗?如果可以请告诉我一下怎么,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在Struts2中,我的Action中不管有execute方法,还有edit()  list() add()等方法,我如果只想让拦截器拦截edit方法和add方法,请问可以吗?如果可以请告诉我一下怎么写。
我说的是拦截器拦截某些action方法。不是拦截所有的action方法,怎么实现。
 
1.继承类MethodFilterInterceptor(此类是类AbstractInterceptor的子类)import java.util.Map;import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;/**拦截指定方法 */
public class MyFilterInterceptor  extends MethodFilterInterceptor{private static final long serialVersionUID = 1L;private String name;public void setName(String name){this.name = name;}@Overrideprotected String doIntercept(ActionInvocation invocation) throws Exception {//取得请求相关的ActionContext实例 ActionContext ctx = invocation.getInvocationContext(); Map session = ctx.getSession(); //取出名为user的Session属性 String user = (String)session.get("user"); //如果没有登陆,或者登陆所用的用户名不是scott,都返回重新登陆 if (user != null && user.equals("scott") ) { return invocation.invoke(); } //没有登陆,将服务器提示设置成一个HttpServletRequest属性 ctx.put("tip" , "您还没有登陆,请输入scott,tiger登陆系统"); //直接返回login的逻辑视图 return Action.LOGIN; }}2.struts.xml配置<package name="site" extends="struts-default" namespace="/site"><interceptors> <!-- 定义了一个名为authority的拦截器 --> <interceptor name="authority" class="cn.zgcyx.filter.MyFilterInterceptor"/> <!--上面自定义的拦截器类--><interceptor-stack name="myDefault"><interceptor-ref name="authority"> <!-- 引用拦截器  --><param name="includeMethods">getALL,getPart,listUser</param> <!-- 设置需要拦截的方法,多个以逗号隔开 --></interceptor-ref><interceptor-ref name="defaultStack"></interceptor-ref></interceptor-stack></interceptors><default-interceptor-ref name="myDefault"></default-interceptor-ref><!-- 全局 --><global-results> <!-- 当返回login视图名时,转入/login.jsp页面 --> <result name="login">/login.jsp</result> </global-results>
<action name="site" class="siteServiceAction"><!--省略跳转--></action></package>
使用MethodFilterInterceptor 自定义方法过滤拦截器,然后:
在struts.xml配置文件中,找到interceptor-ref 标签,添加:
<param name="拦截器名称.includeMethods">edit,add</param>自定义拦截器:package net.hncu.interceptor;import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class MyInterceptor3  extends MethodFilterInterceptor{//拦截器的名称private String interceptorName;public void setInterceptorName(String interceptorName) {this.interceptorName = interceptorName;}//实现拦截的方法public String intercept(ActionInvocation invocation) throws Exception {System.out.println(interceptorName + ":-----2拦截前操作-----");String result = invocation.invoke();System.out.println(interceptorName + ":------2拦截后操作------");return result;}
}给一个struts.xml文件你参考:<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><!-- struts为配置文件根元素-->
<struts><!-- Action必须放在指定的包名空间中--><package name="struts2" extends="struts-default">
<interceptors><interceptor name="myInter3" class="net.hncu.interceptor.MyInterceptor3"><param name="interceptorName">过滤拦截器</param></interceptor>   <interceptor-stack name="myInterStack"><interceptor-ref name="myInter3"><param name="interceptorName">自定义过滤拦截器</param></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref></interceptor-stack>
</interceptors><action name="login" class="net.hncu.struts2.action.LoginAction"><result name="success">/login_success.jsp</result><result name="error">/login_failure.jsp</result><result name="input">login.jsp</result><interceptor-ref name="myInterStack"><param name="myInter3.includeMethods">edit,add</param>  <!-- 用includeMethods指定被拦截的名称 -->
</interceptor-ref>
</action></package><!-- 指定资源文件baseName为messageResource --><constant name="struts.custom.i18n.resources" value="messageResource"></constant>
</struts>


这篇关于在Struts2中,我的Action中不管有execute方法,还有edit() list() add()等方法,我如果只想让拦截器拦截edit方法和add方法,请问可以吗?如果可以请告诉我一下怎么的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/849854

相关文章

Python中__init__方法使用的深度解析

《Python中__init__方法使用的深度解析》在Python的面向对象编程(OOP)体系中,__init__方法如同建造房屋时的奠基仪式——它定义了对象诞生时的初始状态,下面我们就来深入了解下_... 目录一、__init__的基因图谱二、初始化过程的魔法时刻继承链中的初始化顺序self参数的奥秘默认

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

Spring 基于XML配置 bean管理 Bean-IOC的方法

《Spring基于XML配置bean管理Bean-IOC的方法》:本文主要介绍Spring基于XML配置bean管理Bean-IOC的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录一. spring学习的核心内容二. 基于 XML 配置 bean1. 通过类型来获取 bean2. 通过

基于Python实现读取嵌套压缩包下文件的方法

《基于Python实现读取嵌套压缩包下文件的方法》工作中遇到的问题,需要用Python实现嵌套压缩包下文件读取,本文给大家介绍了详细的解决方法,并有相关的代码示例供大家参考,需要的朋友可以参考下... 目录思路完整代码代码优化思路打开外层zip压缩包并遍历文件:使用with zipfile.ZipFil

Python处理函数调用超时的四种方法

《Python处理函数调用超时的四种方法》在实际开发过程中,我们可能会遇到一些场景,需要对函数的执行时间进行限制,例如,当一个函数执行时间过长时,可能会导致程序卡顿、资源占用过高,因此,在某些情况下,... 目录前言func-timeout1. 安装 func-timeout2. 基本用法自定义进程subp

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下

电脑死机无反应怎么强制重启? 一文读懂方法及注意事项

《电脑死机无反应怎么强制重启?一文读懂方法及注意事项》在日常使用电脑的过程中,我们难免会遇到电脑无法正常启动的情况,本文将详细介绍几种常见的电脑强制开机方法,并探讨在强制开机后应注意的事项,以及如何... 在日常生活和工作中,我们经常会遇到电脑突然无反应的情况,这时候强制重启就成了解决问题的“救命稻草”。那