在web.xml中配置有
当页面请求到来时, ActionServlet进行初始化, 执行该Servlet的init()方法,
完成初始化操作。
之后, 调用该Servlet的doGet 、doPost方法,
继续方法调用
其中获取processor方法为:
下面简要分析一下上面提及的processor.process(request, response)
processForwardConfig(request, response, forward) 又会调用RequestDispatcher的forward等方法, 完成
此次转发。
关于action的获取方法 processActionCreate
可见action是单例的
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
当页面请求到来时, ActionServlet进行初始化, 执行该Servlet的init()方法,
initModuleMessageResources(moduleConfig);
initModulePlugIns(moduleConfig);
initModuleFormBeans(moduleConfig);
initModuleForwards(moduleConfig);
initModuleExceptionConfigs(moduleConfig);
initModuleActions(moduleConfig);
完成初始化操作。
之后, 调用该Servlet的doGet 、doPost方法,
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
}
.....
继续方法调用
processor.process(request, response);
其中获取processor方法为:
/**
* <p>Returns the RequestProcessor for the given module or null if one
* does not exist. This method will not create a RequestProcessor.</p>
*
* @param config The ModuleConfig.
* @return The <code>RequestProcessor</code> for the given module, or
* <code>null</code> if one does not exist.
*/
private RequestProcessor getProcessorForModule(ModuleConfig config) {
String key = Globals.REQUEST_PROCESSOR_KEY + config.getPrefix();
return (RequestProcessor) getServletContext().getAttribute(key);
}
下面简要分析一下上面提及的processor.process(request, response)
public void process(HttpServletRequest request, HttpServletResponse response) {
// 处理文件上传
request = processMultipart(request);
// 得到ActionMapping
ActionMapping mapping = processMapping(request, response, path);
// 得到ActionForm
ActionForm form = processActionForm(request, response, mapping);
// 得到action
Action action = processActionCreate(request, response, mapping);
// 得到forward
ActionForward forward =
processActionPerform(request, response, action, form, mapping);
....
processForwardConfig(request, response, forward);
}
processForwardConfig(request, response, forward) 又会调用RequestDispatcher的forward等方法, 完成
此次转发。
关于action的获取方法 processActionCreate
protected Action processActionCreate(HttpServletRequest request,
HttpServletResponse response, ActionMapping mapping)
throws IOException {
// Acquire the Action instance we will be using (if there is one)
String className = mapping.getType();
if (log.isDebugEnabled()) {
log.debug(" Looking for Action instance for class " + className);
}
// If there were a mapping property indicating whether
// an Action were a singleton or not ([true]),
// could we just instantiate and return a new instance here?
Action instance;
synchronized (actions) {
// Return any existing Action instance of this class
instance = (Action) actions.get(className);
可见action是单例的