• struts2基础——请求与响应、获取web资源


    一、请求与响应

    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

    常用的有5个类型:chain,dispatcher,redirect,redirectAction,stream

    (2) 常用结果类型详解
    dispatcher:struts2默认的结果类型,转发到目标资源,但是不能是一个 Action,因为进行内部转发的时候,struts2 无法进行拦截。

    chain:转发到一个 Action ,如:

        <action name="hello" class="com.nucsoft.struts2.helloworld.HelloWorld" method="secondMethod">
                <result type="chain">
                    <param name="actionName">worldhello</param>
                    <param name="namespace">/</param>
                </result>
            </action>
    chain

    redirect:重定向到目标资源

    <result name="redirect" type="redirect">/result.jsp</result>
    redirect

    这里指定目标资源的虚拟路径时,不能包含 web 应用的虚拟路径,目标资源也可以是一个 Action。

    redirectAction:重定向到一个 Action

        <action name="helloworld_*" class="com.nucsoft.struts2.helloworld.HelloWorld" method="{1}">
                <result>/success.jsp</result>
                <result type="redirectAction" name="redirectAction">
                    <param name="actionName">redirect2Action</param>
                </result>
            </action>
            
            <action name="redirect2Action" class="com.nucsoft.struts2.helloworld.HelloWorld" method="redirectAction">
                <result>/success.jsp</result>
            </action>
    redirectAction

    stream:
    以输出流的形式返回响应结果,用于文件的下载和 Ajax


    通配符映射规则:
    1.精确匹配优先。
    2.在不符合精确匹配优先的情况下,则先声明的有效(如果一个请求能够与多个带有通配符的ActionName匹配,则先声明的有效)。


    动态方法调用:
    在URL地址中动态调用 Action 中的方法。
    Struts2 默认是禁止动态方法调用的,可以通过修改常量的方式修改。
    正常访问:http://localhost:8989/Web应用虚拟路径/dynamicAction.action
    动态方法调用:http://localhost:8989/Web应用虚拟路径/dynamicAction!dynamicMethod.action

    二、web 资源
    Struts2 针对常用数据进行了封装,封装为一系列 Map 对象: RequestMap HashMap SessionMap ApplicationMap

    获取方式:
    1.与 Servlet 解耦
    (1) 使用 ActionContext 类,通过 ActionContext 对象可以获取到 appMap、sessionMap、paramMap对象
    (2) 实现 XxxAware 接口,RequestAware,ParameterAware,SessionAware,ApplicationAware

    2.与 Servlet 耦合
    (1) 使用 ServletActionContext 类,通过它获取到 HttpServletRequest、HttpServletResponse、ServletContext
    (2) 实现 ServletXxxAware 接口:ServletRequestAware、ServletResponseAware、ServletContextAware

  • 相关阅读:
    SuperMap房产测绘成果管理平台
    SuperMap产权登记管理平台
    Android adb shell am 的用法(1)
    由浅入深谈Perl中的排序
    Android 内存监测和分析工具
    Android 网络通信
    adb server is out of date. killing...
    引导页使用ViewPager遇到OutofMemoryError的解决方案
    adb logcat 详解
    How to send mail by java mail in Android uiautomator testing?
  • 原文地址:https://www.cnblogs.com/solverpeng/p/5646158.html
Copyright © 2020-2023  润新知