自定义拦截器
接上个话题所有的Struts 2的拦截器都直接或间接实现接口
MyInterceptor.java
package com.zenoh.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
@SuppressWarnings("serial")
public class MyInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
String result = null;
// invocation.invoke()之前的代码,将会在Action之前被依次执行
String actionName = invocation.getAction().getClass().getName();
// 获取此次调用的Action的方法名
String method = invocation.getProxy().getMethod();
System.out.println("开始执行" + actionName + "的" + method + "方法");
result = invocation.invoke(); // 调用下一个资源
// invocation.invoke()之后的代码,将会在Action之后被逆序执行
System.out.println("执行" + actionName + "的" + method + "方法完毕");
return result;
}
}
src/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>
<include file="struts-default.xml"></include>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="InterceptorDemo" namespace="/" extends="struts-default">
<interceptors>
<!-- 自定义拦截器 -->
<interceptor name="myInterceptor" class="com.zenoh.interceptor.MyInterceptor"></interceptor>
<!-- 自定义的拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="myInterceptor"/>
</interceptor-stack>
</interceptors>
<default-action-ref name="index" />
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<action name ="Timer" class ="com.zenoh.action.TimerInterceptorAction" >
<!-- 这里配置拦截器 -->
<interceptor-ref name="myInterceptor"/>
<interceptor-ref name ="timer"/>
<result>/Timer.jsp</result>
</action >
</package>
</struts>
Struts2拦截器的原理: 客户端请求某一个Action时,都会经过配置好的拦截器。
分享到:
相关推荐
Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)
STRUTS2:拦截器Interceptor
利用反射和动态代理机制实现自定义拦截器Interceptor http://blog.csdn.net/alanchen520/article/details/34087833
Struts2.3.6实现自定义拦截器Interceptor http://blog.csdn.net/alanchen520/article/details/34086699
struts2 Interceptor拦截器 http://whitewolf.0fees.net
NULL 博文链接:https://wang4717.iteye.com/blog/796941
NULL 博文链接:https://chenyajie.iteye.com/blog/1980865
简单的拦截器例子,适合初学者学习,也是一个备忘录
struts2 拦截器_教程.zip
其中,init和destroy方法会在程序开始和结束时各执行一遍,不管使用了该拦截器与否,只要在struts.xml中声明了该Struts2拦截器就会被执行。 intercept方法就是拦截的主体了,每次拦截器生效时都会执行其中的逻辑。
谈到拦截器,还有一个词大家应该知道——拦截器链(Interceptor Chain,在Struts 2中称为拦截器栈Interceptor Stack)。拦截器链就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,拦截器链
struts2,Interceptor struts2拦截器实例,两套实例,一套是针对单个的action配置的,另一套实例是针对全局的action配置的拦截器interceptor
主要介绍了SpringBoot拦截器原理解析及使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
综合使用servlet filter与struts2 interceptor对权限进行验证。
使用Struts2实现用户权限拦截、重点是掌握拦截器的使用和配置方法,通过案例的学习,就能掌握struts中拦截器的使用方法,让我们能快速上手
jfinal中拦截器(Interceptor)的参数注入jfinal中拦截器(Interceptor)的参数注入
拦截器是一种AOP(面向切面编程)思想的编程方式.它提供一种机制是开发者能够把相对独立的代码抽离出来,...下面这篇文章主要给大家介绍了关于Struts2拦截器Interceptor的原理与配置的相关资料,需要的朋友可以参考下。
Struts2在com.opensymphony.xwork2.interceptor.annotations包中定义了3个拦截器注解类型。
Struts2_自定义拦截器 struts2_3500_my_interceptor
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.