第一次写这种技术性的东西,可能写得比较凌乱不易阅读,请大家多多见谅。
首先浅谈下struts的优点,主要表现在两个方面,封装的struts标签库和页面导航。
才有struts标签库能够大大的提高开发效率与提高代码复用。struts在页面跳转方面并不直接转发一个实际的页面而是转发给在strus-config.xml中已经配置的对象。这意味着,在不改变Action代码的情况下就可以更换其转发的页面,便以系统的维护。可想而知如果我们系统中有上百个页面要修改转发路径我们不必找出每个页面对应的action直接在配置文件中修改又不用重新编译即可完成。
接下来开始分析strtus的源码。
struts 核心部分 ServletAction是一个继承HttpServlet的一个普通select类。进入该类将会调用init()方法。
public void init()
throws ServletException
{
initInternal();
initOther();
initServlet();
getServletContext().
setAttribute"org.apache.struts.action.ACTION_SERVLET", this);
ModuleConfig moduleConfig = initModuleConfig("", this.config);
initModuleMessageResources(moduleConfig);
initModuleDataSources(moduleConfig);
initModulePlugIns(moduleConfig);
moduleConfig.freeze();
Enumeration names = getServletConfig().getInitParameterNames();
while (names.hasMoreElements()) {
String name = (String)names.nextElement();
if (!name.startsWith("config/")) {
continue;
}
String prefix = name.substring(6);
moduleConfig = initModuleConfig(prefix, getServletConfig ().getInitParameter(name));
initModuleMessageResources(moduleConfig);
initModuleDataSources(moduleConfig);
initModulePlugIns(moduleConfig);
moduleConfig.freeze();
}
destroyConfigDigester();
}
initInternal()方法主要对资源文件进行初始化
initOther()方法主要是加载struts配置文件的路径debug级别以及参数为convertNull时设置数据类型为空是返回null不为数据类型的默认值。
简单介绍下加载struts的配置文件默认情况下struts加载
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts</param-value>
</init-param>
读取init参数为config的这里也可以支持param-value以“,”分割的文件
如果系统偏大struts-*.xml文件过多不便于书写我们可以采用另一种形式来加载
可以继承一个ActionServlet类的一个子类来处理struts文件的加载重写initOther方法
如下面类
public class ActionServletDirectory extends ActionServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final String STRUTS_CONFIG_DIRECTORY = "directory";
private static final Log log = LogFactory.getLog(ActionServletDirectory.class);
protected void initOther () {
super.initOther();
//父类的config
this.config = getStrutsConfigFiles();
}
//文件筛选配置
private final static FilenameFilter STRUTS_CONFIG_FILE_FILTER = new FilenameFilter() {
public boolean accept(File dir, String fileName) {
String name = fileName.toLowerCase();
boolean isStrutsFile = name.indexOf("struts")!=-1;
boolean isXmlFile = name.indexOf(".xml")!=-1;
return isStrutsFile && isXmlFile;
}
};
private String getStrutsConfigFiles()
{
String directory = getServletConfig().getInitParameter(STRUTS_CONFIG_DIRECTORY);
String path = getServletContext().getRealPath(directory);
File strutsConfigDir = new File(path);
if(!strutsConfigDir.isDirectory())
{
throw new IllegalArgumentException(" >>>>>>>>>>>>>> 无效的Struts文件目录 [" + strutsConfigDir.getPath()
+ strutsConfigDir.getAbsolutePath() + "]");
}
File[] files = strutsConfigDir.listFiles(STRUTS_CONFIG_FILE_FILTER);
StringBuffer config = new StringBuffer();
log.info("start Load struts config file !");
for(int i = 0;i<files.length;i++)
{
String configFileName = directory + "/" + files[i].getName();
config.append(configFileName);
config.append(",");
log.info("[" + configFileName + "]");
}
log.info("end Load struts config file !");
return config.toString();
}
}
配置文件改成这种形式
<init-param>
<param-name>directory</param-name>
<param-value>/WEB-INF/struts</param-value>
</init-param>
直接加载directory目录下的struts-*.xml文件
分享到:
相关推荐
struts2 源码分析struts2 源码分析struts2 源码分析struts2 源码分析struts2 源码分析struts2 源码分析
struts2源码分析总结 是我在网上找到的关于源码讲解比较深刻的资料总结。对于研究struts2源码很有价值。
struts2源码详细解析51CTO下载-struts2源代码分析(个人觉得非常经典)
有关struts2运行原理的源码分析,附加相关的类图
Strut2源码分析,写的非常好, 喜欢Struts2的人可以看一看
struts 1 源码分析 struts1.2
Struts2源码阅读
struts的源码分析,整个流程,ActionServlet开始创建formbean开始
struts详细的源码分析,运行过程,想深入学习struts的小伙伴可以参看
Struts+Spring源码分析(喜欢分析源码的有福了),Struts+Spring 源码分析文档.
唉,咋下载东西都要积分呢,没办法了,只能逼着我要一分了,自己写的笔记,研究了源码,不过不太深入,大致思路还是对的
Struts2源码分析--请求处理.pdf
读者如果曾经学习过Struts1.x或者有过Struts1.x的开发经验,那么千万不要想当然地以为这一章可以跳过。实际上Struts1.x与Struts2并无我们想象的血缘关系。虽然Struts2的开发小组极力保留Struts1.x的习惯,但因为...
Struts2源码分析--请求处理[汇编].pdf
Struts2框架的简单回顾和源码分析 Strtus2框架,是基于MVC开发模式的表现层框架 作用可以说和servlet相同,作为页面和java代码之间数据交互的桥梁
struts的html标签的源码分析,关键是如何创建Formbean对象和action中的写法,加.do还是可以不加
Struts-menu源码分析(转贴).rar