经过对struts1.x源码的研读发现:
struts1.2获取action的方式是单例的,所有的action都被维护在一个hashMap里,当有请求到达时,先根据action的名称去hashMap里查找要请求的Action是否已经存在,如果存在,则直接返回hashMap里的action。如果不存在,则创建一个新的Action实例。
下面我们来分析一下源码:
请求到达ActionServlet时,首先会到达doGet()或doPost()方法,而ActionServlet转给了process()方法进行统一处理
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
}
而process()方法又会转给processor的process()方法进行处理
protected void process(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
...
processor.process(request, response);
}
processor的process()方法里经过一系列处理后,最后通过processActionCreate方法来返回一个具体的action实例
public void process(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
...
// Create or acquire the Action instance to process this request
Action action = processActionCreate(request, response, mapping);
if (action == null) {
return;
}
...
}
那我们就到processActionCreate这个方法里去一窥究竟吧:
1、先获取类名
2、根据类名去一个名为actions的map里查寻实例是否已经存在
3、如果存在,则直接返回
4、如果不存在,则创建一个新实例
5、把创建好的action放到map里备用
protected Action processActionCreate(HttpServletRequest request,
HttpServletResponse response,
ActionMapping mapping)
throws IOException {
// Acquire the Action instance we will be using (if there is one)
String className = mapping.getType();//1、先获取类名
...
Action instance = null;
synchronized (actions) {
// Return any existing Action instance of this class
instance = (Action) actions.get(className);//2、根据类名去map里查寻实例是否已经存在
if (instance != null) {
return (instance); //3、如果存在,则直接返回
}
// Create and return a new Action instance
//4、如果不存在,则创建一个新实例
instance = (Action) RequestUtils.applicationInstance(className)
instance.setServlet(this.servlet);
actions.put(className, instance);//5、把创建好的action放到map里
}
...
return (instance);
}
我们再来看看actions的定义:
/**
* The set of Action instances that have been created and
* initialized, keyed by the fully qualified Java class name of the
* Action class.
*/
protected HashMap actions = new HashMap();
结论:
struts1.x里的action是单例模式的,供多个请求共享。所以不是线程安全的。
其数据都通过actionForm进行传递。
因此,
action实例里面不应该声明带有状态的实例变量。
分享到:
相关推荐
struts2-core-2.0.1.jar, struts2-core-2.0.11.1.jar, struts2-core-2.0.11.2.jar, struts2-core-2.0.11.jar, struts2-core-2.0.12.jar, struts2-core-2.0.14.jar, struts2-core-2.0.5.jar, struts2-core-2.0.6.jar,...
struts-core-1.3.10.jar, struts2-cdi-plugin-2.3.30.jar, struts2-codebehind-plugin-2.3.30.jar, struts2-config-browser-plugin-2.3.30.jar, struts2-convention-plugin-2.3.30.jar, struts2-core-2.3.30.jar, ...
在struts1.x版本中使用validator验证框架实例。
servlet-api-2.5.jar和jsp-api-2.1.jar
struts-2.1.8.1源码和struts-2.3.15.1源码
java框架struts-2.3.15.3源码,这个本来是源码和lib库一起的,但是文件太大,我就把lib库删了,只剩源码了.后面我继续上传lib库
struts2-bean-validation-plugin-2.5.2.jar, struts2-cdi-plugin-2.5.2.jar, struts2-config-browser-plugin-2.5.2.jar, struts2-convention-plugin-2.5.2.jar, struts2-core-2.5.2.jar, struts2-dwr-plugin-2.5.2....
完全是html格式,适用于所有技术人员,放在tomcat下可直接运行的struts1.x标签详细帮助文档。
Struts1.x常用的开发包,需要学习struts1.x朋友可以下载
jquery和struts2结合使用的jar包
struts-bean-el.tld, struts-bean.tld, struts-faces.tld, struts-html-el.tld, struts-html.tld, struts-logic-el.tld, struts-logic.tld, struts-nested.tld, struts-tiles-el.tld, struts-tiles.tld, x-rt.tld, x...
【struts1.x---2.0】-jar包大全,全部由本人在做项目时全部整理,做项目时所用到的jar包,希望能给各位带来一点方便~~~~~~!!!!!
struts1.x和struts2.x区别
struts 2.3.15.1 struts-2.3.15.1 源码 src
struts2最新版本struts2-core-2.1.8.1与xwork-core-2.1.6 源码
struts-2.1.8.1+xwork-2.1.6源码
从官网下的Struts2.3.1.2源码,方便自己也方便大家下下来看看。
Struts1.x-Jdbc,学习Struts1.的曾删改查
org.springframework.aop-3.0.4.RELEASE.jar org.springframework.asm-3.0.4.RELEASE.jar org.springframework....org.springframework.web.struts-3.0.4.RELEASE.jar org.springframework.web-3.0.4.RELEASE.jar
第一次上传,要5分,是物有所值,处理了jsonplugin 的no session的问题,并且添加了,新的注释,例如 @JSON(exclude='对象的名称') 就可以剔除 解决json与Hibernate之间的大...3.不需要配置底层实体,在当前action配置就可以