1.Action是干什么的?翻译过来就是“动作”的意思...

传统的MVC框架中,Control层一般都是一个类似与Servlet的一个Java对象。因为从职责上讲,Control层需要完成以下的职责:

1. 接收从Web容器传递过来的参数,并做恰当的类型转化

2. 调用逻辑处理

3. 搜集数据,并返回到视图

而在这个其中的第一步和第三步,都离不开Web容器中的对象的处理。

 

2.struts2中Action

在Struts2中的Action,并不需要依赖于特定的Web容器。我们看不到类似HttpServletRequest,HttpServletResponse等Web容器相关的对象。

这时候问题就出来了

a.为什么Action只是一个普通的再也不能普通的类,并且不带任何的有关Web容器相关的对象,Action又是如何工作在Web容器中的?

又是如何与WEB进行通信并获取参数的?  

Struts2为Action的执行,准备了完整的数据环境和执行环境。而这个执行环境,就保证了Action在Web容器中的顺利运行。

在Struts2中,每个Http的请求,会被发送到一个Filter。而这个Filter,就会针对每个请求,创建出一个代码的执行环境,并在这个基础上,

为每个执行环境配备与之对应的数据环境,这个数据环境中的内容,就来自于Web容器中的一个又一个对象。这样,就能够顺利调用Action执行代码而无需担心

它是否运行在   Web容器中了。

其中,数据环境就成为了Action获取Web容器的基础。所以,当Action需要获取Web容器的相关对象,需要通过数据环境来进行。

b.何为数据环境?何为执行环境?

             --ActionContext —— 数据环境

具备以下特性:

1. ActionContext应成为Action与Web容器之间的桥梁

                         2. ActionContext中应该保存有针对某个请求的详细信息

                         3. ActionContext应该是一个线程安全的类对象

             --ActionProxy —— 执行环境

既然是执行环境,那么ActionProxy就需要提供Action执行的时候一切所需要的配置、参数等等,当然,也要有进行Action调用的入口。所以让我们来看一下ActionProxy的接口:

 1 public interface ActionProxy {
 2
 3     /**
 4      * Called after all dependencies are set
 5      */
 6     void prepare() throws Exception;
 7
 8     /**
 9      * @return the Action instance for this Proxy
10      */
11     Object getAction();
12
13     /**
14      * @return the alias name this ActionProxy is mapped to
15      */
16     String getActionName();
17
18     /**
19      * @return the ActionConfig this ActionProxy is built from
20      */
21     ActionConfig getConfig();
22
23     /**
24      * Sets whether this ActionProxy should also execute the Result after executing the Action
25      *
26      * @param executeResult
27      */
28     void setExecuteResult(boolean executeResult);
29
30     /**
31      * @return the status of whether the ActionProxy is set to execute the Result after the Action is executed
32      */
33     boolean getExecuteResult();
34
35     /**
36      * @return the ActionInvocation associated with this ActionProxy
37      */
38     ActionInvocation getInvocation();
39
40     /**
41      * @return the namespace the ActionConfig for this ActionProxy is mapped to
42      */
43     String getNamespace();
44
45     /**
46      * Execute this ActionProxy. This will set the ActionContext from the ActionInvocation into the ActionContext
47      * ThreadLocal before invoking the ActionInvocation, then set the old ActionContext back into the ThreadLocal.
48      *
49      * @return the result code returned from executing the ActionInvocation
50      * @throws Exception
51      * @see ActionInvocation
52      */
53     String execute() throws Exception;
54
55     /**
56      * Sets the method to execute for the action invocation. If no method is specified, the method provided by
57      * in the action's configuration will be used.
58      *
59      * @param method the string name of the method to invoke
60      */
61     void setMethod(String method);
62
63     /**
64      * Returns the method to execute, or null if no method has been specified (meaning "execute" will be invoked)
65      */
66     String getMethod();
67
68 }  

在这其中,prepare和execute方法是用作Action调用的入口函数,其他的接口定义都与Action执行时的运行参数和配置有关。

3.实现Action类:

Struts2不要求 Action类继承任何的Struts2的基类或实现Struts2接口。(但是,我们为了方便实现Action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并重写此类里的public String execute() throws Exception方法。

Struts2中通常直接使用Action来封装HTTP请求参数,因 此,Action类里还应该包含与请求参数对应的属性,并且为属性提供对应的getter和setter方法。(当然,Action类中还可以封装处理结 果,把处理结果信息当作一属性,提供对应的getter和setter方法)

例如:

package org.caijieru.web.struts2.action;import com.opensymphony.xwork2.ActionSupport;/***@authorcaijieru*@version1.0*/
publicclass LoginAction extends ActionSupport{private String userName;private String password;private String msg; //结果信息属性/***@returnthemsg*/public String getMsg() {returnmsg;}/***@parammsgthemsgtoset*/publicvoid setMsg(String msg) {this.msg = msg;}/***@returntheuserName*/public String getUserName() {returnuserName;}/***@paramuserNametheuserNametoset*/publicvoid setUserName(String userName) {this.userName = userName;}/***@returnthepassword*/public String getPassword() {returnpassword;}/***@parampasswordthepasswordtoset*/publicvoid setPassword(String password) {this.password = password;}/***处理用户请求的excute()方法*@return结果导航字符串*@throwsException*/public String execute() throws Exception{if("test".equals(this.userName) &&
"test".equals(this.password)){msg = "登录成功,欢迎" + this.userName;returnthis.SUCCESS;}else{msg = "登录失败,用户名或密码错";returnthis.ERROR;}}
}

4.Action访问Servlet API:

Struts2中的Action并没有和任何Servlet API耦合,这样框架更具灵活性,更易测试。
但是,对于web应用的控制器而言,不访问Servlet API几乎是不可能的,例如跟踪HTTP Session状态等。
例如:
public String execute() throws Exception{if("test".equals(this.userName) && "test".equals(this.password)){msg = "登录成功,欢迎" + this.userName;//获取ActionContext实例,通过它来访问Servlet APIActionContext context = ActionContext.getContext();//看session中是否已经存放了用户名,如果存放了:说明已经登录了;
//否则说明是第一次登录成功if(null != context.getSession().get("uName")){msg = this.userName + ":你已经登录过了!!!";}else{context.getSession().put("uName", this.userName);}returnthis.SUCCESS;}else{msg = "登录失败,用户名或密码错";returnthis.ERROR;}}

Struts2中通过ActionContext来访问Servlet API,让Action彻底从Servlet API 中分离出来,最大的好处就是可以脱离Web容器测试Action。

 

转载于:https://www.cnblogs.com/cxy0703/archive/2012/05/22/2513526.html

struts2学到屎挫死-深入Struts2(2)--Action相关推荐

  1. struts2学习笔记(二) 初识Struts2

    使用Struts2的好处 (1)    第一个好处就是我们不必非得编写一个控制器不可,这使得我们可以把注意力集中在编写各种动作类里的业务逻辑方面. (2)    Struts2使用了一个基于XML的配 ...

  2. struts2学习笔记(二):Struts2配置

    struts2的默认配置文件default.properties在和核心包中的org.apache.struts2下.可以通过eclipse进行查看. 对struts2的属性配置进行调整有两种方法.下 ...

  3. Struts2(2)_什么是 struts2

    本系列博客汇总在这里:Struts2 汇总 什么是 struts2 一.概念和简介 二.Strust2 核心功能 三.Struts2 目录结构 一.概念和简介 Struts2 是一个非常优秀的 MVC ...

  4. 为什么学人工智能专业后悔死了?

    学习人工智能专业需要有足够的准备和决心,并且要了解相关的知识和技能,以及职业发展方向.最近总有同学和我说选了AI专业很后悔,如果学生已经感到后悔,可以采取一些方法来缓解情绪,并重新评估自己的职业规划. ...

  5. 【Java EE (Struts2 + Spring + Hibernate)开发】:Struts2(二)之【拦截器机制】

    [Java EE (Struts2 + Spring + Hibernate)开发]:Struts2(二)之[拦截器机制] 本文地址:http://blog.csdn.net/shanglianlm/ ...

  6. 【Struts2学习笔记(1)】Struts2中Action名称的搜索顺序和多个Action共享一个视图--全局result配置...

    一.Action名称的搜索顺序 1.获得请求路径的URI,比如url是:http://server/struts2/path1/path2/path3/test.action 2.首先寻找namesp ...

  7. 【面试 struts2】【第三篇】struts2的问题

    1.struts2的工作流程 1>客户端浏览器发出HTTP请求. 2>根据web.xml配置,该请求被FilterDispatcher接收 3>根据struts.xml配置,找到需要 ...

  8. Struts2学习笔记(六):Struts2的处理流程与Action的管理方式

    StrutsPrepareAndExecuteFilter是Struts 2框架的核心控制器,它负责拦截由<url-pattern>/*</url-pattern>指定的所有用 ...

  9. Struts2源码学习(一)——Struts2中的XWork容器

    接下来记录几篇学习Struts2源码的文章,希望能温故而知新. 目录: 1, 为什么引入容器 2,容器的定义 3,对象创建分析 4,依赖注入分析 5,对象创建和依赖注入的实现 首先,了解为什么框架要引 ...

  10. struts2学习一:搭建第一个struts2.5项目

    在官网下载struts2.5.5的jar包,地址 http://struts.apache.org/download.cgi 使用tomcat7+1.7 打开eclipse,新建Dynamic Web ...

最新文章

  1. spring计划任务
  2. ionic app调试问题
  3. 回调函数_实用程序类与函数式编程无关
  4. 错误笔记(1)——关于克隆虚拟机引发的后续问题
  5. python模块初始与time、datetime及random
  6. redis 槽点重新分配 集群_redis集群高可用部署-cluster-槽点的迁移查看
  7. c mysql 的默认值_为mysql表字段设置默认值的方法及注意细节
  8. 导出文件_一招解决PDF文件导出图片
  9. linux上安装fio教程,fio工具安装及使用
  10. ubuntu下创建软件图标和直接点文件打开
  11. 腾讯优图刷新人体姿态估计国际权威榜单,相关论文被ECCV2020收录
  12. 使用Sencha Touch加载服务器端数据。
  13. 实例:供应商管理报表需求调研报告
  14. 富文本支持粘贴excel表格_Anki插件-OneNote importer(富文本批量导入)
  15. Eigen教程(11)之存储顺序
  16. 哪所985计算机专业招文科,4张表看透36所985大学自主招生专业
  17. Android Studio如何打jar包
  18. 模板题——数位DP、状态压缩、记忆化搜索
  19. 菜鸟电子面单获取教程
  20. GraphQL 总结 + 在Django应用(Graphene)

热门文章

  1. 【BZOJ1150】数据备份(堆/优先队列)
  2. 【2019银川网络赛D:】Take Your Seat(概率--递推+思维)
  3. windows与linux线程,Linux和Windows两种风格的操作系统,创建线程的方式有何不同?...
  4. ds查找—二叉树平衡因子_《大话数据结构》C++实现二叉平衡树的建立
  5. Kubernetes可以代替Docker,可笑
  6. 易筋SpringBoot 2.1 | 第十三篇:SpringBoot综合应用多个DataSoure
  7. Centos Python安装graphviz和pydotplus
  8. Ubuntu16.04实现定时免密远程拷贝脚本
  9. 翻山越岭上uvaoj
  10. 圆环和环形是一样的吗_饼图太丑?只需3步,立马变身成好看的环形图!