层层递进Struts1(四)之预加载ActionServlet2013-12-07 csdn 李达Struts的执行相当于分为两个阶段,预加载阶段和执行阶段,预加载阶段是指在Tomcat启动之时就开始执 行的内容,而此时我们并未真正进入跳转逻辑,这篇博客我们来分析一下预加载阶段。配置文件还记得web.xml中关于Struts的Servlet是如何配置的吗?
<servlet-name>action</servlet-name><servlet-class>org.apache.struts.action.ActionServlet</servlet-class><init-param><param-name>config</param-name><param-value>/WEB-INF/struts-config.xml</param-value></init-param><init-param><param-name>debug</param-name><param-value>2</param-value></init-param><init-param><param-name>detail</param-name><param-value>2</param-value></init-param><load-on-startup>2</load-on-startup></servlet><!-- Standard Action Servlet Mapping --><servlet-mapping><servlet-name>action</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping>
其中<load-on-startup>2</load-on-startup>指的就是在 Tomcat启动之时即执行,实际只要数字大于0,意思就是Tomcat启动即执行,为了进入调试,我们把这里改为0 。<load-on-startup>0</load-on-startup> 主函数init()经过调 试发现,当执行完此函数,Struts的预加载阶段即结束,所以我们主要来看这个函数即可。
/*** <p>Initialize this servlet.Most of the processing has been factored into* support methods so that you can override particular functionality at a* fairly granular level.</p>** @exception ServletException if we cannot configure ourselves correctly*/public void init() throws ServletException {// Wraps the entire initialization in a try/catch to better handle// unexpected exceptions and errors to provide better feedback// to the developertry {initInternal();initOther();initServlet();getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);initModuleConfigFactory();// Initialize modules as neededModuleConfig moduleConfig = initModuleConfig("", config);initModuleMessageResources(moduleConfig);initModuleDataSources(moduleConfig);initModulePlugIns(moduleConfig);moduleConfig.freeze();Enumeration names = getServletConfig().getInitParameterNames();while (names.hasMoreElements()) {String name = (String) names.nextElement();if (!name.startsWith("config/")) {continue;}String prefix = name.substring(6);moduleConfig = initModuleConfig(prefix, getServletConfig().getInitParameter(name));initModuleMessageResources(moduleConfig);initModuleDataSources(moduleConfig);initModulePlugIns(moduleConfig);moduleConfig.freeze();}this.initModulePrefixes(this.getServletContext());this.destroyConfigDigester();} catch (UnavailableException ex) {throw ex;} catch (Throwable t) {// The follow error message is not retrieved from internal message// resources as they may not have been able to have been // initializedlog.error("Unable to initialize Struts ActionServlet due to an "+ "unexpected exception or error thrown, so marking the "+ "servlet as unavailable.Most likely, this is due to an "+ "incorrect or missing library dependency.", t);throw new UnavailableException(t.getMessage());}}