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. SpringMVC原理及非注解配置详解

    1. Spring介绍 Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得控制器的开发和测试更加简单. 这些控制器 ...

  2. 安装Scala-2.11.7——集群学习日记

    前言 在安装Spark之前,我们需要安装Scala语言的支持.在此我选择的是scala-2.11.7版本. scala-2.11.7下载 为了方便,我现在我的SparkMaster主机上先安装,把目录 ...

  3. Spring定时器实现(二)

    Spring结合quarzt可以实现更复杂的定时器,现做简单介绍相关配置: <?xml version="1.0" encoding="UTF-8"?&g ...

  4. useradd新建用户和权限分配

    场景:在搭建Ftp服务器时候,需要新建ftp用户,其实新建的ftp用户和Linux中root新建的用户一样,只是需要了解新建用户时候的相关规则. 1 解决新建用户缺少配置文件 1.1 新建用户 指定目 ...

  5. Windows查看端口使用状况

    使用端口是我们在进行远程或者打印机等都会遇到的,但是有很多用户会遇到端口被占用的情况,遇到这样的问题首先就要找出电脑中的所以端口然后进行查看,还是有很多人不知道该如何查看电脑端口. 1 查看windo ...

  6. Spring源码情操陶冶-ContextLoader

    前言-阅读源码有利于陶冶情操,本文承接前文Spring源码情操陶冶-ContextLoaderListener 静态代码块内容 ContextLoader在被主动调用的时候,会执行其的一个静态块,代码 ...

  7. eclipse中console的输出行数控制

    eclipse中console的输出行数控制 开发中,会遇到当输出大量的sql语句或者错误的时候,往往会因为console输出的限制而不能完整显示,所以我们自己就需要迫切的增加显示的行数,这样 就可以 ...

  8. openstack中使用linux_bridge实现vxlan网络

    openstack环境: 1 版本:ocata 2 系统:ubuntu16.04.2 3 控制节点 1个 + 计算节点 1个 4 控制节点网卡为ens33,ip = 172.171.5.200 ens ...

  9. 你可能需要为你的APP适配iOS11

    WeTest 导读  iOS 11 为整个生态系统的 UI 元素带来了一种更加大胆.动态的新风格. 本文介绍了iOS11在UI方面做了哪些更新,有些更新可以为用户提供更加完美的体验,但也有的可能会给目 ...

  10. python列表反转

    使用reverse来让列表反转特别方便, 没事自己写了几种方式 In [59]: def reverse(nums): length = len(nums) for i in range(length ...