- 浏览: 404422 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
skying007:
...
Mysql coalesce() 函数认识和用法 -
kchiu:
关于这个心跳的发送和接收 晚上全是the fuccking s ...
socket心跳检测 -
bjsq618:
你的想法在大的物流公司已经实现了,只不过他们使用是GPS定位
ddd -
jiaguwen123:
2,AuthenticationHandler类的写法
pa ...
xfire客户端 -
sornor:
总结的不错哟!
Java中的函数yield(),sleep()和wait()的区别
主要通过一个自定义interceptor简单的例子来解释 Struts 2 interceptor 工作流程
例子非常简单 没有什么业务 处理,重在 试验 工作流程!
环境:
调试环境:MyEclipse8.5刚下的 非常双爽
web容器:tomcat6
一.自定义interceptor 代码:
这里定义两个interceptor 来进行试验:
//打个包包!
package com.struts.interceptor;
//引入必要的类
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
//继承interceptor接口
public class InterOne implements Interceptor {
//复写destory()方法就一句话主要看看什么时候调用!
@Override
public void destroy() {
System.out.println("this is interceptor one destory ");
}
//复写init()方法就一句话主要看看什么时候调用!
@Override
public void init() {
// TODO Auto-generated method stub
System.out.println("this is interceptor one init ");
}
//复写interceptor方法,主要的业务处理模块,试验目的触发interceptor时方法中代码执行顺序!
@Override
public String intercept(ActionInvocation ait) throws Exception {
System.out.println("this is interceptor one befor invoke");
ait.invoke();//调用下一个interceptor
System.out.println("this is interceptor one after invoke");
return null;
}
}
第二个interceptor与上一个基本相同
package com.struts.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class InterTwo implements Interceptor {
@Override
public void destroy() {
System.out.println("this is interceptor two destory ");
}
@Override
public void init() {
// TODO Auto-generated method stub
System.out.println("this is interceptor two init ");
}
@Override
public String intercept(ActionInvocation ait) throws Exception {
System.out.println("this is interceptor two befor invoke");
ait.invoke();
System.out.println("this is interceptor two after invoke");
return null;
}
}
二.做个jsp页面:
<body>
<% System.out.println("this is jsp!"); %> <!-- 在命令行输出点信息,一边看到触发流程-->
this is interceptortest.jsp <br>
</body>
三.配置web.xml这个东东,在myeclipse中会默认生成!只要选择相应的版本!
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
四.配置struts.xml
<struts>
<!--定义为调试模式-->
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<!--把interceptor定义在package中,会把package中的方法都加上形同的interceptor-->
<interceptors>
<interceptor name="interceptorone" class="com.struts.interceptor.InterOne"/>
<interceptor name="interceptortow" class="com.struts.interceptor.InterTwo"/>
<interceptor-stack name="mystack">
<interceptor-ref name="interceptorone"/>
<interceptor-ref name="interceptortow"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="mystack"/>
<action name="interceptor">
<result>/interceptortest.jsp</result>
</action>
</package>
</struts>
五.部署,调试,查看输出:
1.启动容器:
信息: Overriding property struts.i18n.reload - old value: false new value: true
2010-4-1 1:04:05 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Overriding property struts.configuration.xml.reload - old value: false new value: true
看见在启动容器时 interceptor方法中的init方法就已经进行初始化了!
this is interceptor one init
this is interceptor two init
2010-4-1 1:04:07 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2010-4-1 1:04:07 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2010-4-1 1:04:07 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/63 config=null
2010-4-1 1:04:07 org.apache.catalina.startup.Catalina start
信息: Server startup in 9733 ms
2.
地址栏输入http://localhost:8080/strutsinterceptor/interceptor.action
查看命令行输出:
看见,在触发interceptor时 会先触发 第一个interceptor相应方法中的invoke()之前的代码之后是第二个,在返回jsp页面后会反向执行 invoke()方法之后的代码!
this is interceptor one befor invoke
this is interceptor two befor invoke
this is jsp!
this is interceptor two after invoke
this is interceptor one after invoke
3.结束容器:
命令行输出了定义在 interceptor中destory()方法中的代码!注意输出顺序也是以stack方式运行的
信息: Stopping service Catalina
this is interceptor two destory
this is interceptor one destory
2010-4-1 1:10:19 org.apache.coyote.http11.Http11Protocol destroy
信息: Stopping Coyote HTTP/1.1 on http-8080
好了基本就是这些已经可以说明问题了!
发表评论
-
Struts2自定义拦截器—Session超时的处理
2011-10-11 11:20 1467http://blog.csdn.net/java_cxrs/ ... -
struts2工作原理
2010-09-14 13:09 1632一个请求在Struts2框架中的处理分为以下几个步骤: 1.客 ... -
struts2配置多个struts.xml文件
2010-09-14 12:36 2250在Struts2中如何加载多个配置文件,不是用include的 ... -
struts中#、%和$这三个符号的使用方法
2010-08-14 01:07 1024一、"#"的用法 1、 访问OGNL ... -
s:url标签使用
2010-08-11 16:39 9059一. value <s:set name ... -
标签tree使用
2010-08-11 16:26 1030<head> <s:head theme ... -
s:同行显示
2010-08-11 11:39 1001<s:form action="Login ... -
拦截器详解
2010-08-02 12:51 795在之前的文章中,我们 ... -
Struts2的国际化支持
2010-08-02 11:23 984每种框价都会有国际化的支持,struts2的国际化大致上分 ... -
struts1与struts2区别
2010-07-26 14:01 8301 在Action实现类方面的对比:Struts 1要求 ... -
struts2中ognl表达式<二>
2010-07-26 11:08 1334OGNL是Object Graph Navigation La ... -
struts2中ognl表达式<一>
2010-07-26 10:54 1912OGNL —— 完美的催化剂 为了解决数 ... -
struts2 iterator标签 status属性
2010-07-22 10:22 1231iterator标签主要是用于迭代输出集合元素,如list s ... -
Struts2.1 标签详细说明
2010-07-22 10:09 872附件为:struts2标签详细说明! -
The Struts dispatcher cannot be found. This is usually caused by using Struts ta
2010-07-21 19:35 1443异常信息:The Struts dispatcher cann ... -
org.apache.struts2.util.Counter
2010-07-21 15:40 2731<s:bean name="org.apach ... -
使用struts2中的s:bean标签
2010-07-21 15:26 1800先添加一个bean: Test1.java package ... -
<s:token />防重复提交
2010-07-21 13:58 1862<s:token />标签防止重复提交,用法如下: ... -
struts2输入校验
2010-07-19 17:36 1009***************** regist.jsp ... -
struts2.0的资源文件读取
2010-07-19 15:54 2731原文地址(可查看图片):http://axuxinggang. ...
相关推荐
详细介绍struts2 interceptor
Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)
NULL 博文链接:https://weizhilizhiwei.iteye.com/blog/1005210
struts2 Interceptor拦截器 http://whitewolf.0fees.net
Struts2_interceptor_和_filter区别
STRUTS2:拦截器Interceptor
简单的拦截器例子,适合初学者学习,也是一个备忘录
用注解(annotation)方式配置struts2 (webwork) 中action的拦截器(interceptor)
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.
struts1 拦截器功能的实现。struts2 中的interceptor很方便。但在struts1中没有这个功能,用这个插件很容易实现。
2.2 Struts的动作处理流程 15 2.3 拦截器 17 2.4 Struts配置文件 18 2.4.1 struts.xml文件 19 2.4.2 struts.properties文件 26 2.5 Struts应用程序示例 26 2.5.1 部署描述文件和Struts配置文件 27 2.5.2 动作...
一个请求在Struts 2框架中的处理大概分为以下几个步骤。 客户端提交一个(HttpServletRequest)请求,如上文在浏览器中输入 http://localhost: 8080/bookcode/ch2/Reg.action就是提交一个(HttpServletRequest)...
Struts2.x_Interceptor已经打包好了,解压后直接用,
综合使用servlet filter与struts2 interceptor对权限进行验证。
struts2 拦截器_教程.zip
Struts2.3.6实现自定义拦截器Interceptor http://blog.csdn.net/alanchen520/article/details/34086699
《Struts 2实战》结合实例介绍了Struts 2框架,主要内容包括Action、Result、Interceptor等框架组件,基于注解的配置选项等新特征,Struts 2插件 FreeMarker,如何从Struts 1和WebWork 2迁移到Struts 2,Ajax标签、...
Interceptor,学习Struts 2实例,一步步教你如何实现一个示例