Struts1的处理流程

本文从收到一个请求开始讲述,忽略之前的filter等工作.

处理工作的主要承担者为RequestProcessor

1.处理请求的url. RequestProcessor.processPath(request,response)
  String path = processPath(request, response);

     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); }

  从request中获取请求的url.path = request.getPathInfo();

  在对获取的path进行处理:

         path = path.substring(prefix.length());
2 int slash = path.lastIndexOf("/");
3 int period = path.lastIndexOf(".");
if ((period >= 0) && (period > slash)) {
path = path.substring(0, period);
}
return (path);

2.根据获取的url-path来构建ActionMapping

  ActionMapping mapping = processMapping(request, response, path);  

  

    protected ActionMapping processMapping(HttpServletRequest request,
HttpServletResponse response,
String path)
throws IOException { // Is there a mapping for this path?
ActionMapping mapping = (ActionMapping)
moduleConfig.findActionConfig(path); // 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);
}
} // No mapping can be found to process this request
String msg = getInternal().getMessage("processInvalid");
log.error(msg + " " + path);
response.sendError(HttpServletResponse.SC_NOT_FOUND, msg); return null;
}

  这个过程主要是从struts-config.xml中读取<action-mapping>结点的action信息,将所有的<action>结点保存到ActionMapping中.

3.获取ActionForm,

   ActionForm form = processActionForm(request, response, mapping);

  上一步通过url已经定位到了相应的action,然后通过 String name = mapping.getName(); 获取actionform(该项在<form-beans>中获取),同时设置该action的作用域(request/session默认session).再将创建号的actionform放到request/session中.

4.从actionform中获取相应的值,完成相应数据的赋值,这其中包括类型的转换,使用了第三方的工具类BeanUtils.

5.创建相应的action

  protected Action processActionCreate(HttpServletRequest request,HttpServletResponse response,ActionMapping mapping)

  actionform信息存在于actionmapping中。首先根据action类的完整名称(<action>标签下面的type),如果已经存在直接返回;否则再使用反射机制创建action。

6.最终执行action的execute方法。

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

  从中执行action的execute方法,返回actinforward,再根据返回的actionforward来实现转发/转向。

具体的处理流程如下图:

Struts1的处理流程

附件一 struts-config.xml

<struts-config>
<form-beans>
<form-bean name="loginactionform" type="com.volshell.actionform.LoginActionForm"/>
</form-beans>
<action-mappings>
<action path="/login"
name="loginactionform"
type="com.volshell.action.LoginAction"
scope="request">
<forward name="error" path="/login_fail.jsp"></forward>
<forward name="success" path="/login_success.jsp"></forward>
</action>
</action-mappings>
</struts-config>
上一篇:Android 工程报错解决 Unable to resolve target 'android-17'


下一篇:基于zepto的H5/移动端tab切换触摸拖动加载更多数据