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. Android - DrawerLayout

    Android DrawerLayout 的使用 Android L Android Studio 1.4 从主视图左侧能抽出一个导航栏,效果图:  点击后弹出新界面:  新界面也可以抽出左侧导航栏 ...

  2. 【Splay】例题

    营业额统计 题目背景 HNOI2002 DAY2 T2 题目描述 Tiger 最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger 拿出了公司 ...

  3. NLP —— 图模型(三)pLSA(Probabilistic latent semantic analysis,概率隐性语义分析)模型

    LSA(Latent semantic analysis,隐性语义分析).pLSA(Probabilistic latent semantic analysis,概率隐性语义分析)和 LDA(Late ...

  4. 安装harbor私有镜像仓库

    有朋友安装harbor的过程中遇到很多问题,为此写一篇最简单安装harbor的文档,希望能帮助所有刚开始接触harbor的新手.harbor的架构不做探究. 实验验环境:os --> cento ...

  5. CocoaPods私有库管理

    简介: 前一篇文章已经介绍过如果安装使用CocoaPods,下面将要介绍如果通过CocoaPods和git来维护我们私有的库. 个人或公司在开发过程中,会积累很多可以复用的代码包,有些我们不想开源,又 ...

  6. Excel Countif函数用法

    公式:=COUNTIF(范围,条件) 例1: 统计A1:A11当中,等于数字3的单元格,结果是4个. 例2:还可以进行大于(>),大于等于(>=),小于(<),小于等于(<=) ...

  7. 【JAVASCRIPT】React学习-组件生命周期

    摘要 整理组件加载过程,详细见官方文档:https://facebook.github.io/react/docs/react-component.html mount 过程 1)constructo ...

  8. [server]nginx 一系列命令

    h1. 启动 nginx -c /usr/local/etc/nginx/nginx.conf h1. 停止 nginx -s stop h1. reload nginx -s reload h1.

  9. 打造 高性能,轻量级的 webform框架---js直接调后台(第二天)

    问题2: 每次与后台打交道 都需要写一些自己都看不太懂的事件,而且传参数很麻烦,这就是.net 封装的事件,如何解决呢?        首先以为webfrom事件,都需要写 服务器控件来绑定后台的事件 ...

  10. ionic时间插件ion-datetime-picker

    https://github.com/katemihalikova/ion-datetime-picker