struts1源码学习1
来源:互联网 发布:黄绮珊高音知乎 编辑:程序博客网 时间:2024/05/12 04:31
ActionServlet初始化方法init
public class ActionServlet extends HttpServlet//servlet初始化 public void init() throws ServletException { final String configPrefix = "config/"; final int configPrefixLength = configPrefix.length() - 1; // Wraps the entire initialization in a try/catch to better handle // unexpected exceptions and errors to provide better feedback // to the developer try { initInternal(); initOther(); initServlet(); initChain(); getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this); initModuleConfigFactory(); // Initialize modules as needed ModuleConfig moduleConfig = initModuleConfig("", config); initModuleMessageResources(moduleConfig); initModulePlugIns(moduleConfig); initModuleFormBeans(moduleConfig); initModuleForwards(moduleConfig); initModuleExceptionConfigs(moduleConfig); initModuleActions(moduleConfig); moduleConfig.freeze(); Enumeration names = getServletConfig().getInitParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); if (!name.startsWith(configPrefix)) { continue; } String prefix = name.substring(configPrefixLength); moduleConfig = initModuleConfig(prefix, getServletConfig().getInitParameter(name)); initModuleMessageResources(moduleConfig); initModulePlugIns(moduleConfig); initModuleFormBeans(moduleConfig); initModuleForwards(moduleConfig); initModuleExceptionConfigs(moduleConfig); initModuleActions(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 // initialized log.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()); } }
1、initInternal
protected void initInternal() throws ServletException { try {//internalName是org.apache.struts.action.ActionResources internal = MessageResources.getMessageResources(internalName); } catch (MissingResourceException e) { log.error("Cannot load internal resources from '" + internalName + "'", e); throw new UnavailableException( "Cannot load internal resources from '" + internalName + "'"); } }
看一下org.apache.struts.action.ActionResources,提示信息数据,我这看到的是英文版和日文版
该方法主要就是加载提示信息数据,加载完成后保存到internal这个属性里
2、initOthers
protected void initOther() throws ServletException { String value;//读取actionservlet的配置文件,config默认是/WEB-INF/struts-config.xml value = getServletConfig().getInitParameter("config"); if (value != null) { config = value; } // Backwards compatibility for form beans of Java wrapper classes // Set to true for strict Struts 1.0 compatibility//从web.xml读取数据转换器开关 value = getServletConfig().getInitParameter("convertNull"); if ("true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value) || "on".equalsIgnoreCase(value) || "y".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value)) { convertNull = true; } if (convertNull) { ConvertUtils.deregister();//几种类型转换 ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class); ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class); ConvertUtils.register(new BooleanConverter(null), Boolean.class); ConvertUtils.register(new ByteConverter(null), Byte.class); ConvertUtils.register(new CharacterConverter(null), Character.class); ConvertUtils.register(new DoubleConverter(null), Double.class); ConvertUtils.register(new FloatConverter(null), Float.class); ConvertUtils.register(new IntegerConverter(null), Integer.class); ConvertUtils.register(new LongConverter(null), Long.class); ConvertUtils.register(new ShortConverter(null), Short.class); } }3、initServlet
初始化actionservlet参数
protected void initServlet() throws ServletException { // Remember our servlet name//通过servletconfig获得该servlet名称 this.servletName = getServletConfig().getServletName(); // Prepare a Digester to scan the web application deployment descriptor//apache的一个xml解析工具 Digester digester = new Digester();//把当前对象(ActinServlet)放入digester中 digester.push(this); digester.setNamespaceAware(true); digester.setValidating(false); // Register our local copy of the DTDs that we can find//解析了几个dtd,struts1自带的 for (int i = 0; i < registrations.length; i += 2) { URL url = this.getClass().getResource(registrations[i + 1]); if (url != null) { digester.register(registrations[i], url.toString()); } } // Configure the processing rules that we need//类似xpath,得到web-app/servlet-mapping下的参数,并传给ActinServletaddServletMapping方法执行</span> digester.addCallMethod("web-app/servlet-mapping", "addServletMapping", 2);//将web-app/servlet-mapping/servlet-name的值,作为方法的第一个参数 digester.addCallParam("web-app/servlet-mapping/servlet-name", 0);//将web-app/servlet-mapping/url-pattern的值,作为方法的第二个参数 digester.addCallParam("web-app/servlet-mapping/url-pattern", 1); // Process the web application deployment descriptor if (log.isDebugEnabled()) { log.debug("Scanning web.xml for controller servlet mapping"); }//读取/WEB-INF/web.xml(写死的 InputStream input = getServletContext().getResourceAsStream("/WEB-INF/web.xml"); if (input == null) { log.error(internal.getMessage("configWebXml")); throw new ServletException(internal.getMessage("configWebXml")); } try {//解析xml,随后执行了前面的方法addServletMapping digester.parse(input); } catch (IOException e) { log.error(internal.getMessage("configWebXml"), e); throw new ServletException(e); } catch (SAXException e) { log.error(internal.getMessage("configWebXml"), e); throw new ServletException(e); } finally { try { input.close(); } catch (IOException e) { log.error(internal.getMessage("configWebXml"), e); throw new ServletException(e); } } // Record a servlet context attribute (if appropriate) if (log.isDebugEnabled()) { log.debug("Mapping for servlet '" + servletName + "' = '" + servletMapping + "'"); } if (servletMapping != null) {//把struts1配置的url匹配字符串放入servletcontext中 getServletContext().setAttribute(Globals.SERVLET_KEY, servletMapping); } }
0 0
- struts1源码学习1
- struts1.3源码学习
- Struts1 - 源码学习 - ActionServlet
- struts1源码学习3
- struts1源码学习4
- Struts1源码查看(1)
- Struts1.3源码分析(1)
- struts1源码阅读(1)
- struts1源码阅读(1)
- struts1学习之--1、struts1相关理论
- struts1学习笔记1
- struts1源码学习2(initChain)
- Struts1.3源码学习笔记(一)——ActionServlet(1)
- Struts1.X 学习笔记1
- struts1学习
- Struts1学习
- struts1学习
- Struts1 学习
- rabbitMQ文档代码样例
- //比较实用的一段代码,单击鼠标并在单击位置创建物体的功能
- struts2和freemarker集成实例
- MTK6592平台 touchpanel驱动设备加载(三)
- 执行事务
- struts1源码学习1
- 关于C,看过的一些书
- Java appendReplacement的理解
- Comment on "Apprenticeship Patterns: Guidance for the Aspiring Software Craftsman"
- iOS开发UI篇—CAlayer层的属性
- 两数组比较
- 视图
- div层调整zindex属性无效原因分析及解决方法
- 学习笔记 Java_毕向东_数组_面向对象 2014.7.31