12. Struts2_result

来源:互联网 发布:思科网络架构师 编辑:程序博客网 时间:2024/06/06 07:40

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

这里写图片描述

/WEB-INF/views/success.jsp
dispatcher 结果类型是最常用的结果类型, 也是 struts 框架默认的结果类型
该结果类型有一个 location 参数, 它是一个默认参数
dispatcher 结果类型将把控制权转发给应用程序里的指定资源.
dispatcher 结果类型不能把控制权转发给一个外部资源. 若需要把控制权重定向到一个外部资源, 应该使用 redirect 结果类型

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

            <!-- 重定向的时候要注意,不能通过重定向去访问web-info目录下的页面。能重定向到另外一个请求            请求要注意:是到对应的package下的:namespace+action下的name+默认的后缀名            <constant name="struts.action.extension" value="action,do"></constant>            即:package:namespace+action:name+struts.action.extension:value=            /hgh/testaction.do            <result name="redirectTest" type="redirect">/hgh/testaction.do</result>     -->

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

            <!-- 重定向到一个 Action -->            <result name="redirectActionTest" type="redirectAction">                <param name="actionName">testaction</param>                <param name="namespace">/hgh</param>            </result>
0 0