// Identify the path component we will use to select a mapping String path = processPath(request, response); 截取URL,若为null,直接返回。
2.根据path,获取匹配的ActionMapping,每个Action对应一个ActionMapping a.// Identify the mapping for this request ActionMapping mapping = processMapping(request, response, path); b.// Is there a mapping for this path? ActionMapping mapping = (ActionMapping) moduleConfig.findActionConfig(path); moduleConfig是ModuleConfig的对象,ModuleConfig是一个接口,定义了对struts-config.xml文件的操作,具体由ModuleConfigImpl实现,对findActionConfig()方法的实现为: ActionConfig config = (ActionConfig) actionConfigs.get(path); actionConfigs是一个HashMap。当ActionServlet第一次实例化得时候,就读取struts-config.xml,所有的配置都读入到HsahMap,并保存到ServletContext的范围内,HashMap的“键值对”中key为path,value为ActionMapping 的实例。 ActionMapping继承ActionConfig,则根据path 得到对应Action的配置信息。
3.处理ActionForm
a.// Process any ActionForm bean related to this request ActionForm form = processActionForm(request, response, mapping);
b.// Create (if necessary) a form bean to use ActionForm instance = RequestUtils.createActionForm (request, mapping, moduleConfig, servlet);
c.// Look up the form bean configuration information to use String name = mapping.getName(); FormBeanConfig config = moduleConfig.findFormBeanConfig(name); moduleConfig就是配置文件的一套操作的接口。并且FormBeanConfig也是保存在一个HashMap中,key为name,value为FormBeanConfig。返回配置信息。若不存在直接返回,因为不配置ActionForm是正确的。
e.// Validate any fields of the ActionForm bean, if applicable 进行一定得表单验证,调用ActionForm的public ActionErrors validate()方法,进行一定得验证,此方法可以覆写。
4.Action // Create or acquire the Action instance to process this request Action action = processActionCreate(request, response, mapping); 对于同一个Action,是单实例的模式,切记。 ActionForward action.execute(mapping, form, request, response) 执行execute方法,返回ActionForward。
/** * Returns the RequestProcessor for the given module or null if one does not exist. This method will not create a RequestProcessor. * @param config The ModuleConfig. */ private RequestProcessor getProcessorForModule(ModuleConfig config) { String key = Globals.REQUEST_PROCESSOR_KEY + config.getPrefix(); return (RequestProcessor)getServletContext().getAttribute(key); }