`

自定义Struts2_Interceptor

阅读更多
    今天上午学习了一下关于如何自定义Interceptor的知识,做了一个超级简单的自定义拦截器。

    自定义的拦截器代码:
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class InterceptorByPersonal implements Interceptor
{

	private static final long serialVersionUID = 1L;

	public void destroy()
	{
		System.err.println("InterceptorByPersonal destroy . . .");
	}

	public void init()
	{
		System.err.println("InterceptorByPersonal initializing . . .");
	}

	public String intercept(ActionInvocation invocation) throws Exception
	{
		System.err.println("InterceptorByPersonal is invoked . . .");
		String result = invocation.invoke();
		return result;
	}

}


    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>
	<!-- 引入struts框架的配置文件 -->
	<include file="struts-default.xml"></include>
	<package name="struts2_test" extends="struts-default">
		<!-- 在配置文件中声明定义自己的拦截器InterceptorByPersonal -->
		<interceptors>
			<interceptor name="interceptorByPersonal"
				class="com.harry.demo.interceptors.InterceptorByPersonal"></interceptor>
		</interceptors>
		<action name="userCase" class="com.harry.demo.actions.UsercaseAction">
			<interceptor-ref name="interceptorByPersonal"/>
			<interceptor-ref name="basicStack"/>
			<interceptor-ref name="timer" />
			<result name="input">/index.jsp</result>
			<result name="success">/success.jsp</result>
		</action>
	</package>
</struts>



    OK,基本上就是这样了,现在重新部署一下项目,发现在控制台输出了我自定义拦截器中init()方法的代码,说明拦截器在此时已经初始化了,然后我去访问上面配置文件中已经准备好的action,这时,在控制台会打印出intercept()方法中我要打印的语句,说明该方法被调用了。

    接着,多访问几次该action,发现在控制台会一直输出
InterceptorByPersonal is invoked . . .

说明了拦截器在运行时,会在每次访问之前进行调用。


引申:我看了Struts2的API,该方法的介绍时这样的:
invoke

String invoke()
              throws Exception

    Invokes the next step in processing this ActionInvocation. If there are more Interceptors, this will call the next one. If Interceptors choose not to short-circuit ActionInvocation processing and return their own return code, they will call invoke() to allow the next Interceptor to execute. If there are no more Interceptors to be applied, the Action is executed. If the ActionProxy getExecuteResult() method returns true, the Result is also executed.

    Throws:
        Exception



大概意思明白,但是还是需要多理解!毕竟自己的英文水平有限。不过,我发现一点,如果我把自定义的Interceptor中的intercept方法返回值改为null,仍旧会进行传递。但是,如果在改方法中没有执行
ActionInvocation.invoke();
方法,就会出现空白页面!!!
则有趣儿的事情就发生了。当我访问action之后,页面变成空白了,什么反应都没有。这是为何呢?参照上面的API,发现这个方法的返回值似乎是在给下面的拦截器通知(或者没有其他拦截器了,就是通知调用action实例的方法了),我让其返回null,自然就啥都没了。我目前是这样认为的,就先这样吧!


修炼内功,CODING逐鹿!
分享到:
评论

相关推荐

    Struts2_自定义拦截器

    Struts2_自定义拦截器 struts2_3500_my_interceptor

    Java struts2 Spring 整合文档附加演示工程

    3.struts2_interceptor 拦截器,自定义拦截器 4.struts2_log4j 5.struts2_spring struts spring 整合 以上功能包含完整示例代码 Eclipse Java EE IDE for Web Developers. Build id: 20090920-1017 Tomcat6.0 下编译...

    Struts2.3.6实现自定义拦截器Interceptor

    Struts2.3.6实现自定义拦截器Interceptor http://blog.csdn.net/alanchen520/article/details/34086699

    使用struts2拦截器对登陆权限验证

    综合使用servlet filter与struts2 interceptor对权限进行验证。

    Struts2 in action中文版

    8.1.1 页面上:如何使用自定义结果组件构建Struts 2 Ajax应用程序 171 8.1.2 实现JSON结果类型 173 8.2 常用的结果类型 180 8.2.1 RequestDispatcher,也叫做dispatcher 180 8.2.2 ServletRedirectResult,也叫做...

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

    2.自定义拦截器 28 方式一,实现Interceptor接口。 28 方式二、继承AbstractInterceptor抽象类 29 方式三、继承MethodFilterInteceptor类 30 3.使用来MethodFilterInterceptor灵活拦截 32 4.使用默认的execAndWait...

    struts2的自定义拦截器代码

    struts2的自定义拦截器代码

    深入浅出Struts2(附源码)

    本书是广受赞誉的Struts 2优秀教程,它全面而深入地阐述了Struts 2的各个特性,并指导开发人员如何根据遇到的问题对症下药,选择使用最合适的特性。作者处处从实战出发,在丰富的示例中直观地探讨了许多实用的技术,...

    Struts:Strurs的Somp应用

    Struts2Interceptor: 关于Struts自定义拦截器的应用 Struts2ParameterPostMethod_common: 关于Struts普通传值的应用 Struts2ParameterPostMethod_packagebybean: 关于Struts封装参数传递的应用 Struts2SQLConnection...

    Servlet简单模拟Struts2

    用sevrlet模拟Struts2的简单功能。从拦截请求、解析自定义xml数据文件以及动态生成action的代理去执行目标方法,并实现了简单的日志拦截【interceptor】

    Struts拦截器及token拦截器防止重复提交例子源码

    Struts2规定用户自定义拦截器必须实现com.opensymphony.xwork2.interceptor.Interceptor接口。该接口声明了3个方法, void init(); void destroy(); String intercept(ActionInvocation invocation) throws ...

    JSP 开发之Struts2内建自定义拦截器

    JSP 开发之Struts2内建自定义拦截器 Struts2的自定义拦截器主要用于解析请求参数,将请求参数赋值给Action属性,执行数据校验,文件上传等等操作。当需要扩展Struts2的功能时,我们只需要提供相应的拦截器并将它配置...

    Struts2 入门培训

    4 1.3.2. 受控目录 5 2. 入门例子 5 2.1. 项目 5 2.2. 在WEB.XML中,配置FILTERDISPATCHER过滤器 6 2.3. 配置STRUTS.PROPERTIES 6 2.4. 编写一个简单的ACTION类 7 2.5. STRUTS.XML配置文件 8...

    Struts2演示源码

    该代码演示了Action中result的四种转发类型、多文件上传、自定义拦截器、对Action中方法进行输入校验以及OGNL表达式等内容。

    Struts2串讲

    1、Struts2概述 2、Struts2的前端控制器 3、自定义Interceptor 4、一些常用的API和数据模型 5、一些标签的使用

    深入浅出Struts 2 .pdf(原书扫描版) part 1

    书中介绍了如何利用Struts 2 来解决Web 应用开发中的常见问题,同时还深入浅出地探讨了许多能帮助程序员编写Struts 2 应用程序的技巧,如管理页面导航活动、输入验证、国际化和本地化、对Ajax 的支持,等等。...

    struts2拦截器

    拦截器可以说相当于是个过滤器:就是把你不想要的或不想显示的内容给过滤掉。拦截器可以抽象出一部分代码可以用来完善原来的...这样如果有新增权限的话,不用在Action里修改任何代码,直接在Interceptor里修改就行了。

    spring2.5 struts2.0 hibernate3.2.5 搭建的企业级开发基础模块

    Struts:struts.xml、struts.properties 配置很简单,用点心看就会了,多的不说了,好好享受咯 哦 忘记介绍了, 当中还有本人写的几个自定义标签:com.light.framework.tag 自定义标签的帮助类:...

Global site tag (gtag.js) - Google Analytics