- 浏览: 273958 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
u014704612:
你好,请问这个实现由需要什么条件吗,我复制进去达不到全屏的效果 ...
JavaScript控制页面全屏,并且禁止使用ALT+TAB键切换 -
shengtu:
能不能兼容其它浏览器呢?
JavaScript控制页面全屏,并且禁止使用ALT+TAB键切换 -
leongfeng:
xd_fjx 写道如果使用那个HibernateCallbac ...
Spring中使用HibernateCallback -
soft5200:
jQuery.getJSON('http://localhos ...
使用JQuery结合HIghcharts实现从后台获取JSON实时刷新图表 -
monica617:
讲的还可以字体和格式再设置为易读模式就更好了
Struts1.3.x中ActionServlet源码分析之执行
- ActionServlet中的常量定义
//公共的日志实例
protected static Log log = LogFactory.getLog(ActionServlet.class);
//默认的配置文件路径
protected String config = "/WEB-INF/struts-config.xml";
//默认的处理流程配置文件
protected String chainConfig = "org/apache/struts/chain/chain-config.xml";
//未知
protected Digester configDigester = null;
//convertNull若为true,则struts中Java包装类的初始值为null
protected boolean convertNull = false;
//未知
protected MessageResources internal = null;
//默认的资源文件
protected String internalName = "org.apache.struts.action.ActionResources";
//用来验证配置文件格式的dtd文件
protected String[] registrations =
{
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN",
"/org/apache/struts/resources/struts-config_1_0.dtd",
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN",
"/org/apache/struts/resources/struts-config_1_1.dtd",
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN",
"/org/apache/struts/resources/struts-config_1_2.dtd",
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN",
"/org/apache/struts/resources/struts-config_1_3.dtd",
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN",
"/org/apache/struts/resources/web-app_2_3.dtd"
};
//未知
protected String servletMapping = null; // :FIXME: - multiples?
//未知
protected String servletName = null;
2. ActionServlet中init方法的执行流程
public void init() throws ServletException {
final String configPrefix = "config/";
final int configPrefixLength = configPrefix.length() - 1;
try {
//①内部资源文件的初始化
initInternal();
//②从web.xml文件中加载ActionServlet的初始化参数:config\convetrNull
initOther();
//③从web.xml文件中加载ActionServlet的初始化参数:servlet-name,加载DTD文件并把其放入HashMap缓存,读取并解析web.xml的内容
initServlet();
//④读取web.xml中命令链文件初始值chainConfig
initChain();
//把servlet对象存储到servletContext中,属性名为Globals.ACTION_SERVLET_KEY
getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);
//⑤调用 initModuleConfigFactory(); 和 initModuleConfig("", config); 创建ModuleConfig 对象。Struts中的MessageResource、PlugIn、数据源等,都是通过ModuleConfig来实现的
initModuleConfigFactory();
// Initialize modules as needed
ModuleConfig moduleConfig = initModuleConfig("", config);
//⑥用户资源文件的初始化 initModuleMessageResources(moduleConfig);
initModuleMessageResources(moduleConfig);
//⑦用户插件的初始化 initModulePlugIns(moduleConfig);
initModulePlugIns(moduleConfig);
//⑧ 把struts配置文件中的其他配置存储到servletContext中,包括
initModuleFormBeans(moduleConfig);
initModuleForwards(moduleConfig);
initModuleExceptionConfigs(moduleConfig);
initModuleActions(moduleConfig);
//⑨ 调用moduleConfig.freeze();固定组件配置
/* 使ModuleConfig中的 actionConfigs/actionConfigList 、exceptions 、formBeans、forwards、messageResources、plugIns等的配置等变得不可改变 */
moduleConfig.freeze();
⑩ 解析以"config/"开头的其他struts配置文件
// 遍历web.xml中servletConfig配置的 initParameterNames
// 如发现以" config/ " 开始的parameter,则根据此值初始化其它的ModuleConfig
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();
}
// 初始化其他模块的前缀
/* 把其他模块prefixes存储到servletContext中,属性名为 Globals.MODULE_PREFIXES_KEY ( " org.apache.struts.globals.MODULE_PREFIXES " ) */
this.initModulePrefixes(this.getServletContext());
// 设置configDigester = null,释放内存
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());
}
}
3.ActionServlet中init()方法调用的各个方法详解
①内部资源文件的初始化 initInternal()方法;
// initInternal 方法中通过下面得到一个MessageResources对象 internal = MessageResources.getMessageResources(internalName); 此资源文件主要包括一些消息信息的定义,具体可参考org.apache.struts.action下的ActionResources.properties文件
②从web.xml文件中加载ActionServlet的初始化参数:config\convetrNull initOther();
protected void initOther()
throws ServletException {
String value;
//得到web.xml文件中的"config"参数
value = getServletConfig().getInitParameter("config");
if (value != null) {
config = value;
}
//获得web.xml文件中convertNull的值,如果此参数的值为true,则所有数值型类型(Integer,Double)的Java
包装类的初始值为null,而非0
value = getServletConfig().getInitParameter("convertNull");
if ("true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value)
|| "on".equalsIgnoreCase(value) || "y".equalsIgnoreCase(value)
|| "1".equalsIgnoreCase(value)) {
convertNull = true;
}
if (convertNull) {
//将所有的转换器注销掉,使所有值初始为null
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);
}
}
③initServlet();从web.xml文件中加载ActionServlet的初始化参数:servlet-name,加载DTD文件并把其放入HashMap缓存,读取并解析
web.xml的内容
protected void initServlet()
throws ServletException {
//获取当前Servlet的名称,即web.xml文件中定义的<servlet-name>
this.servletName = getServletConfig().getServletName();
Digester digester = new Digester();
// 把当前的 ActionServlet 对象放入到解析堆栈中
digester.push(this);
//需要考虑命名空间
digester.setNamespaceAware(true);
// 缺省值[false] ,解析器只是检查XML是否格式良好(well formed)
digester.setValidating(false);
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());
}
}
digester.addCallMethod("web-app/servlet-mapping", "addServletMapping", 2);
digester.addCallParam("web-app/servlet-mapping/servlet-name", 0);
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");
}
InputStream input =
getServletContext().getResourceAsStream("/WEB-INF/web.xml");
if (input == null) {
log.error(internal.getMessage("configWebXml"));
throw new ServletException(internal.getMessage("configWebXml"));
}
try {
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) {
getServletContext().setAttribute(Globals.SERVLET_KEY, servletMapping);
}
}
④读取web.xml中命令链文件初始值chainConfig initChain();
protected void initChain()
throws ServletException {
// Parse the configuration file specified by path or resource
try {
String value;
//读取web.xml文件中的chainConfig参数值,如没有chainConfig参数,则使用默认 "org/apache/struts/chain/chain-config.xml"
value = getServletConfig().getInitParameter("chainConfig");
if (value != null) {
chainConfig = value;
}
ConfigParser parser = new ConfigParser();
List urls = splitAndResolvePaths(chainConfig);
URL resource;
for (Iterator i = urls.iterator(); i.hasNext();) {
resource = (URL) i.next();
log.info("Loading chain catalog from " + resource);
// chainConfig 替换了原来传统的在 RequestProcessor 类中执行的 HTTP 请求处理
parser.parse(resource);
}
} catch (Exception e) {
log.error("Exception loading resources", e);
throw new ServletException(e);
}
}
⑤调用 initModuleConfigFactory(); 和 initModuleConfig("", config); 创建ModuleConfig 对象。Struts中的MessageResource、
PlugIn、数据源等,都是通过ModuleConfig来实现的
protected void initModuleConfigFactory() {
// 得到web.xml中"configFactory"参数,如果找不到,则使用 默认工厂
String configFactory =
getServletConfig().getInitParameter("configFactory");
if (configFactory != null) {
ModuleConfigFactory.setFactoryClass(configFactory);
}
}
protected ModuleConfig initModuleConfig(String prefix, String paths)
throws ServletException {
if (log.isDebugEnabled()) {
log.debug("Initializing module path '" + prefix
+ "' configuration from '" + paths + "'");
}
// Parse the configuration for this module
ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
ModuleConfig config = factoryObject.createModuleConfig(prefix);
// Configure the Digester instance we will use
Digester digester = initConfigDigester();
/* 循环struts配置文件(用","分开的多个struts配置文件)并解析, parseModuleConfigFile 执行之后可以 从struts-config.xml等配置文件中得到 Ⅱ 中 actionConfigs/actionConfigList 、exceptions 、formBeans、forwards、messageResources、plugIns等的配置,并把得到的所有值封装到对象ModuleConfig对象(config)中 */
List urls = splitAndResolvePaths(paths);
URL url;
for (Iterator i = urls.iterator(); i.hasNext();) {
url = (URL) i.next();
digester.push(config);
this.parseModuleConfigFile(digester, url);
}
// 把config存储到servletContext中 ,属性名为Globals.MODULE_KEY
getServletContext().setAttribute(Globals.MODULE_KEY
+ config.getPrefix(), config);
return config;
}
⑥用户资源文件的初始化 initModuleMessageResources(moduleConfig);
protected void initModuleMessageResources(ModuleConfig config)
throws ServletException {
//从moduleConfig中读取所有的资源文件
protected void initModuleMessageResources(ModuleConfig config)
throws ServletException {
MessageResourcesConfig[] mrcs = config.findMessageResourcesConfigs();
for (int i = 0; i < mrcs.length; i++) {
if ((mrcs[i].getFactory() == null)
|| (mrcs[i].getParameter() == null)) {
continue;
}
if (log.isDebugEnabled()) {
log.debug("Initializing module path '" + config.getPrefix()
+ "' message resources from '" + mrcs[i].getParameter()
+ "'");
}
String factory = mrcs[i].getFactory();
MessageResourcesFactory.setFactoryClass(factory);
MessageResourcesFactory factoryObject =
MessageResourcesFactory.createFactory();
factoryObject.setConfig(mrcs[i]);
MessageResources resources =
factoryObject.createResources(mrcs[i].getParameter());
resources.setReturnNull(mrcs[i].getNull());
resources.setEscape(mrcs[i].isEscape());
getServletContext().setAttribute(mrcs[i].getKey()
+ config.getPrefix(), resources);
}
}
MessageResourcesConfig[] mrcs = config.findMessageResourcesConfigs();
for (int i = 0; i < mrcs.length; i++) {
if ((mrcs[i].getFactory() == null)
|| (mrcs[i].getParameter() == null)) {
continue;
}
if (log.isDebugEnabled()) {
log.debug("Initializing module path '" + config.getPrefix()
+ "' message resources from '" + mrcs[i].getParameter()
+ "'");
}
String factory = mrcs[i].getFactory();
MessageResourcesFactory.setFactoryClass(factory);
MessageResourcesFactory factoryObject =
MessageResourcesFactory.createFactory();
factoryObject.setConfig(mrcs[i]);
MessageResources resources =
factoryObject.createResources(mrcs[i].getParameter());
resources.setReturnNull(mrcs[i].getNull());
resources.setEscape(mrcs[i].isEscape());
getServletContext().setAttribute(mrcs[i].getKey()
+ config.getPrefix(), resources);
}
}
⑦用户插件的初始化 initModulePlugIns(moduleConfig);
protected void initModulePlugIns(ModuleConfig config)
throws ServletException {
if (log.isDebugEnabled()) {
log.debug("Initializing module path '" + config.getPrefix()
+ "' plug ins");
}
// 从moduleConfig中读取所有的插件文件
PlugInConfig[] plugInConfigs = config.findPlugInConfigs();
PlugIn[] plugIns = new PlugIn[plugInConfigs.length];
// 把所有plugIns存储到servletContext中
getServletContext().setAttribute(Globals.PLUG_INS_KEY
+ config.getPrefix(), plugIns);
for (int i = 0; i < plugIns.length; i++) {
try {
plugIns[i] =
(PlugIn) RequestUtils.applicationInstance(plugInConfigs[i]
.getClassName());
BeanUtils.populate(plugIns[i], plugInConfigs[i].getProperties());
// Pass the current plugIn config object to the PlugIn.
// The property is set only if the plugin declares it.
// This plugin config object is needed by Tiles
try {
PropertyUtils.setProperty(plugIns[i],
"currentPlugInConfigObject", plugInConfigs[i]);
} catch (Exception e) {
;
// FIXME Whenever we fail silently, we must document a valid
// reason for doing so. Why should we fail silently if a
// property can't be set on the plugin?
/**
* Between version 1.138-1.140 cedric made these changes.
* The exceptions are caught to deal with containers
* applying strict security. This was in response to bug
* #15736
*
* Recommend that we make the currentPlugInConfigObject part
* of the PlugIn Interface if we can, Rob
*/
}
plugIns[i].init(this, config);
} catch (ServletException e) {
throw e;
} catch (Exception e) {
String errMsg =
internal.getMessage("plugIn.init",
plugInConfigs[i].getClassName());
log(errMsg, e);
throw new UnavailableException(errMsg);
}
}
}
至此,ActionServlet的初始化工作完成!
发表评论
-
Struts的一些问题总结
2010-06-30 16:09 11201.ActionForm是控制层还是显示层控件? 控 ... -
(9)LookupDispatchAction
2009-07-09 17:10 11611. 创建资源信息文件,内容如下 button.save=s ... -
(8)ForwardAction
2009-07-09 17:04 8881. 创建页面 <html:lin ... -
(7)扩展RequestProcessor类
2009-07-09 17:02 12341. 创建一个类继承RequestProces ... -
(6)将实体对象作为DynaActionForm的属性
2009-07-09 16:56 10761. 新建实体对象Student ... -
(5)将实体对象作为ActionForm的属性
2009-07-09 16:52 21081. 新建实体对象Student publi ... -
(4)DynaActionForm
2009-07-08 15:01 12061.创建页面文件 <body> ... -
(3)struts的错误提示
2009-07-08 14:43 10491. 在资源配置文件中添加错误信息键值对 ... -
(2)DiapacthAction
2009-07-08 14:25 1052DispatchAction用于分发的Action,主 ... -
(1)第一个struts应用
2009-07-08 13:51 1118开发第一个 ...
相关推荐
saaj-api-1.3.jar saaj-api-1.3.jar
其中包括xdoclet2与hibernate3.6的整合xdoclet1.2.3分别与spring3.0.5、struts1.3、struts2.1、 servlet的整合配置。其中还有相应的.xdt文件模板,其中struts2.1.xdt为自己写的。 需要xdoclet1.2.3 ...
java+servlet+commons-io-2.4.jar+commons-fileupload-1.3.jar实现文件的上传与下载
struts1.x的例子,struts2.x可以参考自己修改 1.action的写法 import java.io.*; import java.sql.*; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet....
课程目标: 使学员掌握并熟练使用框架进行企业项目开发 学员要求: 1).学员必须具备jsp/servlet技术知识 2).学员具备java基础知识 3).学员具备数据库知识以及jdbc相关知识
org.apache.struts2.servlet.interceptor org.apache.struts2.util org.apache.struts2.views org.apache.struts2.views.annotations org.apache.struts2.views.freemarker org.apache.struts2.views....
struts.action.extension The URL extension to use to determine if the request is meant for a Struts action 用URL扩展名来确定是否这个请求是被用作Struts action,其实也就是设置 action的后缀,例如login....
可以对struts进行深入的学习.个人觉得是个很不错的文档,因积分的问题不得不出卖自己的珍藏之物
Files contained in javax.servlet.jar: META-INF/MANIFEST.MF javax/servlet/http/LocalStrings.properties javax.servlet.http.HttpSessionBindingListener.class javax.servlet....
org.apache.struts2.interceptor.debugging, org.apache.struts2.interceptor.validation, org.apache.struts2.servlet.interceptor, org.apache.struts2.util, org.apache.struts2.views, org.apache.struts2....
servlet源码 servlet-api-src javax.servlet.Servlet源码
java servlet src源码包 javax.servlet.* javax.servlet.http.* javax.servlet.jsp.*
Java.Web开发JSP.Servlet.Struts.Spring.Hibernate.Ajax.rar
java web整合开发王者归来光盘代码-第16、17章 Struts2.x 概述、Struts2.x 高级应用 两章 (JSP+Servlet+Struts+Hibernate+Spring)
1.3. Struts 框架中的组件.3 1.3.1. Struts配置文件.3 1.4. ActionServlet类6 1.4.1. ActionServlet配置6 1.4.2. ActionServlet方法7 1.5. ActionMapping 类8 1.6. Action 类.8 1.6.1. Action 类的方法.9 1.7. ...
Java.Web开发 JSP Servlet. Struts Spring Hibernate Ajax
由于 Struts 的入口点是 ActionServlet ,所以得在 web.xml 中配置一下这 个 Servlet 。 2. 编写 Action 类(一般从 org.apache.struts.action.Action 类继承)。 3. 编写 ActionForm 类(一般从 org.apache.struts....
零基础学Java.Web开发:JSP.Servlet.Struts.Spring.Hibernate.Ajax(PPT).