strut组件-RequestProcessor类

Struts框架只允许应用中存在一个ActionServlet类,但是可以存在多个客户化的RequestProcessor类,每个子应用模块都可以有单独的RequestProcessor类,

ActionServlet主要负责初始化,以及介绍请求并找到合适的RequestRrocessor,之后真正干活的是RequestProecssor和Action.
上回说到ActionServlet的process方法最终会调用RequestProcessor类的process方法.下面介绍这个方法.
一.RequestProcessor的process方法
Java代码 复制代码 收藏代码
  1. public void process(HttpServletRequest request,
  2. HttpServletResponse response)
  3. throws IOException, ServletException {
  4. // Wrap multipart requests with a special wrapper
  5. request = processMultipart(request);
  6. // Identify the path component we will use to select a mapping
  7. String path = processPath(request, response);
  8. if (path == null) {
  9. return;
  10. }
  11. if (log.isDebugEnabled()) {
  12. log.debug("Processing a '" + request.getMethod() +
  13. "' for path '" + path + "'");
  14. }
  15. // Select a Locale for the current user if requested
  16. processLocale(request, response);
  17. // Set the content type and no-caching headers if requested
  18. processContent(request, response);
  19. processNoCache(request, response);
  20. // General purpose preprocessing hook
  21. if (!processPreprocess(request, response)) {
  22. return;
  23. }
  24. this.processCachedMessages(request, response);
  25. // Identify the mapping for this request
  26. ActionMapping mapping = processMapping(request, response, path);
  27. if (mapping == null) {
  28. return;
  29. }
  30. // Check for any role required to perform this action
  31. if (!processRoles(request, response, mapping)) {
  32. return;
  33. }
  34. // Process any ActionForm bean related to this request
  35. ActionForm form = processActionForm(request, response, mapping);
  36. processPopulate(request, response, form, mapping);
  37. // Validate any fields of the ActionForm bean, if applicable
  38. try {
  39. if (!processValidate(request, response, form, mapping)) {
  40. return;
  41. }
  42. } catch (InvalidCancelException e) {
  43. ActionForward forward = processException(request, response, e, form, mapping);
  44. processForwardConfig(request, response, forward);
  45. return;
  46. } catch (IOException e) {
  47. throw e;
  48. } catch (ServletException e) {
  49. throw e;
  50. }
  51. // Process a forward or include specified by this mapping
  52. if (!processForward(request, response, mapping)) {
  53. return;
  54. }
  55. if (!processInclude(request, response, mapping)) {
  56. return;
  57. }
  58. // Create or acquire the Action instance to process this request
  59. Action action = processActionCreate(request, response, mapping);
  60. if (action == null) {
  61. return;
  62. }
  63. // Call the Action instance itself
  64. ActionForward forward =
  65. processActionPerform(request, response,
  66. action, form, mapping);
  67. // Process the returned ActionForward instance
  68. processForwardConfig(request, response, forward);
  69. }
 
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()方法如下
Java代码 复制代码 收藏代码
  1. protected void processContent(HttpServletRequest request,
  2. HttpServletResponse response) {
  3. String contentType = moduleConfig.getControllerConfig().getContentType();
  4. if (contentType != null) {
  5. response.setContentType(contentType);
  6. }
  7. }
 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方法的代码如下:
Java代码 复制代码 收藏代码
  1. protected void processNoCache(HttpServletRequest request,
  2. HttpServletResponse response) {
  3. if (moduleConfig.getControllerConfig().getNocache()) {
  4. response.setHeader("Pragma", "No-cache");
  5. response.setHeader("Cache-Control", "no-cache,no-store,max-age=0");
  6. response.setDateHeader("Expires", 1);
  7. }
  8. }
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()方放代码如下。
Java代码 复制代码 收藏代码
  1. protected ActionForward
  2. processActionPerform(HttpServletRequest request,
  3. HttpServletResponse response,
  4. Action action,
  5. ActionForm form,
  6. ActionMapping mapping)
  7. throws IOException, ServletException {
  8. try {
  9. return (action.execute(mapping, form, request, response));
  10. } catch (Exception e) {
  11. return (processException(request, response,
  12. e, form, mapping));
  13. }
 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配置文件的<controlle>r元素对应,ActionMapping类和<action>元素对应,ActionForward和<forward>元素对应,process方法通过访问这三个类实例的属性来获得相关的配置信息。
写了这么多,RequestProcessor干得事够多的吧。

二.扩展RequestProcessor类
如果想修改RequestProcessor的一些默认功能,改易覆盖RequestProcessor基类中的相关方法.
Java代码 复制代码 收藏代码
  1. Public class CustomRequestProcessor extends RequestProcessor{
  2. protected void processPreprocess (HttpServletRequest request,
  3. HttpServletResponse response) {
  4. ………………….
  5. }
  6. }
Public class CustomRequestProcessor extends RequestProcessor{
  protected void processPreprocess (HttpServletRequest request,
                                 HttpServletResponse response) { 
………………….
}
}

在struts配置文件中,<controller>元素的processorClass属性用于配置你自己的RequestProcessor类
Java代码 复制代码 收藏代码
  1. </controller
  2. contentType=“text/html:charset=”GB2312”
  3. locale=”true” nocache=”true” processorCalss=”com.test.CustomRequestProcessor”/>
</controller 
contentType=“text/html:charset=”GB2312”
locale=”true” nocache=”true” processorCalss=”com.test.CustomRequestProcessor”/>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值