这篇博客我们深入Struts框架执行部分源码,从ActionServlet的process函数开始,看一下其内在的执行过程。
流程图
以下流程图展示的是ActionServlet和RequestProcessor两个类用到的函数,如RequestProcessor调用的其它类的函数不再说明。
函数说明
我们选择几个重要的函数说明,其它函数则简单说明一下即可。
ActionServlet
process
/**
* <p>Perform the standard request processing for this request, and create
* the corresponding response.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception is thrown
*/
protected void process(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException { ModuleUtils.getInstance().selectModule(request, getServletContext());
ModuleConfig config = getModuleConfig(request); RequestProcessor processor = getProcessorForModule(config);
if (processor == null) {
processor = getRequestProcessor(config);
}
processor.process(request, response); } }
在调试时首先进入这个函数(Tomcat启动完毕,产生请求后),这个函数的作用是获取加载阶段产生的Module对象,产生struts逻辑处理的主要对象RequestProcessor。
RequestProcessor
process
/**
* <p>Process an <code>HttpServletRequest</code> and create the
* corresponding <code>HttpServletResponse</code> or dispatch
* to another resource.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a processing exception occurs
*/
public void process(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException { // Wrap multipart requests with a special wrapper
request = processMultipart(request); // Identify the path component we will use to select a mapping
String path = processPath(request, response);
if (path == null) {
return;
} if (log.isDebugEnabled()) {
log.debug("Processing a '" + request.getMethod() +
"' for path '" + path + "'");
} // Select a Locale for the current user if requested
processLocale(request, response); // Set the content type and no-caching headers if requested
processContent(request, response);
processNoCache(request, response); // General purpose preprocessing hook
if (!processPreprocess(request, response)) {
return;
} this.processCachedMessages(request, response); // Identify the mapping for this request
ActionMapping mapping = processMapping(request, response, path);
if (mapping == null) {
return;
} // Check for any role required to perform this action
if (!processRoles(request, response, mapping)) {
return;
} // Process any ActionForm bean related to this request
ActionForm form = processActionForm(request, response, mapping);
processPopulate(request, response, form, mapping); // Validate any fields of the ActionForm bean, if applicable
try {
if (!processValidate(request, response, form, mapping)) {
return;
}
} catch (InvalidCancelException e) {
ActionForward forward = processException(request, response, e, form, mapping);
processForwardConfig(request, response, forward);
return;
} catch (IOException e) {
throw e;
} catch (ServletException e) {
throw e;
} // Process a forward or include specified by this mapping
if (!processForward(request, response, mapping)) {
return;
} if (!processInclude(request, response, mapping)) {
return;
} // Create or acquire the Action instance to process this request
Action action = processActionCreate(request, response, mapping);
if (action == null) {
return;
} // Call the Action instance itself
ActionForward forward =
processActionPerform(request, response,
action, form, mapping); // Process the returned ActionForward instance
processForwardConfig(request, response, forward); }
process是RequestProcessor对象主要的逻辑处理函数,根据上面的流程图可以看到,整个逻辑处理都是在这个函数中完成,它所调用的函数实现的功能如下:
processMultipart
这个函数的作用是判断是否是文件上传请求,如果是则特殊处理。
/**
* <p>If this is a multipart request, wrap it with a special wrapper.
* Otherwise, return the request unchanged.</p>
*
* @param request The HttpServletRequest we are processing
*/
protected HttpServletRequest processMultipart(HttpServletRequest request) { if (!"POST".equalsIgnoreCase(request.getMethod())) {
return (request);
} String contentType = request.getContentType();
if ((contentType != null) &&
contentType.startsWith("multipart/form-data")) {
return (new MultipartRequestWrapper(request));
} else {
return (request);
} }
processPath
获取并截取请求,处理后变为需要的字符串,例如请求:http://localhost:8080/struts_login/login.do,处理后的字符串为/login.do。
/**
* <p>Identify and return the path component (from the request URI) that
* we will use to select an <code>ActionMapping</code> with which to dispatch.
* If no such path can be identified, create an error response and return
* <code>null</code>.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*
* @exception IOException if an input/output error occurs
*/
protected String processPath(HttpServletRequest request,
HttpServletResponse response)
throws IOException { String path = null; // For prefix matching, match on the path info (if any)
path = (String) request.getAttribute(INCLUDE_PATH_INFO);
if (path == null) {
path = request.getPathInfo();
}
if ((path != null) && (path.length() > 0)) {
return (path);
} // For extension matching, strip the module prefix and extension
path = (String) request.getAttribute(INCLUDE_SERVLET_PATH);
if (path == null) {
path = request.getServletPath();
}
String prefix = moduleConfig.getPrefix();
if (!path.startsWith(prefix)) {
String msg = getInternal().getMessage("processPath"); log.error(msg + " " + request.getRequestURI());
response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); return null;
} path = path.substring(prefix.length());
int slash = path.lastIndexOf("/");
int period = path.lastIndexOf(".");
if ((period >= 0) && (period > slash)) {
path = path.substring(0, period);
}
return (path); }
设置相关函数
processLocale、processContent、processNoCache、processCachedMessages,这四个函数的作用分别是:设置国际化文件、设置内容类型、设置取消缓存、设置清楚session中struts的错误信息。
processMapping
根据上面生成的path,实例化对应的ActionMapping对象,如果此对象不为空则加载到request中,对应名称为Globals.MAPPING_KEY,如果为空,发送异常并添加到response中。
// If a mapping is found, put it in the request and return it
if (mapping != null) {
request.setAttribute(Globals.MAPPING_KEY, mapping);
return (mapping);
} // Locate the mapping for unknown paths (if any)
ActionConfig configs[] = moduleConfig.findActionConfigs();
for (int i = 0; i < configs.length; i++) {
if (configs[i].getUnknown()) {
mapping = (ActionMapping) configs[i];
request.setAttribute(Globals.MAPPING_KEY, mapping);
return (mapping);
}
}
processRoles
Action执行是否需要特定的角色权限,如果不需要,则继续执行。
String roles[] = mapping.getRoleNames();
if ((roles == null) || (roles.length < 1)) {
return (true);
}
processActionForm
创建ActionForm并检测此Action的作用域,如果是Request则添加到request中,如果是Session则添加到session中。
// Create (if necessary) a form bean to use
ActionForm instance = RequestUtils.createActionForm
(request, mapping, moduleConfig, servlet);
if (instance == null) {
return (null);
} // Store the new instance in the appropriate scope
if (log.isDebugEnabled()) {
log.debug(" Storing ActionForm bean instance in scope '" +
mapping.getScope() + "' under attribute key '" +
mapping.getAttribute() + "'");
}
if ("request".equals(mapping.getScope())) {
request.setAttribute(mapping.getAttribute(), instance);
} else {
HttpSession session = request.getSession();
session.setAttribute(mapping.getAttribute(), instance);
}
ActionForm相关
processPopulate这个函数的作用是:调用processPopulate()方法,如果存在为ActionMapping配置的ActionForm,则封装请求对象中的数据到ActionForm 中,在进行封装之前,先调用ActionForm 的reset()方法进行属性值的默认化
processValidate:如果action元素的属性validate被设置为true ,则进一步调用validate()方法进行规则校验。如果validate()方法校验失败,就会保存一个ActionErrors 对象到请求区域中,请求将会自动重定向到action映射的input属性所指定的页面中;如果校验通过或在action 映射中没有配置ActionForm,则继续处理请求。
异常处理
process这个函数中,包含一个try...catch块,如果出现InvalidCancelException则执行两个函数。
processException,将异常写入日志警告文件,并跑出异常;processForwardConfig,与下面最后执行的一个函数相同,捕获结束
跳转路径
processForward、processInclude:这个两个函数的作用是,检测struts-config下<action>元素的forward和include属性的值,如有配置,则把forward和include 请求放在配置的页面内;processForward()调用 RequestDispatcher.forward(),而processInclude()调用RequestDispatcher.include()。
如果同时配置了forward 和include 属性,Struts会优先选择forward。
processActionCreate
这个函数的作用是从struts-config下<action>的type属性得到Action类名,创建并返回它的实例。
Action instance = null;
synchronized (actions) { // Return any existing Action instance of this class
instance = (Action) actions.get(className);
if (instance != null) {
if (log.isTraceEnabled()) {
log.trace(" Returning existing Action instance");
}
return (instance);
} // Create and return a new Action instance
if (log.isTraceEnabled()) {
log.trace(" Creating new Action instance");
} try {
instance = (Action) RequestUtils.applicationInstance(className);
// :TODO: Maybe we should propagate this exception
// instead of returning null.
} catch (Exception e) {
log.error(
getInternal().getMessage("actionCreate", mapping.getPath()),
e); response.sendError(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
getInternal().getMessage("actionCreate", mapping.getPath())); return (null);
} instance.setServlet(this.servlet);
actions.put(className, instance);
} return (instance);
processActionPerform
执行自己写的Action中execute函数,当然包含跳转逻辑:
try {
return (action.execute(mapping, form, request, response));
} catch (Exception e) {
return (processException(request, response,
e, form, mapping));
}
processForwardConfig
String forwardPath = forward.getPath();
String uri = null; // paths not starting with / should be passed through without any processing
// (ie. they're absolute)
if (forwardPath.startsWith("/")) {
uri = RequestUtils.forwardURL(request, forward, null); // get module relative uri
} else {
uri = forwardPath;
}
doForward
if (request instanceof MultipartRequestWrapper) {
request = ((MultipartRequestWrapper) request).getRequest();
} RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
if (rd == null) {
response.sendError(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
getInternal().getMessage("requestDispatcher", uri));
return;
}
rd.forward(request, response);
总结
如果我们忽略函数,只从宏观上看,可以总结为:
- 读取配置文件
- 获取访问地址
- 设置Struts
- 创建、赋值、验证ActionForm
- 异常处理
- 创建Action
- 执行Action中的逻辑
- 页面跳转
至此,如果不追究到最底层的函数,struts的执行流程已经讲解完毕,如果有问题,欢迎大家指出。
更多相关博客,请至《层层递进Struts1(八)之总结》
层层递进Struts1(五)之处理流程的更多相关文章
-
层层递进Struts1(八)之总结
先来看一下Struts1学习总结的思维导图,画的主要是Struts1中的重点和博客分布,如下所示: 系列博客的侧重点是: Struts1是什么? 为什么要使用它? 如何使用Struts1? Strut ...
-
层层递进Struts1(七)详解DispatchAction
通过前面几篇博客,不知道大家有没有发现这个问题,虽然现在可以灵活控制跳转了,但是Action的数量还是比较多,如何既能保证跳转灵活,还能减少Action的数量?这就是我们这篇博客所说的Dispatch ...
-
层层递进Struts1(六)自定义转换器
Struts提供的类型转换有限,如果我们强行使用没有的类型转换,则会出现错误,以Date类型为例: org.apache.catalina.core.StandardWrapperValve invo ...
-
层层递进Struts1(三)之Struts组成
这篇博客我们来说一下Struts的主要组成我们,通过前几篇博客,我们知道这个框架最重要的几个步骤:获取路径.封装表单.获取转向列表.转向逻辑处理.转向,与此对应的是:ActionServlet.Act ...
-
Hadoop化繁为简(二)—层层递进轻松入门hdfs
层层递进-解开hdfs的面纱 1.hdfs是什么?它与普通服务器的文件系统有什么区别?它的特性有什么? 2.hdfs的工作原理是怎样的? 3.每台机器都单独启动有什么弊端?假设有1000台机器需要启动 ...
-
“全栈2019”Java第二十五章:流程控制语句中循环语句while
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
-
.Net基础篇_学习笔记_第五天_流程控制while循环
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
-
JavaScript(第五天)【流程控制语句】
ECMA-262规定了一组流程控制语句.语句定义了ECMAScript中的主要语法,语句通常由一个或者多个关键字来完成给定的任务.诸如:判断.循环.退出等. 一.语句的定义 在ECMAScri ...
-
嵌入式Linux驱动学习之路(五)u-boot启动流程分析
这里说的u-boot启动流程,值得是从上电开机执行u-boot,到u-boot,到u-boot加载操作系统的过程.这一过程可以分为两个过程,各个阶段的功能如下. 第一阶段的功能: 硬件设备初始化. 加 ...
随机推荐
-
vb6.0如何让窗体跟随鼠标运动
首先将form的boderstyle属性设为0 Dim movesScreen As Boolean Dim mousX As Integer Dim mousY As Integer Dim cur ...
-
Java单例模式深入详解
原文地址:http://www.cnblogs.com/hxsyl/ 仅作为笔记收藏…… 一.问题引入 偶然想想到的如果把Java的构造方法弄成private,那里面的成员属性是不是只有通过stati ...
-
cocoaPods的安装使用 以及 Carthage
http://cnbin.github.io/blog/2015/05/25/cocoapods-an-zhuang-he-shi-yong/ 按照这个步骤就OK Note:当引入已有的project ...
-
201521123062《Java程序设计》第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 for (int i = 0; i < list.size(); ...
-
mybatis查询修改同时操作
update dic_purchase set state =0 where purchase_number in (select tmp.* from (select purchase_number ...
-
Java8新特性_stream API 练习
交易员类 public class Trader { private String name; private String city; public Trader() { } public Trad ...
-
[特征工程]-------使用sklearn做单机特征工程[转载]
https://www.cnblogs.com/jasonfreak/p/5448385.html 使用sklearn做单机特征工程 目录 1 特征工程是什么?2 数据预处理 2.1 无量纲化 2.1 ...
-
Javascript高级编程学习笔记(9)—— 执行环境
今天主要讲一下,JS底层的一些东西,这些东西不太好举例(应该是我水平不够) 望大家多多海涵,比心心 执行环境 执行环境(执行上下文,全文使用执行环境 )是JS中最为重要的一个概念,执行环境决定了,变量 ...
-
用jq获取元素内文本,但不包括其子元素内的文本值的方法
<li id="listItem"> This is some text <span id="firstSpan">First span ...
-
python-day9-集合数据类型
pythons=['alex','egon','yuanhao','wupeiqi','gangdan','biubiu']linuxs=['wupeiqi','oldboy','gangdan'] ...