HTTP请求 提交 Struts2 StrutsPrepareAndExecuteFilter 核心控制器 —— 请求分发给不同Action

Action书写的的三种格式

第一种 Action可以是 POJO ((PlainOldJavaObjects)简单的Java对象) —- 不需要继承任何父类,实现任何接口

* struts2框架 读取struts.xml 获得 完整Action类名

* obj = Class.forName(“完整类名”).newInstance();

* Method m = Class.forName(“完整类名”).getMethod(“execute”); m.invoke(obj); 通过反射 执行 execute方法

第二种 编写Action 实现Action接口

Action接口中,定义默认五种 逻辑视图名称

public static final String SUCCESS = “success”; // 数据处理成功 (成功页面)

public static final String NONE = “none”; // 页面不跳转 return null; 效果一样

public static final String ERROR = “error”; // 数据处理发送错误 (错误页面)

public static final String INPUT = “input”; // 用户输入数据有误,通常用于表单数据校验 (输入页面)

public static final String LOGIN = “login”; // 主要权限认证 (登陆页面)

  • 五种逻辑视图,解决Action处理数据后,跳转页面

第三种 编写Action 继承ActionSupport (推荐)

在Action中使用 表单校验、错误信息设置、读取国际化信息 三个功能

Action的配置method(通配符)

1) 在配置 元素时,没有指定method属性, 默认执行 Action类中 execute方法



2) 在 元素内部 添加 method属性,指定执行Action中哪个方法

执行 RegistAction 的regist方法

* 将多个请求 业务方法 写入到一个Action 类中

3) 使用通配符* ,简化struts.xml配置

添加客户

删除客户

struts.xml
<action name="customer_*" class="cn.itcast.struts2.demo4.CustomerAction" method="{1}"></action>   ---  {1}就是第一个* 匹配内容

动态方法调用

访问Action中指定方法,不进行配置

1) 在工程中使用 动态方法调用 ,必须保证 struts.enable.DynamicMethodInvocation = true 常量值 为true

2) 在action的访问路径 中 使用 “!方法名”

页面

添加商品

配置



执行 ProductAction 中的 add方法

Action访问Servlet

1、 在Action 中解耦合方式 间接访问 Servlet API ——— 使用 ActionContext 对象

在struts2 中 Action API 已经与 Servlet API 解耦合 (没有依赖关系 )

* Servlet API 常见操作 : 表单提交请求参数获取,向request、session、application三个范围存取数据

actionContext = ActionContext.getContext();

1) actionContext.getParameters(); 获得所有请求参数Map集合

2) actionContext.put(“company”, “传智播客”); / actionContext.get(“company”) 对request范围存取数据

3) actionContext.getSession(); 获得session数据Map,对Session范围存取数据

4) actionContext.getApplication(); 获得ServletContext数据Map,对应用访问存取数据

2、 使用接口注入的方式,操作Servlet API (耦合)

ServletContextAware : 注入ServletContext对象

ServletRequestAware :注入 request对象

ServletResponseAware : 注入response对象

  • 程序要使用哪个Servlet的对象,实现对应接口

3、 在Action中直接通过 ServletActionContext 获得Servlet API

ServletActionContext.getRequest() : 获得request对象 (session)

ServletActionContext.getResponse() : 获得response 对象

ServletActionContext.getServletContext() : 获得ServletContext对象

* 静态方法没有线程问题,ThreadLocal

Result结果类型

Action处理请求后, 返回字符串(逻辑视图名), 需要在struts.xml 提供 元素定义结果页面

1、 局部结果页面 和 全局结果页面





/demo6/result.jsp

/demo6/result.jsp

2、 结果页面跳转类型

* 在struts-default.xml 定义了 一些结果页面类型

* 使用默认type 是 dispatcher 转发 (request.getRequestDispatcher.forward)

1) dispatcher :Action 转发给 JSP

2) chain :Action调用另一个Action (同一次请求)

hello hello是一个Action的name

3) redirect : Action重定向到 JSP

4) redirectAction :Action重定向到另一个Action

hello

struts2--Action的更多相关文章

  1. struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法

    struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法

  2. struts2 action 页面跳转

    struts2 action 页面跳转 标签: actionstruts2redirect 2013-11-06 16:22 20148人阅读 评论(0) 收藏 举报 (1)type="di ...

  3. Java Hour 32 Weather ( 5 ) struts2 – Action class

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 32 Struts2 Action 1 将action 映射到 ac ...

  4. Struts2 Action接收表单参数

    struts2 Action获取表单传值    1.通过属性驱动式    JSP:        <form action="sys/login.action" method ...

  5. struts2 action result type类型

    struts2 action result type类型 1.chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. com.opensymphony. ...

  6. struts2 action通配符

    首先,看一个struts2的配置文件: <package name="actions" extends="struts-default" namespac ...

  7. 第三章Struts2 Action中动态方法调用、通配符的使用

    01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...

  8. [Struts2] Action Implements SessionAware

    struts2 的Action中若希望访问Session对象,可采用两种方式: 1.从ActionContext中获取: 2.实现SessionAware接口. 1.从ActionContext中获取 ...

  9. Struts2 Action下面的Method调用方法

    1. 在struts.xml中加入<constant name="struts.enable.DynamicMethodInvocation" value="tru ...

  10. Struts2 Action中动态方法调用、通配符的使用

    一.Struts2执行过程图: 二.struts2配置文件的加载顺序 struts-default.xml---struts-plugin.xml---struts.xml 具体步骤: 三.Actio ...

随机推荐

  1. 【转载】接触Matlab10年后的一个总结,随时使用Matlab要掌握的一些要点

    来源: http://www.cnblogs.com/asxinyu/p/Basic_Matlab_Experience.html 接触Matlab10年后的一个总结,随时使用Matlab要掌握的一些 ...

  2. ionic2+Angular 使用ng2-file-upload 插件上传图片并实现本地预览

    第一步:npm install ng2-file-upload --save 安装 ng2-file-upload 第二步:在需要使用该插件的页面的对应module文件的imports中引入Commo ...

  3. 将bbr功能合入到centos7.3

    今天将bbr的算法合入到了centos7.3的内核,基线内核版本是3.10.0-514.el7.x86_64, 内核编译测试通过.感谢隆春和文洋的帮助,隆春是将bbr合入到了cgslv5版本. 这种反 ...

  4. 【SignalR学习系列】3. SignalR实时高刷新率程序

    创建项目 创建一个空的 Web 项目,并在 Nuget 里面添加 SignalR,jQuery UI 包,添加以后项目里包含了 jQuery,jQuery.UI ,和 SignalR 的脚本. 创建基 ...

  5. JavaScript的六种继承方式

    继承是面向对象编程中又一非常重要的概念,JavaScript支持实现继承,不支持接口继承,实现继承主要依靠原型链来实现的 原型链 首先得要明白什么是原型链,在一篇文章看懂proto和prototype ...

  6. Mybatis-Generator生成Mapper文件中<if test="criteria.valid">的问题解答

    写在前面 <Docker+SpringBoot+Mybatis+thymeleaf的Java博客系统开源啦> 由于开源了项目的缘故,很多使用了My Blog项目的朋友遇到问题也都会联系我去 ...

  7. CRM权限管理

    CRM权限管理 一.概念 权限管理就是管理用户对于资源的操作.本 CRM 系统的权限(也称作资源)是基于角色操作权限来实现的,即RBAC(Role-Based Access Control,基于角色的 ...

  8. LNMP环境的安装

    一.LNMP的安装 1.准备工作 #清理已经安装包 rpm -e httpd rpm -e mysql rpm -e php yum -y remove httpd yum -y remove mys ...

  9. python爬取百度搜索结果ur汇总

    写了两篇之后,我觉得关于爬虫,重点还是分析过程 分析些什么呢: 1)首先明确自己要爬取的目标 比如这次我们需要爬取的是使用百度搜索之后所有出来的url结果 2)分析手动进行的获取目标的过程,以便以程序 ...

  10. wamp问题:关于另个php.ini文件的”…

    一.现象解说 修改从图表打开的php.ini文件,重启apache后,我们的问题没有解决... 二.解决方法 1.php.ini的位置 wamp/apache2/bin/php.ini wamp/ph ...