Struts2 第三讲 -- Struts2的处理流程
4.Struts2的处理流程

以下是struts-defautl.xml中的拦截器

建议通过这个struts-default的副本查看,更形象

它实现了很多的功能,其中包括国际化,文件上传,类型转换,表单验证,都是在跳转到Action类处理之前就做好了,所有我们只需要在Action类中使用就可以了,大大方便了我们的开发
下面通过使用eclipse的断点调试,看看拦截器的执行过程。我们分别在一下三个拦截器中设置断点,在我们的三个拦截器类中(invocation.invoke()所在函数中断点)中设置断点,然后根据流程查看拦截器执行顺序.

@Override
public String doIntercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction(); if (action instanceof Preparable) {
try {
String[] prefixes;
if (firstCallPrepareDo) {
prefixes = new String[] {ALT_PREPARE_PREFIX, PREPARE_PREFIX};
} else {
prefixes = new String[] {PREPARE_PREFIX, ALT_PREPARE_PREFIX};
}
PrefixMethodInvocationUtil.invokePrefixMethod(invocation, prefixes);
}
catch (InvocationTargetException e) {
/*
* The invoked method threw an exception and reflection wrapped it
* in an InvocationTargetException.
* If possible re-throw the original exception so that normal
* exception handling will take place.
*/
Throwable cause = e.getCause();
if (cause instanceof Exception) {
throw (Exception) cause;
} else if(cause instanceof Error) {
throw (Error) cause;
} else {
/*
* The cause is not an Exception or Error (must be Throwable) so
* just re-throw the wrapped exception.
*/
throw e;
}
} if (alwaysInvokePrepare) {
((Preparable) action).prepare();
}
} return invocation.invoke();
}
可见简写成下面,容易理解
PrepareInterceptor @Override
public String doIntercept(ActionInvocation invocation) throws Exception { //invoke()表示放行,跳转到Action类(在跳转到Action类之前,先将所有的拦截器先执行完成)
//invoke()方法的返回值,返回Action类方法的返回值
System.out.println("PrepareInterceptor的being方法");
String value = invocation.invoke();
System.out.println("PrepareInterceptor的end方法");
return value
}
ChainingInterceptor @Override
public String intercept(ActionInvocation invocation) throws Exception {
ValueStack stack = invocation.getStack();
CompoundRoot root = stack.getRoot();
if (shouldCopyStack(invocation, root)) {
copyStack(invocation, root);
}
//invoke()表示放行,跳转到Action类(在跳转到Action类之前,先将所有的拦截器先执行完成)
//invoke()方法的返回值,返回Action类方法的返回值
System.out.println("ChainingInterceptorbeing的begin方法");
String value = invocation.invoke();
System.out.println("ChainingInterceptor的end方法");
return value
ParametersInterceptor @Override
public String doIntercept(ActionInvocation invocation) throws Exception { //invoke()表示放行,跳转到Action类(在跳转到Action类之前,先将所有的拦截器先执行完成)
//invoke()方法的返回值,返回Action类方法的返回值
System.out.println("ParametersInterceptor的begin方法");
String value = invocation.invoke();
System.out.println("ParametersInterceptor的end方法");
return value
}
public class HelloWorldAction extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("欢迎访问HelloWorldAction中的execute方法!");
return "success";
}
//自定义add方法
public String add(){
System.out.println("欢迎访问HelloWorldAction中的add的方法!");
return "success";
}
}
这里注意:当执行每个拦截器时,调用拦截器中的invocation.invoke();方法前的内容,此时会陆续继续执行拦截器,执行拦截器的顺序是prepare、chain、params。那么执行完Action的方法后,即调用invocation.invoke(); 方法,会继续执行params、chain、prepare拦截器中invocation.invoke();后的内容,所以我们看到顺序会是后又3,2,1.

注意上面只是为了方便理解,所以简化了源码并在源码中添加输出语句,实际源码不能改变,具体可以自己打断点,看一下流程运转;
Struts2 第三讲 -- Struts2的处理流程的更多相关文章
- Struts2入门2 Struts2深入
Struts2入门2 Struts2深入 链接: http://pan.baidu.com/s/1rdCDh 密码: sm5h 前言: 前面学习那一节,搞得我是在是太痛苦了.因为在Web项目中确实不知 ...
- Struts2入门1 Struts2基础知识
Struts2入门1 Struts2基础知识 20131130 代码下载: 链接: http://pan.baidu.com/s/11mYG1 密码: aua5 前言: 之前学习了Spring和Hib ...
- 【Struts2】剖析Struts2中的反射技术 ValueStack(值栈)
1,Struts2框架主要组件的处理流程 在说ValueStack之前,笔者先说一说Struts2中常用的组件,struts2中常用组件有strutsPrepareAndExecuteExceptio ...
- Struts2笔记02——Struts2 概述(转)
原始内容:https://www.tutorialspoint.com/struts_2/basic_mvc_architecture.htm Struts2是基于MVC设计模式的一种流行.成熟的We ...
- Struts2学习一----------Struts2的工作原理及HelloWorld简单实现
© 版权声明:本文为博主原创文章,转载请注明出处 Struts2工作原理 一个请求在Struts2框架中的处理步骤: 1.客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2.这个请求 ...
- Struts2 Convention Plugin ( struts2 零配置 )
Struts2 Convention Plugin ( struts2 零配置 ) convention-plugin 可以用来实现 struts2 的零配置.零配置的意思并不是说没有配置,而是通过约 ...
- Struts2 四、Struts2 处理流程
1. 一个请求在Struts2框架中的处理步骤: a) 客户端初始化一个指向Servlet容器的请求: b) 根据Web.xml配置,请求首先经过ActionContextCleanUp过滤器,其为可 ...
- Struts2的工作原理及工作流程
众所周知,Struts2是个非常优秀的开源框架,我们能用Struts2框架进行开发,同时能 快速搭建好一个Struts2框架,但我们是否能把Struts2框架的工作原理用语言表达清楚,你表达的原理不需 ...
- struts2 之 【struts2简介,struts2开发步骤,struts2详细配置,struts2执行流程】
入门框架学习避免不了的问题: 1. 什么是框架? 简单的说,框架就是模板,模子,模型.就是一个可重用的半成品. 2. 如何学习框架? 学习框架其实就是学习规则,使用框架就是遵循框架的规则,框架是可变的 ...
随机推荐
- php 项目中自定义日志方法
在现在项目中之前没有定义日志的方法,每次调试起来很麻烦,经常不能输出参数,只能用写日志的方法,一直用file_put_contents很烦躁,于是用了一点时间,写了这样一个方法: <?php / ...
- artDialog组件应用学习(二)
一.没有操作选项的对话框 预览: html前台引入代码:(之后各种效果对话框引入代码致,调用方法也一样,就不一一写入) <script type="text/javascript&qu ...
- BindingResult参数验证的跨层次迭代验证
public ResponseWrapper<Object> recordAdd(@RequestBody @Valid ReqAddEnterpriseInfoDTO addEnterp ...
- scss-变量分隔符
scss的变量名可以与css中的属性名和选择器名称相同,包括中划线和下划线. 在使用中划线还是下划线来进行变量分隔完全根据个人喜好. scss完全兼容这两种写法,也就是说scss认为中划线和下划线是完 ...
- Java操作Mongodb(转载)
好文章,值得收藏 HelloWorld程序 学习任何程序的第一步,都是编写HelloWorld程序,我们也不例外,看下如何通过Java编写一个HelloWorld的程序. 首先,要通过Java操作Mo ...
- 【Linux】GCC编译
GCC简介 GCC基本用法 GCC程序产生过程 GCC编译选项 一.GCC简介 1.1 GCC特点 Gcc(GNU C Compiler)是GNU推出的功能强大.性能优越的多平台编译器,是GNU的 代 ...
- Multidex (方法数超过限制的处理)
报错 : Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: 65536 ...
- 如何设置树莓派的VNC开机时启动
转载:http://www.linuxidc.com/Linux/2016-12/138793.htm 下面正式开始配置 首先 sudo nano /etc/init.d/vncserver 然后 复 ...
- OLAP和数据挖掘的区别
总结来说: 数据仓库提供了一个分析的数据源 数据挖掘能分析出未知的信息,提出假设 OLAP能通过分析,验证假设 从技术角度看,商务智能的过程是企业的决策人员以企业中的数据仓库为基础,经由数据挖掘工具. ...
- monkeyrunner多点触摸
思路是:在屏幕上某个位置按着不放:device.touch(x,y,md.DOWN) 然后再做一个滑动的操作:device.drap((x1,y1),(x2,y2),0.2,10) 然后再松开按键:d ...