---
title: "Struts1 学习笔记1"
author: "iwfy"
date: "2008-11-05T15:12:55Z"
tags: ["Apache", "Servlet", "Struts", "Web", "XML"]
slug: "struts1-1"
draft: false
lastmod: "2008-11-05T15:12:55Z"
id: 262696
url: "https://iwfy.iteye.com/blog/262696"
---

学会了使用Struts1，也大概明白了它的主要流程，为了学习的深入些，在这里记录下读Struts1源码的过程，加深理解！ 使用Struts的入口在web.xml文件，通过配置Servlet和Servlet-mapping标签： action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml 2 action \*.do 当浏览器接收一个用户请求，如login.do, web服务器就把这个请求交给org.apache.struts.action.ActionServlet来处理，这个ActionServlet从HttpServlet类继承，它的几个主要的属性： protected String config = "/WEB-INF/struts-config.xml"; protected boolean convertNull = false; protected String servletMapping = null; protected String servletName = null; 生命周期跟普通的Servlet一样，这里配置了由web服务器开启时自动加载实例化，实例化过程： public void init() throws ServletException { final String configPrefix = "config/"; final int configPrefixLength = configPrefix.length() - 1; try { initInternal(); //关于国际化的一些准备工作 initOther(); //读取web.xml里此Servlet初始化参数给属性config并判断有没有配置类型转换器 initServlet(); //得到ServletName initChain(); //读取chainConfig //Globals.ACTION\_SERVLET\_KEY=org.apache.struts.action.ACTION\_SERVLET getServletContext().setAttribute(Globals.ACTION\_SERVLET\_KEY, this); initModuleConfigFactory(); //实例化ModuleConfigFactory // Initialize modules as needed //读取config里所以配置文件内容，保存到moduleConfig，把moduleConfig保存到context中 ModuleConfig moduleConfig = initModuleConfig("", config); //下面都是对Struts配置文件各个配置项的读取操作 initModuleMessageResources(moduleConfig); initModulePlugIns(moduleConfig); initModuleFormBeans(moduleConfig); initModuleForwards(moduleConfig); initModuleExceptionConfigs(moduleConfig); initModuleActions(moduleConfig); moduleConfig.freeze(); //把基本的配置信息保存到HashMap里 Enumeration names = getServletConfig().getInitParameterNames(); //找到所有的config配置信息 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(); } //context.setAttribute(Globals.MODULE\_PREFIXES\_KEY, prefixes) //"org.apache.struts.globals.MODULE\_PREFIXES" this.initModulePrefixes(this.getServletContext()); this.destroyConfigDigester(); } catch (UnavailableException ex) { throw ex; } catch (Throwable t) { log.error(".........", t); throw new UnavailableException(t.getMessage()); } } 从上面的init()大概可以看出，在初始化时完成了配置信息的提取，并保存到相应的HashMap中，key就是标签名form-bean,action-mapping,forward等等，value就是它们的设置内容type,class等等。主要实体类ModuleConfig。 当客户请求到达ActionServlet后,调用相应的doxxx(request, response),直接传递参数给process(request, response)方法，在这个方法里，首先分析request的地址串，得到相应的moduleConfig并放到request中，查找对应的处理类RequestProcessor,调用其process(request, response)方法，这个是核心方法，它完成数据收集，分发任务，转向等操作。