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的处理流程的更多相关文章

  1. Struts2入门2 Struts2深入

    Struts2入门2 Struts2深入 链接: http://pan.baidu.com/s/1rdCDh 密码: sm5h 前言: 前面学习那一节,搞得我是在是太痛苦了.因为在Web项目中确实不知 ...

  2. Struts2入门1 Struts2基础知识

    Struts2入门1 Struts2基础知识 20131130 代码下载: 链接: http://pan.baidu.com/s/11mYG1 密码: aua5 前言: 之前学习了Spring和Hib ...

  3. 【Struts2】剖析Struts2中的反射技术 ValueStack(值栈)

    1,Struts2框架主要组件的处理流程 在说ValueStack之前,笔者先说一说Struts2中常用的组件,struts2中常用组件有strutsPrepareAndExecuteExceptio ...

  4. Struts2笔记02——Struts2 概述(转)

    原始内容:https://www.tutorialspoint.com/struts_2/basic_mvc_architecture.htm Struts2是基于MVC设计模式的一种流行.成熟的We ...

  5. Struts2学习一----------Struts2的工作原理及HelloWorld简单实现

    © 版权声明:本文为博主原创文章,转载请注明出处 Struts2工作原理 一个请求在Struts2框架中的处理步骤: 1.客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2.这个请求 ...

  6. Struts2 Convention Plugin ( struts2 零配置 )

    Struts2 Convention Plugin ( struts2 零配置 ) convention-plugin 可以用来实现 struts2 的零配置.零配置的意思并不是说没有配置,而是通过约 ...

  7. Struts2 四、Struts2 处理流程

    1. 一个请求在Struts2框架中的处理步骤: a) 客户端初始化一个指向Servlet容器的请求: b) 根据Web.xml配置,请求首先经过ActionContextCleanUp过滤器,其为可 ...

  8. Struts2的工作原理及工作流程

    众所周知,Struts2是个非常优秀的开源框架,我们能用Struts2框架进行开发,同时能 快速搭建好一个Struts2框架,但我们是否能把Struts2框架的工作原理用语言表达清楚,你表达的原理不需 ...

  9. struts2 之 【struts2简介,struts2开发步骤,struts2详细配置,struts2执行流程】

    入门框架学习避免不了的问题: 1. 什么是框架? 简单的说,框架就是模板,模子,模型.就是一个可重用的半成品. 2. 如何学习框架? 学习框架其实就是学习规则,使用框架就是遵循框架的规则,框架是可变的 ...

随机推荐

  1. js面向对象2

    1.发展史 面向机器 面向过程:将程序的执行分解成若干个步骤 面向对象:将程序的执行分解成若干个事物 2.面向对象两个基本概念 类:代表某类事物,是抽象的 对象:代表某个事物,是具体的 3.快速入门 ...

  2. 修改Linux时区的2种办法

    由于Azure 上所有的服务时间都采用了 UTC 时间.UTC 时间比中国时间晚 8 个小时,该如何按照自己的需要来进行修改呢,下面提供2种办法以供参考: 1.修改 /etc/localtime 文件 ...

  3. 03.if 和 switch结合练习

    namespace _04.练习01 { class Program { static void Main(string[] args) { //请用户输入年份,再输入月份,输出该月有多少天 Cons ...

  4. [LeetCode]20. Valid Parentheses有效的括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  5. Java 的版本历史与特性

    Java SE 8[2014-03-14发行] Lambda表达式 Pipelines和Streams Date和Time API Default方法 Type注解 Nashhorn JavaScri ...

  6. 集合之Iterator迭代器

      Iterator迭代器概述: java中提供了很多个集合,它们在存储元素时,采用的存储方式不同.我们要取出这些集合中的元素,可通过一种通用的获取方式来完成. Collection集合元素的通用获取 ...

  7. javascript: Convert special characters to HTML

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. vuejs源码摘抄

    订阅功能的部分实现代码如下: /* */ var uid = 0; /** * A dep is an observable that can have multiple * directives s ...

  9. 如何用dva来构建你的应用

    dva的github地址: https://github.com/dvajs/dva-knowledgemap#%E9%80%9A%E8%BF%87-connect-%E7%BB%91%E5%AE%9 ...

  10. 使用 yield生成迭代对象函数

    https://www.cnblogs.com/python-life/articles/4549996.html https://www.liaoxuefeng.com/wiki/001431608 ...