一、请求与响应

Action
1.含义:
(1) struts.xml 中的 action 元素,也指 from 表单的 action 属性,总之代表一个 struts2 请求。
(2) 用于处理 Struts2 请求的 Action 类

2.Action 类
(1) 使用 setXxx() 方法和 getXxx() 方法定义属性,使用 setXxx() 属性方法接受请求参数值,使用 getXxx() 方法来在页面显示数据。
(2) 有无参构造器
(3) 至少有一个供 Struts2 在执行这个 action 时调用的方法
(4) 同一个 Action 来可以包含多个 action 方法
(5) Action 类不是单例的,Struts2 为每一个请求创建一个 Action 实例

3.ActionSupport 类
(1) ActionSupport 是默认的 Action 类
(2) ActionSupport 实现的接口

Action:提供了 SUCCESS、INPUT 等字符串常量可以直接使用,提供 execute() 抽象方法供实现
Validateable:用于编程式输入验证
ValidationAware:用于获取和显示错误消息,错误消息有两个级别:一个是类级别,另一个是字段级别
TextProvider:从资源文件中读取属性值,用于国际化
LocaleProvider:获取 Locale 对象,用于国际化
Serializable:序列化 Action 类

4.请求扩展名
(1) 请求路径中 ServletPath 部分包含的后缀,例如:.action/.do
(2) Struts2 根据扩展名来区分哪些请求需要 Struts2 处理,哪些不需要。
(3) 默认支持的扩展名: .action 和 没有

5.default.properties
在 default.properties 定义了许多常量,如默认支持的请求扩展名
修改 default.properties 定义的常量:
在 struts 根标签下加入元素:
<constant name="" value=""/>
如修改默认支持的请求扩展名:
<constant name="struts.action.extension" value=".action,,.do"/>


Result:
1.含义:
(1) 代表 Struts2 请求的响应,每个 action 标签可以包含多个 result 元素
(2) result 标签:name 属性:对应 action 方法的 String 类型方法返回值,type 属性:执行结果类型,以什么方式跳转。

2.结果类型:
(1) Struts2 在 struts-default.xml 中定义了 10 结果类型

<result-types>
            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
            <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
            <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
            <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
            <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
            <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
            <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
            <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
            <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
            <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
        </result-types>
result types

相关文章: