struts1.1中ActionServlet对http请求的处理过程

ActionServlet通过调用process方法处理http请求,具体声明如下:

protected void process(HttpServletRequest request,HttpServletResponse response)

        throws IOException, ServletException

该方法主要调用了两个方法,RequestUtils.selectModule(request, getServletContext());

getRequestProcessor(getModuleConfig(request)).process(request, response);

下面分别介绍:

1. RequestUtils.selectModule(request, getServletContext())

该方法主要是通过截取requestServletPath的路径,并在ServletContext中查找所有已经配置的子模块(即:prefix不是空字符串””的所有的ModuleConfigImpl)中的prefix是否有与截取的ServletPath路径相等的,如果存在则返回该子模块的prefix,否则返回默认模块的prefix,即空字符串””;代码如下:

              String prefix = ""; // Initialize prefix before we try lookup

        String prefixes[] = getModulePrefixes(context); // Get all other possible prefixes

        int lastSlash = 0; // Initialize before loop

        /* 如果当前请求的ServletPath匹配到某个特定的前缀,则会选定该前缀模块;

         * 否则选定默认模块 */

        while (prefix.equals("") && ((lastSlash = matchPath.lastIndexOf("/")) > 0)) {

            // We may be in a non-default module.  Try to get it's prefix.

            matchPath = matchPath.substring(0, lastSlash);

            // Match against the list of module prefixes

            for (int i = 0; i < prefixes.length; i++) {

                if (matchPath.equals(prefixes[i])) {

                    prefix = prefixes[i];

                    break;

                }

            }

        }

        return prefix;

确定ModuleConfigImpl对象后,将该对象以及相应的消息资源对象放进request的属性中,代码如下:

              ModuleConfig config = (ModuleConfig) context.getAttribute(Globals.MODULE_KEY + prefix);

        if (config != null) {

            request.setAttribute(Globals.MODULE_KEY, config);

        } else {

            request.removeAttribute(Globals.MODULE_KEY);

        }

        MessageResources resources =

            (MessageResources) context.getAttribute(Globals.MESSAGES_KEY + prefix);

        if (resources != null) {

            request.setAttribute(Globals.MESSAGES_KEY, resources);

        } else {

            request.removeAttribute(Globals.MESSAGES_KEY);

        }

 

2. getRequestProcessor(getModuleConfig(request)).process(request, response)

该方法主要是调用了RequestProcessor类的

public void process(HttpServletRequest request, HttpServletResponse response)

        throws IOException, ServletException 方法,该方法的主要流程如下:

2.1 request = processMultipart(request);判断参数request是否是multipart,如果是则将request封装成MultipartRequestWrapper类,否则直接返回request

2.2 String path = processPath(request, response);获取当前请求的path,以便用来匹配当前ModuleConfigImplaction标记;

2.3 processLocale(request, response);requestLocale放进Session变量中;

2.4 processContent(request, response);responsecontent type 设置为配置文件中controller标记中的contentType属性的设置值,默认为”text/html”

2.5 processNoCache(request, response);如果controller标记的nocache属性设置为true,则设置response不缓存,默认设置为false

2.6 ActionMapping mapping = processMapping(request, response, path);查找该ModuleConfigImpl对象中对应该pathActionMapping对象,如果找不到则查找默认的ActionMapping,即unknown属性为trueActionMapping

2.7 processRoles(request, response, mapping)JAAS相关

2.8 ActionForm form = processActionForm(request, response, mapping);该方法主要根据mappingattribute属性或name属性查找requestsession(根据mappingscope属性决定)的属性中是否已经存在该ActionForm对象,如果存在则直接返回,否则根据mappingattributename属性值查找ModuleConfigImplFormBeanConfig对象,然后根据FormBeanConfig对象的type属性值动态创建一个ActionForm实例;

2.9 processPopulate(request, response, form, mapping);此步主要是根据表单的输入参数对ActionForm进行属性赋值,其中ActionForm的属性名称与表单参数名称截去ActionMappingprefixsuffix的属性值标示的前缀和后缀后的字符串一致;利用表单进行上传(requestcontent type属性以multipart/form-data开头)的情况以后详述;

2.10 processValidate(request, response, form, mapping);该方法主要通过调用ActionFormform.validate(mapping, request)方法,如果返回结果不为null,则将跳转到mappinginput属性标记的uri,如果此时未标明input属性则出错;执行ActionFormvalidate方法的前提是mappingvalidate属性为true,否则将不检验;

2.11 processForward(request, response, mapping);如果mappingforward属性不为null,则将请求和相应转发到mappingforward属性表示的uri处理,并返回;

2.12 processInclude(request, response, mapping); 如果mappinginclude属性不为null,则将请求和相应转发到mappinginclude属性表示的uri处理,并返回;

2.13 Action action = processActionCreate(request, response, mapping);根据mappingtype属性值在一个HashMap变量中查找相应的对象实例,如果不存在则动态创建并存入HashMap变量以提高以后的读取效率;

2.14 ActionForward forward =processActionPerform(       request, response, action, form, mapping);

该方法主要是执行actionexecute方法,并将该方法返回的ActionForward对象获取下一步的forward

2.15 processForwardConfig(request, response, forward);该方法主要是将请求和响应转发给forwardpath属性标示的uri处理;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值