<o:p> </o:p>
Struts 源码的切片学习(三)
—— ActionServlet 的初始化细节
<o:p>
</o:p>
<o:p> </o:p>
Author : zhyiwww<o:p>
E-Mail : zhyiwww@163.com<o:p>
Date : 2007-1-10<o:p>
转载请注明出处 www.BlogJava.net/zhyiwww<o:p>
(copyright by @ zhangyi)<o:p>
<o:p> </o:p>
<o:p> </o:p>
在 ActionServlet 初始化的过程中,有一个函数 initServlet() 用来初始化此 ActionServlet, 主要是初始化其 url, 也就是到底此 ActionServlet 接收和处理那些 url 。 <o:p> </o:p>
<o:p> </o:p>
我们先看一下代码:
protected void initServlet() throws ServletException {<o:p>
<o:p> </o:p>
// Remember our servlet name<o:p>
this.servletName = getServletConfig().getServletName();<o:p>
<o:p> </o:p>
// Prepare a Digester to scan the web application deployment descriptor<o:p>
Digester digester = new Digester();<o:p>
digester.push(this);<o:p>
digester.setNamespaceAware(true);<o:p>
digester.setValidating(false);<o:p>
<o:p> </o:p>
// Register our local copy of the DTDs that we can find<o:p>
for (int i = 0; i < registrations.length; i += 2) {<o:p>
URL url = this.getClass().getResource(registrations[i+1]);<o:p>
if (url != null) {<o:p>
digester.register(registrations[i], url.toString());<o:p>
}<o:p>
}<o:p>
/********************************************<o:p>
上面的紫色的代码是把当前的 ActionServlet 实例放入到解析堆栈中。这一步很重要。 <o:p> </o:p>
正是 actionServlet 放入了堆栈,下面的调用所取得配置参数才正是 actionSerlvet 的配置参数,而不是其他的 servlet 的配置参数。 <o:p> </o:p>
这个地方是一个难点。 <o:p> </o:p>
********************************************/<o:p>
<o:p> </o:p>
<o:p> </o:p>
// Configure the processing rules that we need<o:p>
digester.addCallMethod("web-app/servlet-mapping",<o:p>
"addServletMapping", 2);<o:p>
digester.addCallParam("web-app/servlet-mapping/servlet-name", 0);<o:p>
digester.addCallParam("web-app/servlet-mapping/url-pattern", 1);<o:p>
<o:p> </o:p>
/************************************************************<o:p>
上面的代码执行的时候, digester 就会调用 actionServlet 中的 addServletMapping() 方法,并传进去两个参数。 <o:p> </o:p>
在这个方法里面,就初始化了 servlet 的 url, ,也就是那种后缀的 ur 要由 actionServlet 来接收和处理。 <o:p> </o:p>
例如: *.do 还是 /action/*.do 等。 <o:p> </o:p>
这个 servlet 的 url 保存在 actionServlet 的 servletMapping 参数里面。 <o:p> </o:p>
************************************************************/<o:p>
<o:p> </o:p>
// Process the web application deployment descriptor<o:p>
if (log.isDebugEnabled()) {<o:p>
log.debug("Scanning web.xml for controller servlet mapping");<o:p>
}<o:p>
<o:p> </o:p>
InputStream input =<o:p>
getServletContext().getResourceAsStream("/WEB-INF/web.xml");<o:p>
<o:p> </o:p>
if (input == null) {<o:p>
log.error(internal.getMessage("configWebXml"));<o:p>
throw new ServletException(internal.getMessage("configWebXml"));<o:p>
}<o:p>
<o:p> </o:p>
try {<o:p>
digester.parse(input);<o:p>
<o:p> </o:p>
} catch (IOException e) {<o:p>
log.error(internal.getMessage("configWebXml"), e);<o:p>
throw new ServletException(e);<o:p>
<o:p> </o:p>
} catch (SAXException e) {<o:p>
log.error(internal.getMessage("configWebXml"), e);<o:p>
throw new ServletException(e);<o:p>
<o:p> </o:p>
} finally {<o:p>
try {<o:p>
input.close();<o:p>
} catch (IOException e) {<o:p>
log.error(internal.getMessage("configWebXml"), e);<o:p>
throw new ServletException(e);<o:p>
}<o:p>
}<o:p>
<o:p> </o:p>
// Record a servlet context attribute (if appropriate)<o:p>
if (log.isDebugEnabled()) {<o:p>
log.debug("Mapping for servlet '" + servletName + "' = '" +<o:p>
servletMapping + "'");<o:p>
}<o:p>
<o:p> </o:p>
if (servletMapping != null) {<o:p>
getServletContext().setAttribute(Globals.SERVLET_KEY, servletMapping);<o:p>
}<o:p>
<o:p> </o:p>
}<o:p>
<o:p> </o:p>
上面的过程就是 initServlet 的初始化过程,其中的难点就是对 digester.push() 的理解。
附:public void addServletMapping(String servletName, String urlPattern) {
if (log.isDebugEnabled()) {
log.debug("Process servletName=" + servletName +
", urlPattern=" + urlPattern);
}
if (servletName == null) {
return;
}
if (servletName.equals(this.servletName)) {
this.servletMapping = urlPattern;
}
}
<o:p>
![96549.html](https://i-blog.csdnimg.cn/blog_migrate/80eb711a7e9f0ef61485dc27cbd4be7b.jpeg)