`
johnson2132
  • 浏览: 26685 次
  • 性别: Icon_minigender_1
  • 来自: 潍坊
最近访客 更多访客>>
社区版块
存档分类
最新评论

struts2 拦截器的使用(继承方法拦截器)

阅读更多
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class ExampleInterceptor extends MethodFilterInterceptor {
	//重写方法拦截器拦截方法
	@Override
	protected String doIntercept(ActionInvocation arg0) throws Exception {
		System.out.println("start invoking3...");
		String result = arg0.invoke();
		System.out.println("end invoking3...");
		return result;
	}
LoginAction中增加了method方法

public String method()throws Exception {
		FORWARD = "success";
		return FORWARD;
	}
 

 

<struts>
	<!-- Action所在包定义 -->
	<package name="C04.3" extends="struts-default">
	<!-- 拦截器配置定义 -->
		<interceptors>
			<interceptor name="example"
				class="com.example.struts.interceptor.ExampleInterceptor">
			</interceptor>
		</interceptors>
		<!--  
			缺省拦截器栈配置定义
			<default-interceptor-ref name="example"></default-interceptor-ref>
		-->
		<!-- Action名字,类以及导航页面定义 -->
		<!-- 通过Action类处理才导航的的Action定义 -->
		<action name="Login"
			class="com.example.struts.action.LoginAction" method="method">
			<result name="input">/jsp/login.jsp</result>
			<result name="success">/jsp/success.jsp</result>
			<!-- Action方法拦截器配置定义 -->
			<interceptor-ref name="example">
				<!-- 被拦截方法配置定义 -->
				<param name="includeMethods">method</param>
				<!-- 不被拦截方法配置定义 -->
				<param name="excludeMethods">method,execute</param>
			</interceptor-ref>
		</action>
	</package>
</struts>

 LoginAction.java中又定义了一个名为“method”方法,在struts.xml配置文件中,因为LoginAction中有execute方法,又有method方法,因此在<Action>中,请读者注意struts.xml中黑体部分,该部分代码表示现在LoginAction只执行method方法,而execute方法不被执行。笔者在<Action>中增加了一个“method”属性,该属性中“=”后面的内容是Action中具体方法名,如果不写“method”属性,Action是缺省执行execute方法。如果写了“method”属性,Action就执行“=”后写的具体方法。而不会执行execute方法。“example”拦截器还是如之前在<Action>前定义。在<Action>中配置“example”拦截器,笔者增加了“includeMethods”和“excludeMethods”两个param属性定义。“includeMethods”表示的是被拦截器拦截的方法。方法名写在<param></param>之间,如果有多个方法开发人员需要拦截器拦截,则方法名之间以“,”相隔。“excludeMethods”表示的是不被拦截器拦截的方法。如果有多个方法,也是以“,”相隔

struts.xml配置文件中要么没有<default-interceptor-ref >定义,如果定义了也只能定义一次。该标签在struts.xml配置文件中只能写在<Action>前,而且只能写一次。不能重复定义它

0
2
分享到:
评论

相关推荐

    struts2 拦截器的使用(继承抽象拦截器)

    NULL 博文链接:https://johnson2132.iteye.com/blog/720398

    struts2拦截器demo1

    (2)拦截器组件可以继承AbstractIntercepter类(实现了Intercepter接口)。 (3)拦截器组件可以继承MethodFilterIntercepter类(继承自AbstractIntercepter),增加了方法过滤功能(上面两种方式是拦截所有方法,这个...

    struts2拦截器

    需要继承Interceptor接口以及实现接口里边的方法 */ public class MyInterceptor implements Interceptor{ public void destroy() { } public void init() { } public String intercept...

    Struts2 in action中文版

    4.3 研究内建的Struts 2拦截器 67 4.3.1 工具拦截器 67 4.3.2 数据转移拦截器 67 4.3.3 工作流拦截器 69 4.3.4 其他拦截器 72 4.3.5 内建的拦截器栈 73 4.4 声明拦截器 74 4.4.1 声明独立的拦截器和拦截器栈 74 ...

    struts2讲义_吴峻申

    4.1 拦截器在Struts2中的缺省应用 47 4.2 拦截器原理实现 50 4.3 在Struts2中配置自定义的拦截器 53 4.3.1 扩展拦截器接口的自定义拦截器配置 54 4.3.2 继承抽象拦截器的自定义拦截器配置 56 4.3.3 继承方法拦截器的...

    Struts2入门教程(全新完整版)

    4.使用默认的execAndWait拦截器 33 5. TokenInterceptor防止表单重复提交。 34 6.使用拦截器实现权限验证 35 7.拦截器中的注解 37 8.使用PreResultListener实现回调 39 六、使用标签 40 1.基础表单标签 40 2.单选...

    Struts2的工作原理和流程

    7 ActionInvocation实例使用命名模式来调用,在调用Action的过程前后,涉及到相关拦截器(Intercepter)的调用。 8 一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果。返回结果...

    jfreechar 整合struts2.1.8版本生成线图,饼图,柱形图

    如果Action中引用了其它的拦截器 默认的拦截器将无效 --&gt; &lt;default-interceptor-ref name="mystack"&gt;&lt;/default-interceptor-ref&gt; &lt;!-- 全局results配置 --&gt; &lt;result name="input"&gt;/error.jsp ...

    struts2开发文档

    及到相关拦截器(Intercepter)的调用。 8 一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找 到对应的返回结果。返回结果通常是(但不总是,也可 能是另外的一个Action 链)一个需要被表示的JSP...

    struts2入门实例2 经典入门必备

    1.Struts2_01_login 对应登录。。 ... login_struts2.jsp-------------------------------采用struts标签的登陆页面 ... login_struts_validate_noblank.jsp-----------... 拦截器 17.Struts2_09up&down 上传和下载

    struts2入门实例1

    struts2 最新的入门实例 我自己总结的 呵呵,欢迎提出宝贵的意见 1.Struts2_01_login 对应登录。。 ... login_struts2.jsp-------------------------------采用... 拦截器 17.Struts2_09up&down 上传和下载

    struts项目学习笔记

    基于AOP(面向切面编程)思想的拦截器机制,更易扩展(不修改源代码的条件下,增强代码功能) 更强大、更易用输入校验功能 整合Ajax支持:json插件 Struts2的今生前世: 1.早期开发模型Servlet+JSP+JavaBean显得...

    struts2入门实例4 经典入门必备

    1.Struts2_01_login 对应登录。。 ... login_struts2.jsp-------------------------------采用struts标签的登陆页面 ... login_struts_validate_noblank.jsp-----------... 拦截器 17.Struts2_09up&down 上传和下载

    struts2入门实例3 经典入门必备

    1.Struts2_01_login 对应登录。。 ... login_struts2.jsp-------------------------------采用struts标签的登陆页面 ... login_struts_validate_noblank.jsp-----------... 拦截器 17.Struts2_09up&down 上传和下载

    Struts2的配置文件基础

    Struts2框架中核心组件就是Action、拦截器等,Struts2框架使用包来管理Action和拦截器等。每个包就是多个Action、多个拦截器、多个拦截器引用的集合。在struts.xml文件中package元素用于定义包配置,每个package元素...

    低清版 大型门户网站是这样炼成的.pdf

    2.6.1 struts 2内建拦截器介绍 95 2.6.2 定义和使用拦截器栈 97 2.6.3 使用自定义拦截器 98 2.7 小结 100 第3章 struts 2标签库详解与ajax技术应用 103 3.1 struts 2主题与模板 103 3.2 struts 2控制标签详解 ...

    java从入门到精通70个PPT

    34 Struts 2深入 拦截器 35 Struts 2综合应用 36 类型转换和OGNL 37-40 项目案例:在线投票系统 41 jsp servlet struts总结 42 Hibernate 入门 43 Hibernate 关联映射 44 HQL实用技术 45 HQL高级 46 Criteria 查询 ...

    superscangood

    和使用容器声明式事务管理。持久化层使用hibernate来实现,使用泛型DAO把...在实现细粒度权限管理时使用了Struts2中提供的拦截器实现对Action方法的拦截,当发现用户具有某个权限时,才允许Action方法继续执行。粗粒度

Global site tag (gtag.js) - Google Analytics