Struts框架只允许应用中存在一个ActionServlet类,但是可以存在多个客户化的RequestProcessor类,每个子应用模块都可以有单独的RequestProcessor类,
ActionServlet主要负责初始化,以及介绍请求并找到合适的RequestRrocessor,之后真正干活的是RequestProecssor和Action.
上回说到ActionServlet的process方法最终会调用RequestProcessor类的process方法.下面介绍这个方法.
一.RequestProcessor的process方法
-
- public void process(HttpServletRequest request,
- HttpServletResponse response)
- throws IOException, ServletException {
-
- request = processMultipart(request);
-
- String path = processPath(request, response);
- if (path == null) {
- return;
- }
- if (log.isDebugEnabled()) {
- log.debug("Processing a '" + request.getMethod() +
- "' for path '" + path + "'");
- }
-
- processLocale(request, response);
-
- processContent(request, response);
- processNoCache(request, response);
-
- if (!processPreprocess(request, response)) {
- return;
- }
- this.processCachedMessages(request, response);
-
- ActionMapping mapping = processMapping(request, response, path);
- if (mapping == null) {
- return;
- }
-
- if (!processRoles(request, response, mapping)) {
- return;
- }
-
- ActionForm form = processActionForm(request, response, mapping);
- processPopulate(request, response, form, mapping);
-
- 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;
- }
-
-
- if (!processForward(request, response, mapping)) {
- return;
- }
- if (!processInclude(request, response, mapping)) {
- return;
- }
-
- Action action = processActionCreate(request, response, mapping);
- if (action == null) {
- return;
- }
-
- ActionForward forward =
- processActionPerform(request, response,
- action, form, mapping);
-
-
- processForwardConfig(request, response, forward);
-
- }
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);
}
1)
调用processMultipart()方法 如果HTTP请求方式为post,并且contentType为”multipart/form-data”开头,标准的HttpServletRequest对象将被重新包装,以方便处理”multipart”类型的HTTP请求.如果请求方式为get,或正congtentType属性不是”mulitipart”,就直接返回原始的HttpServletRequest对象.
2) 调用processPath()方法 获得请求的URI的路径,这一信息可用于选择合适的Struts Action组件.
3) 调用processLocale方法 当ControllerConfig对象的locale属性为true,将读取用户请求中包含的Locale信息,然后把Locale实例保存在session范围内.
4) 调用processContendType(contentType)方法 读取ControllerConfig对象的conttentType属性,然后调用response.setContentType(contentType)方法,设置响应结果的文档类型和字符编码.
processContent()方法如下
- protected void processContent(HttpServletRequest request,
- HttpServletResponse response) {
-
- String contentType = moduleConfig.getControllerConfig().getContentType();
- if (contentType != null) {
- response.setContentType(contentType);
- }
-
- }
protected void processContent(HttpServletRequest request,
HttpServletResponse response) {
String contentType = moduleConfig.getControllerConfig().getContentType();
if (contentType != null) {
response.setContentType(contentType);
}
}
5) 调用processNoCache()方法 读取ControllerConfig对象的nocache属性,如果nocache属性为true,在响应结果中将加入特定的头参数:Pragma,Cache-Control和Expires,
防止页面被存储在客户的浏览器的缓存中,processNoCache方法的代码如下:
- protected void processNoCache(HttpServletRequest request,
- HttpServletResponse response) {
-
- if (moduleConfig.getControllerConfig().getNocache()) {
- response.setHeader("Pragma", "No-cache");
- response.setHeader("Cache-Control", "no-cache,no-store,max-age=0");
- response.setDateHeader("Expires", 1);
- }
- }
protected void processNoCache(HttpServletRequest request,
HttpServletResponse response) {
if (moduleConfig.getControllerConfig().getNocache()) {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache,no-store,max-age=0");
response.setDateHeader("Expires", 1);
}
}
6)调用processPreprocess()方法 该方法不执行任何操作.直接返回true.子类可以覆盖这个方法.
执行客户化的预处理请求操作.
7)调用processMapping()方法 寻找和用户请求的URI匹配的ActionMapping,如果不存在这样的ActionMapping,则向用户返回恰当的错误信息.
8)调用processRoles()方法 先判断是否为Action配置了安全角色,如果配置了安全角色,就调用isUserInRole()方法判断当前用户是否具备必需的角色,如果不具备,就结束请求处理流程.,向用户返回恰当的错误消息.
9)调用processActionForm()方法 先判断是否为ActionMapping配置了ActionForm,如果配置了ActionForm,就先从ActionForm的存在范围内(request或session)寻找改ActionForm实例,如果不存在,就创建一个实例,接下来把它保存在合适的范围内,保存时使用的属性key为ActionMapping的name属性。
10)调用processPopulate()方法 如果为ActionMapping配置了ActionForm,就先调用ActionForm的reset()方法,再把请求中的表单数据组装到ActionForm中。
11)调用processValidate()方法 如果为ActionMapping配置了ActionForm,并且ActionMapping的validate属性为true,就调用ActionForm的validate()方法,如果validate方法返回的ActionErrors对象中包含ActionMessage对象,说明表单验证失败。就把ActionErrors对象放在request范围内,再把请求转发到ActionMapping的input属性指定的Web组件。如果ActionForm的validate方法执行表单验证成功,就继续执行下面的处理流程。
12)调用processForward()方法 判断是否在ActionMapping中配置了forward属性。如果配置了这个属性,就调用RequestDispatcher的forward方法,请求处理流程结束。否则进行下一步。
13)调用processInclude()方法 判断是否在ActionMapping中配置了include属性。如果配置了这个属性,就调用RequestDispatcher的include方法,请求处理流程结束。否则进行下一步。
14)调用processActionCreate()方法 先判断是否在Action缓存中存在这个Action实例,如果没有就新建一个Action实例,把它放在Action缓存中。可以看出Action也是只有一个实例在运行的。
15)调用processActionPerform 该方法调用Action实例的execute方法,该方法位于try/catch中,以及捕获异常。processActionPerform()方放代码如下。
- protected ActionForward
- processActionPerform(HttpServletRequest request,
- HttpServletResponse response,
- Action action,
- ActionForm form,
- ActionMapping mapping)
- throws IOException, ServletException {
- try {
- return (action.execute(mapping, form, request, response));
- } catch (Exception e) {
- return (processException(request, response,
- e, form, mapping));
- }
-
protected ActionForward
processActionPerform(HttpServletRequest request,
HttpServletResponse response,
Action action,
ActionForm form,
ActionMapping mapping)
throws IOException, ServletException {
try {
return (action.execute(mapping, form, request, response));
} catch (Exception e) {
return (processException(request, response,
e, form, mapping));
}
}
16)调用processActionForward方法 把你的Action的excute方法返回的ActionFoward对象作为参数传给它,processActionForward对象包的请求转发信息来执行请求转发或重定向。
在RequestProcessor类的process方法中,会访问ControllerConfig、ActionMappig和ActionForward实力的属性,ControllerConfig类和struts配置文件的
r元素对应,ActionMapping类和元素对应,ActionForward和元素对应,process方法通过访问这三个类实例的属性来获得相关的配置信息。
写了这么多,RequestProcessor干得事够多的吧。
二.扩展RequestProcessor类
如果想修改RequestProcessor的一些默认功能,改易覆盖RequestProcessor基类中的相关方法.
- Public class CustomRequestProcessor extends RequestProcessor{
- protected void processPreprocess (HttpServletRequest request,
- HttpServletResponse response) {
- ………………….
- }
- }
Public class CustomRequestProcessor extends RequestProcessor{
protected void processPreprocess (HttpServletRequest request,
HttpServletResponse response) {
………………….
}
}
在struts配置文件中,元素的processorClass属性用于配置你自己的RequestProcessor类
- contentType=“text/html:charset=”GB2312”
- locale=”true” nocache=”true” processorCalss=”com.test.CustomRequestProcessor”/>