`

RequestProcessor类详解

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

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

相关推荐

    DisapatchAction测试和RequestProcessor控制器类

    DisapatchAction测试和RequestProcessor控制器类

    struts1整合spring 通过代理RequestProcessor实现

    这只是一个最简单的demo,只有一个action, 通过代理RequestProcessor实现

    JSP Struts配置文件详解

    默认org.apache.struts.action.RequestProcessor @tempDir:指定文件上传时的临时工作目录.如果没有设置,将才用Servlet容器为web应用分配的临时工作目录. @nochache:true时,在相应结果中加入特定的头参数:Pragma ,...

    Struts_config.xml详解

    parameter="" //指定Actgion的配置参数,在Action类的execute()方法中,可以调用ActionMapping对象的getParameter()方法来读取该配置参数。(相当于初始化赋值) &gt;&gt;&gt; prefix=""// 指定填充当前 Action 关联 FormBean...

    struts1流程和原理

    2、RequestProcessor类(处理异常的核心组件)。 3、ActionForm(接收页面中传过的数据)。 4、Action(是控制器,主要是从ActionForm中接收页面传进来的数据,然后进行逻辑 处理)。 5、ActionForward(页面跳转,...

    http sample

    JHTTP.java RequestProcessor.java

    JAVA程序开发大全---上半部分

    10.6.2 覆盖RequestProcessor类整合Struts 178 10.6.3 将Action管理委托给Spring框架 180 10.7 Spring整合Hibernate框架 182 10.7.1 创建Hibernate+Spring项目 182 10.7.2 反向工程生成Spring整合Hibernate的DAO 184...

    无意间发现的sturts过滤器

    Struts过滤器可以解决页面之间的乱码问题 一个类继承struts的RequestProcessor(分request 和response处理) 然后在struts-config.xml配置即可!

    jsp应用开发源代码

    流程控制层 通过一个特定的请求处理器JAVA类:RequestProcessor.java,将客户端的请求(如:http://localhost:6888/myshop/shp/cart 转化成某个事件(如:CartEvent、OrderEvent...),并交给控制器类...

    JSP应用开发源代码

    N-Tier式的分布式应用 表示层 JSP、HTML、DHTML、XML/XSL、JAVA BEAN、TAGLIB 流程控制层 通过一个特定的请求处理器JAVA类:RequestProcessor.java,将客户端的请求(如:http://localhost:6888/myshop/shp/cart 转化...

    Spring中集成Struts的三种方式

    //第一种方式:使用 Spring 的 ActionSupport 类整合 Structs //第二种方式:使用 Spring 的 DelegatingRequestProcessor 覆盖 Struts 的 RequestProcessor //第三种方式:将 Struts Action 管理委托给 Spring ...

    StrutsAPI(java)

    在請求來臨後,RequestProcessor 根據請求URI呼叫對應的Action物件,將工作交給它,並在最後由Action物件得到一個ActionForward物件, ActionServlet使用ActionForward得知將流程forward至指定的資源。 當請求到達...

    commons-beanutils-1.7.0

    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) at org.apache.struts....

    Struts开发指南之工作流程

     ActionServlet是一个FrontController,它是一个标准的Servlet,它将request转发给RequestProcessor来处理, ActionMapping是ActionConfig的子类,实质上是对struts-config.xml的一个映射,从中可以取得所有的配置...

    Struts控制器示例

    Struts的控制器主要包括: ActionServlet :核心控制器 RequestProcessor :子应用模块的处理器 Action :处理单项业务

    Hibernate+Spring+Struts扩展Struts

    简介: 我看到很多项目中,开发者实现了自己的MVC... 1、PlugIn:如果你想在application startup或shutdown的时候做一些业务逻辑的话,那就创建你自己的PlugIn类。 2、RequestProcessor:如果你想在请求被处理的过程

    J2EE电子商务系统开发从入门到精通

    2.3.2 RequestProcessor类..... 16 2.3.3 Action类..... 26 2.4 Struts视图组件类..... 30 www.j2eedve.com 制作:找不着北 第 2 页 2007-7-27 2.4.1 ActionForm类..... 30 2.4.2 ActionForward类..... 32 2.4.3 ...

    精通Struts基于MVC的Java Web设计与开发 孙卫琴 光盘

    第4章到第7章深入探讨了Struts框架的核心组件ActionServlet和RequestProcessor的实现原理,详细介绍了开发Struts应用的模型、视图和控制器的各种技术,细致的描述了Struts配置文件的每个元素的使用方法。第8章到第9...

    精通 Struts:基于 MVC 的 JavaWeb 设计与开发(PDF)

    第4章到第7章深入探讨了Struts框架的核心组件ActionServlet和RequestProcessor的实现原理,详细介绍了开发Struts应用的模型、视图和控制器的各种技术,细致的描述了Struts配置文件的每个元素的使用方法。第8章到第9...

Global site tag (gtag.js) - Google Analytics