Struts2-014-详解result节点

来源:互联网 发布:小班美工区域观察记录 编辑:程序博客网 时间:2024/06/08 18:33

微信公众号:JavaWeb架构师

概述

  • result是action节点的子节点,每个 action 方法都将返回一个 String 类型的值, Struts 将根据这个值来决定响应什么结果.
  • 每个 action 声明都必须包含有数量足够多的 result 元素, 每个 result 元素分别对应着 action 方法的一个返回值.
  • result 元素可以有下面两个属性
    • name: 结果的名字, 必须与 Action 方法的返回值相匹配, 默认值为 success
    • type: 响应结果的类型. 默认值为 dispatcher(转发),还可以取值redirect(重定向)

结果类型

微信公众号:JavaWeb架构师

dispatcher

  • dispatcher 结果类型是最常用的结果类型, 也是 struts 框架默认的结果类型
  • 该结果类型有一个 location 参数, 它是一个默认参数
    微信公众号:JavaWeb架构师

  • dispatcher 结果类型将把控制权转发给应用程序里的指定资源.

  • dispatcher 结果类型不能把控制权转发给一个外部资源. 若需要把控制权重定向到一个外部资源, 应该使用 redirect 结果类型

redirect

  • redirect 结果类型将把响应重定向到另一个资源, 而不是转发给该资源.
  • redirect 结果类型接受下面这些参数:
    • location: 用来给出重定向的目的地.它是默认属性
    • parse: 用来表明是否把 location 参数的值视为一个 OGNL 表达式来解释. 默认值为 true
  • redirect 结果类型可以把响应重定向到一个外部资源

微信公众号:JavaWeb架构师

redirectAction

  • redirectAction 结果类型把响应重定向到另一个 Action
  • redirectAction 结果类型接受下面这些参数:
    • actionName: 指定 “目的地” action 的名字. 它是默认属性
    • namespace: 用来指定 “目的地” action 的命名空间. 如果没有配置该参数, Struts 会把当前 Action 所在的命名空间作为 “目的地” 的命名空间

微信公众号:JavaWeb架构师

chain

  • chain 结果类型的基本用途是构成一个 action 链: 前一个 action 把控制权转发给后一个 action, 而前一个 action 的状态在后一个 action 中依然保持
  • chain 结果类型接受下面这些参数:
    • actionName: 指定目标 action 的名字. 它是默认属性
    • namespace: 用来指定 “目的地” action 的命名空间. 如果没有配置该参数, Struts 会把当前 action 所在的命名空间作为 “目的地” 的命名空间
    • method: 指定目标 action 方法. 默认值为 execute

注意

  • 通过单纯的redirect也可以重定向到一个Action,形式如下:
    /命名空间/action
  • 通过单纯的dispatcher不可以转发到一个Action,需要使用type=chain

name

  • 我们在action节点设置了class值,还有method值(不设置会有默认值)。当访问这个action的name的时候,就会去访问这个action的class类的method方法,这个method方法会返回一个字符串,这个字符串将与result的name值进行匹配,匹配到了就执行这个result的资源。
// action<action name="access" class="top.itcourse.result.TestResult" method="execute">// top.itcourse.result.TestResult的execute方法public String execute() {        String page = "default";        System.out.println("result: " + result);        if( result.equals("1") ) {            page = "success";        } else if (result.equals("2")) {            page = "redirectAction";        }        else if( result.equals("3")){            page = "logout";        } else if (result.equals("4")) {            page = "dispatcherAction";        }          else {            System.out.println("下面的默认页,类型是重定向,注意看地址栏会变化~~~~");        }        System.out.println("page: " + page);        System.out.println("------------------------------------------------------------");        return page;    }// 匹配result的name值<result name="success">success.jsp</result><result name="logout">logout.jsp</result>           <result name="default" type="redirect">default.jsp</result>

type

  • type所有取值
<result-types>    // 转发到一个Action    <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"/>    // 重定向到一个Action    <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>
  • dispatcher和redirect和之前学过的一样。

  • redirectAction(重定向到一个Action)
    redirectAction的配置:

<!--  重定向到一个Action:    1.result的type指定为:redirectAction    2.在result增加一个节点:<param name="actionName">action的名字</param>    3.在result增加一个节点:<param name="namespace">/命名空间的名字</param>注意:    通过单纯的redirect也可以重定向到一个Action,形式如下:        <result name="结果名" type="redirect">/命名空间/action</result>        <result name="redirectAction名" type="redirect">/testNamespace/testAction.后缀</result>--><!-- 重定向到一个Action --><result name="redirectAction" type="redirectAction">    <!-- action的名字 -->    <param name="actionName">testAction</param>    <!-- 这个action所在的命名空间 -->    <param name="namespace">/testNamespace</param></result> 

testPackage包:

    <!--          重定/转发向到一个Action:            1.package节点的namespace是上面的 <param name="namespace">/命名空间的名字</param> 所指向的            2.action节点的name是上面的 <param name="actionName">action的名字</param> 所指向的    -->    <package name="testPackage" namespace="/testNamespace" extends="struts-default">        <action name="testAction" class="top.itcourse.result.TestRedirectAction">        </action>    </package>
  • chain(转发到一个Action),和redirectAction一样,只是type不同。

  • 注意:

// 通过单纯的redirect也可以重定向到一个Action,形式如下:<result name="结果名" type="redirect">/命名空间/action</result><result name="redirectAction名" type="redirect">/testNamespace/testAction.后缀</result>// 但是通过单纯的dispatcher不可以转发到一个Action

测试代码

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE struts PUBLIC      "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"      "http://struts.apache.org/dtds/struts-2.3.dtd">  <struts>      <constant name="struts.action.extension" value="do,,action"/>      <package name="default" extends="struts-default">        <action name="access" class="top.itcourse.result.TestResult">            <!--                  result:                    1.name属性:                        name: 结果的名字, 必须与 Action 方法的返回值相匹配, 默认值为 success                    2.type属性:                        结果的响应类型,常用取值dispatcher(转发)、redirect(重定向),默认dispatcher。                        所有取值(name值):                                <result-types>                                        // 转发到一个Action                                        <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"/>                                    // 重定向到一个Action                                    <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>                    3.标签体:                        将转发/重定向到资源的路径            -->            <!-- 转发 -->            <result name="success">success.jsp</result>            <result name="logout">logout.jsp</result>            <!-- 请求 -->            <result name="default" type="redirect">default.jsp</result>            <!--              重定向到一个Action:                1.result的type指定为:redirectAction                2.在result增加一个节点:<param name="actionName">action的名字</param>                3.在result增加一个节点:<param name="namespace">/命名空间的名字</param>            注意:                通过单纯的redirect也可以重定向到一个Action,形式如下:                    <result name="结果名" type="redirect">/命名空间/action</result>                    <result name="redirectAction名" type="redirect">/testNamespace/testAction.后缀</result>            -->            <!-- 重定向到一个Action -->            <result name="redirectAction" type="redirectAction">                <!-- action的名字 -->                <param name="actionName">testAction</param>                <!-- 这个action所在的命名空间 -->                <param name="namespace">/testNamespace</param>            </result>             <!--                  转发到一个Action:                    1.和重定向到一个Action一样,只是type值是chain                注意:                    通过单纯的dispatcher不可以转发到一个Action            -->            <result name="dispatcherAction" type="chain">                <!-- action的名字 -->                <param name="actionName">testAction</param>                <!-- 这个action所在的命名空间 -->                <param name="namespace">/testNamespace</param>            </result>        </action>    </package>    <!--          重定/转发向到一个Action:            1.package节点的namespace是上面的 <param name="namespace">/命名空间的名字</param> 所指向的            2.action节点的name是上面的 <param name="actionName">action的名字</param> 所指向的    -->    <package name="testPackage" namespace="/testNamespace" extends="struts-default">        <action name="testAction" class="top.itcourse.result.TestRedirectAction">        </action>    </package></struts>  

TestResult.java

package top.itcourse.result; public class TestResult {    // 根据这个属性的值,来决定action方法的返回值     private String result;    public String getResult() {        return result;    }    public void setResult(String result) {        this.result = result;    }    public TestResult() {    }    // action方法    public String execute() {        String page = "default";        System.out.println("result: " + result);        if( result.equals("1") ) {            page = "success";        } else if (result.equals("2")) {            page = "redirectAction";        }        else if( result.equals("3")){            page = "logout";        } else if (result.equals("4")) {            page = "dispatcherAction";        }          else {            System.out.println("下面的默认页,类型是重定向,注意看地址栏会变化~~~~");        }        System.out.println("page: " + page);        System.out.println("------------------------------------------------------------");        return page;    }}

效果

微信公众号:JavaWeb架构师


其它



- 源码下载

关注下方公众号,回复:struts2_course.code该文件是:Struts2_Course_011_Result
  • 欢迎加入交流群:451826376

  • 更多信息:www.itcourse.top

完整教程PDF版本下载

原创粉丝点击