Action类是用户请求和业务逻辑之间的桥梁,每个Action充当客户的一项业务代理。在RequestProcessor类预处理请求时,在创建了Action的实例后,就调用自身的processActionPerform()方法,该方法在调用Action类的execute()。
Action的excute()方法调用模型的业务方法,完成用户请求,然后根据执行结果把请求转发给其他合适的WEB组件。
一、Action类缓存 struts应用的生命周期中RequestProcessor只保证一个Action实例,所有的客户请求都共享这个实例.所有请求可以同时执行它的excute()方法。RequestProcessor类包含一个HashMap,作为存放所有Action实例的缓存。每个Action实例在缓存中存放的key为Action类名。在RequestProcessor类的processActionCreate()方法中,首先检查在HashMap中是否存在Action实例,如果有直接使用,否则创建一个新的。创建Action实力的代码位于同步代码块中,以保证只有一个线程创建Action实例,然后放在HashMap中。供其他线程使用。
如下代码
- protected Action processActionCreate(HttpServletRequest request,
- HttpServletResponse response,
- ActionMapping mapping)
- throws IOException {
-
-
- String className = mapping.getType();
- if (log.isDebugEnabled()) {
- log.debug(" Looking for Action instance for class " + className);
- }
-
-
-
-
-
- Action instance = null;
- synchronized (actions) {
-
-
- instance = (Action) actions.get(className);
- if (instance != null) {
- if (log.isTraceEnabled()) {
- log.trace(" Returning existing Action instance");
- }
- return (instance);
- }
-
-
- if (log.isTraceEnabled()) {
- log.trace(" Creating new Action instance");
- }
-
- try {
- instance = (Action) RequestUtils.applicationInstance(className);
-
-
- } catch (Exception e) {
- log.error(
- getInternal().getMessage("actionCreate", mapping.getPath()),
- e);
-
- response.sendError(
- HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
- getInternal().getMessage("actionCreate", mapping.getPath()));
-
- return (null);
- }
-
- instance.setServlet(this.servlet);
- actions.put(className, instance);
- }
-
- return (instance);
-
- }
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();
if (log.isDebugEnabled()) {
log.debug(" Looking for Action instance for class " + className);
}
// :TODO: If there were a mapping property indicating whether
// an Action were a singleton or not ([true]),
// could we just instantiate and return a new instance here?
Action instance = null;
synchronized (actions) {
// Return any existing Action instance of this class
instance = (Action) actions.get(className);
if (instance != null) {
if (log.isTraceEnabled()) {
log.trace(" Returning existing Action instance");
}
return (instance);
}
// Create and return a new Action instance
if (log.isTraceEnabled()) {
log.trace(" Creating new Action instance");
}
try {
instance = (Action) RequestUtils.applicationInstance(className);
// :TODO: Maybe we should propagate this exception
// instead of returning null.
} catch (Exception e) {
log.error(
getInternal().getMessage("actionCreate", mapping.getPath()),
e);
response.sendError(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
getInternal().getMessage("actionCreate", mapping.getPath()));
return (null);
}
instance.setServlet(this.servlet);
actions.put(className, instance);
}
return (instance);
}
二.创建支持多线程的Action 1.什么是线程安全的代码
在多线程环境下能正确执行的代码就是线程安全的。
安全的意思是能正确执行,否则后果是程序执行错误,可能出现各种异常情况。
2.如何编写线程安全的代码
很多书籍里都详细讲解了如何这方面的问题,他们主要讲解的是如何同步线程对共享资源的使用的问题。主要是对synchronized关键字的各种用法,以及锁的概念。
Java1.5中也提供了如读写锁这类的工具类。这些都需要较高的技巧,而且相对难于调试。
但是,线程同步是不得以的方法,是比较复杂的,而且会带来性能的损失。等效的代码中,不需要同步在编写容易度和性能上会更好些。
我这里强调的是什么代码是始终为线程安全的、是不需要同步的。如下:
1)常量始终是线程安全的,因为只存在读操作。
2)对构造器的访问(new 操作)是线程安全的,因为每次都新建一个实例,不会访问共享的资源。
3)最重要的是:局部变量是线程安全的。因为每执行一个方法,都会在独立的空间创建局部变量,它不是共享的资源。局部变量包括方法的参数变量。
Servlet是在多线程环境下的。即可能有多个请求发给一个servelt实例,每个请求是一个线程。 struts下的action也类似,同样在多线程环境下,你也必须编写线程安全的Action类。
保证线程安全的原则就是仅仅使用局部变量,谨慎使用实例变量(拥有状态的实例,尤其是拥有业务对象状态的实例). 如果要用到那些有状态的实例,唯一和最好的办法是在Action类中,仅仅在Action类的execute()方法中使用局部变量,对于每个调用execute()方法的线程,JVM会在每个线程的堆栈中创建局部变量,因此每个线程拥有独立的局部变量,不会被其他线程共享.当线程执行完execute()方法后,它的局部变量就会被销毁.
如果Action类的实例变量是必须的话,需要采用JAVA同步机制(synchronized)对访问共享资源的代码块进行同步
三、Struts的几种Action Struts提供了一些现成的Action类,直接使用可以大大节省时间,如下
ForwardAction
可以转发到其他web组件,仅仅提供一个转发功能,不作处理。
IncludeAction
包含其他web组件。
DiapatchAction
通常一个Action只完成一个操作,用这个Action可以完成一组相关的操作。
LookupDispatchAction
他是DiapatchAction的子类,也可以定义多个方法,但主要用于一个表单里有多个按钮,而这些按钮又有一个共同的名字的场合。
SwitchAction
用于子模块之间的切换。
四.ActionForward类 Action类的excute()方法返回一个ActionForward对象,它代表了web资源的逻辑抽象,这里的web资源可以是jsp页面、Java servlet、或Action。
从excute返回ActionForward可以有两种方法。
1) 动态创建一个ActionForward实例
return new ActionForward(”Failure”,”login.jsp”,true);
2) 调用ActionMappin实例的findForward方法
这个方法先从action级别找,然后在<global-forwards />级别找
return mapping.findForward(“Failure”);
相关推荐
Android action 分类大全
很经典的struts2开发实例,其中的action类的写法可以教你很清楚的了解具体的调用过程回给你开发带来很大的帮助
封装后的ACTION 屏蔽掉了actionMaping ,使继承于该类的action类中的每个函数只返回string 类型!
基于web开发的struts应用的一个action代码,帮助初学者学习参考,希望有所帮助。
参考yunchengfeng老师的《EXTJS4.0视频教程视频》自己写的代码,网上都没有后台action中的代码,11课以后没办法学习所以自己看视频写的代码,共大家参考
一个Action对应多个类实例
struts2一个action处理多个请求
action与actionListener的区别
Javascript,提交类中附多个参数,衔接多个参数
主要给大家介绍了关于Struts2学习教程之Action类如何访问WEB资源的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
2.6.2. Struts的强项........................................................................................................58 Struts in Action 中文版 Lastest Revised:10/14/2005 10:27:00 AM ...
CRUD模型动作 用于创建CRUD Action类以在口才模型上执行操作的抽象层。安装您可以通过composer安装该软件包: composer require sfneal/crud-model-actions用法使用从CrudModelAction扩展的Action类对口才模型执行...
struts2 Action 包 命名空间 servlet APIstruts2 Action 包 命名空间 servlet APIstruts2 Action 包 命名空间 servlet APIstruts2 Action 包 命名空间 servlet APIstruts2 Action 包 命名空间 servlet APIstruts2 ...
自定义Action继承ActionSupport实现简单登录,刚开始学习struts2记录一下。
Struts action 国际化 in18
通用web层action,完美解决shiro注解权限控制,读取子类泛型
1. 介绍.........................................................................................................18 1.1. 关于本书...........................................................................
Manning.ExtJS.in.Action.Nov.2010.pdf ,注意大小,完整版,区别于meap版。
ActionServlet (Command) creates and uses Action, an ActionForm, and ActionForward. As mentioned earlier, the struts-config.xml file configures the Command. During the creation of the Web project, ...
12.2.4 创建Struts 2.0的Action类 218 12.2.5 Struts 2.0中的国际化编程 221 12.2.6 创建基于POJO的Action 223 12.3 Spring整合Struts 2.0框架 224 12.4 Struts 2.0应用实例:登录系统 227 12.5 本章小结 231 第13章...