`
jiaguwen123
  • 浏览: 404422 次
  • 性别: Icon_minigender_2
  • 来自: 深圳
社区版块
存档分类
最新评论

浅谈Struts 2 interceptor 工作流程

阅读更多

主要通过一个自定义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

好了基本就是这些已经可以说明问题了!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics