一直用struts做了一些列项目,却没有好好研究struts的代码,不得不说是个遗憾。
这就好像学习降龙十八掌,却一直不看秘籍。难免少几掌。哈哈。
虽然struts经到version2,但是version1比较经典。后面再分析v2.
先从ActionServlet开始,ActionServlet在MVC模型中充当了控制器的角色,所有的http请求都会首先经过这里。
ActionServlet的init()是struts初期化的方法。
init()方法包含了一些列的子方法:
1.initInternal()取得MessageResources的消息资源。通过在ActionResouce.properties中定义一系列的消息,在webserver启动加载struts时候调用。
2.initOther() 子方法中判断convertNull的设置。如果用户在web.xml的配置文件中设置convertNull为ture,或者yes,或者 1,struts会将null转变成根据java的primitive objece 比如,如果你打算出入一个null进入int类型的值,如果convertNull设置成true,struts就会自动帮你转为0
3.
initServlet()注册web.xml中的servlet-mapping,一般我们会用.do来发送action,这里注册后,就会认定.do为后缀的url被发送到action。
4.initChain()如果没有chain配置,则使用默认"org/apache/struts/chain/chain-config.xml"。Struts使用Chain替换了原来传统的在RequestProcessor类中执行的HTTP请求处理
5.initModuleConfigFactory()初期化module配置工厂。
ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory(); //通过加载class名来创建工厂
ModuleConfig config = factoryObject.createModuleConfig(prefix);//
返回了
super();
this.prefix = prefix;
this.actionConfigs = new HashMap();
this.actionConfigIds = new HashMap();
this.actionConfigList = new ArrayList();
this.actionFormBeanClass = "org.apache.struts.action.ActionFormBean";
this.actionMappingClass = "org.apache.struts.action.ActionMapping";
this.actionForwardClass = "org.apache.struts.action.ActionForward";
this.configured = false;
this.controllerConfig = null;
this.exceptions = new HashMap();
this.formBeans = new HashMap();
this.forwards = new HashMap();
this.messageResources = new HashMap();
this.plugIns = new ArrayList();
这里把moduleConfig对象放置到servletContext中
Digester digester = initConfigDigester();// 初期化digester(digester是apache下面的子项目)
循环取得struts配置文件,解析
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);
6. 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();
}
这段代码,将web.xml中不同modules配置文件初始化为moduleConfig对象,struts配置文件离给出的MessageResource,PlugIns,FromBeans等都被初期化为对象,然后放入到ServletContext里。while循环内部是web.xml里ActionServlet配置中已“config/”为开头的初期化参数配置文件。while外部初期化web.xml里ActionServlet指定的config配置文件。
7.this.initModulePrefixes(this.getServletContext());
将所有生成的字符串放到Sting[]中,放入ServletContext中。
8.this.destroyConfigDigester();
将ActionServlet类的configDigester字段重置为null。
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
}
HTTP的get和post放到都会在ationServlet中被process处理。
protected void process(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// 从context中取得module配置,设置到request中
ModuleUtils.getInstance().selectModule(request, getServletContext());
// 返回选择的modules中的配置
ModuleConfig config = getModuleConfig(request);
// 通过config取得RequestProcessor对象
RequestProcessor processor = getProcessorForModule(config);
// 如果processor对象为空,从config文件中生成processor对象。
if (processor == null) {
processor = getRequestProcessor(config);
}
// 所有的get,post对象最后都会通过RequestProcessor来处理。
// RequestProcessor通过ModuleConfig中的信息,客户的访问路径,取得配置文件中的Action,Forward,actionMapping,ActionForm等信息。
processor.process(request, response);
}
这就好像学习降龙十八掌,却一直不看秘籍。难免少几掌。哈哈。
虽然struts经到version2,但是version1比较经典。后面再分析v2.
先从ActionServlet开始,ActionServlet在MVC模型中充当了控制器的角色,所有的http请求都会首先经过这里。
ActionServlet的init()是struts初期化的方法。
init()方法包含了一些列的子方法:
1.initInternal()取得MessageResources的消息资源。通过在ActionResouce.properties中定义一系列的消息,在webserver启动加载struts时候调用。
2.initOther() 子方法中判断convertNull的设置。如果用户在web.xml的配置文件中设置convertNull为ture,或者yes,或者 1,struts会将null转变成根据java的primitive objece 比如,如果你打算出入一个null进入int类型的值,如果convertNull设置成true,struts就会自动帮你转为0
3.
initServlet()注册web.xml中的servlet-mapping,一般我们会用.do来发送action,这里注册后,就会认定.do为后缀的url被发送到action。
4.initChain()如果没有chain配置,则使用默认"org/apache/struts/chain/chain-config.xml"。Struts使用Chain替换了原来传统的在RequestProcessor类中执行的HTTP请求处理
5.initModuleConfigFactory()初期化module配置工厂。
ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory(); //通过加载class名来创建工厂
ModuleConfig config = factoryObject.createModuleConfig(prefix);//
返回了
super();
this.prefix = prefix;
this.actionConfigs = new HashMap();
this.actionConfigIds = new HashMap();
this.actionConfigList = new ArrayList();
this.actionFormBeanClass = "org.apache.struts.action.ActionFormBean";
this.actionMappingClass = "org.apache.struts.action.ActionMapping";
this.actionForwardClass = "org.apache.struts.action.ActionForward";
this.configured = false;
this.controllerConfig = null;
this.exceptions = new HashMap();
this.formBeans = new HashMap();
this.forwards = new HashMap();
this.messageResources = new HashMap();
this.plugIns = new ArrayList();
这里把moduleConfig对象放置到servletContext中
Digester digester = initConfigDigester();// 初期化digester(digester是apache下面的子项目)
循环取得struts配置文件,解析
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);
6. 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();
}
这段代码,将web.xml中不同modules配置文件初始化为moduleConfig对象,struts配置文件离给出的MessageResource,PlugIns,FromBeans等都被初期化为对象,然后放入到ServletContext里。while循环内部是web.xml里ActionServlet配置中已“config/”为开头的初期化参数配置文件。while外部初期化web.xml里ActionServlet指定的config配置文件。
7.this.initModulePrefixes(this.getServletContext());
将所有生成的字符串放到Sting[]中,放入ServletContext中。
8.this.destroyConfigDigester();
将ActionServlet类的configDigester字段重置为null。
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
}
HTTP的get和post放到都会在ationServlet中被process处理。
protected void process(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// 从context中取得module配置,设置到request中
ModuleUtils.getInstance().selectModule(request, getServletContext());
// 返回选择的modules中的配置
ModuleConfig config = getModuleConfig(request);
// 通过config取得RequestProcessor对象
RequestProcessor processor = getProcessorForModule(config);
// 如果processor对象为空,从config文件中生成processor对象。
if (processor == null) {
processor = getRequestProcessor(config);
}
// 所有的get,post对象最后都会通过RequestProcessor来处理。
// RequestProcessor通过ModuleConfig中的信息,客户的访问路径,取得配置文件中的Action,Forward,actionMapping,ActionForm等信息。
processor.process(request, response);
}
本文详细解析了Struts框架的初始化过程,特别是ActionServlet作为核心控制器的作用及其init方法中的关键步骤,包括消息资源的获取、配置文件的解析及模块配置的初始化。

被折叠的 条评论
为什么被折叠?



