您的位置:首页 > 编程语言 > Java开发

day27_struts2のPPT学习4——04 配置处理结果.ppt

2015-03-17 18:36 507 查看

day27_struts2のPPT学习4——04 配置处理结果.ppt

配置处理结果

理解处理结果

Action处理完用户请求后,将返回一个普通字符串

整个普通字符串就是一个逻辑视图名

Struts2 根据逻辑视图名,决定响应哪个结果

Struts2处理结果使用元素配置



局部结果:将作为子元素配置

全局结果:将作为元素的子元素配置

配置元素通常需要指定两个属性


name 该属性指定配置逻辑视图名

type 该属性指定结果类型




全局结果

当多个action中都使用到了相同result,这时我们应该把result定义为全局结果。struts1中提供了全局forward,struts2中也提供了相似功能:

<package ....>
    <global-results>
        <result name="message">/message.jsp</result>
    </global-results>
</package>


注:局部的会覆盖全局

Struts1中应用范围内action的实例 action是单实例(执行时,现在缓存中查找实例,有用,没有创建新的实例)

Struts2中 应用范围内action的实例,每个请求都会创建一个action实例

Servlet属于单实例多线程的应用,实例只在初始化时被加载

多实例比单实例的优点,不会产生并发问题,但执行速度不如单实例

结果类型 (result节点中的type属性取值于结果类型)

struts-default.xml 配置常用结果类型

<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>


结果类型: dispatcher

dispatcher 结果类型是最常用的结果类型, 也是 struts 框架默认的结果类型

该结果类型有一个 location 参数, 它是一个默认参数



<result name="success" type="dispatcher">
    <param name="location">/resulttype/success.jsp</param>
</result>
        ||
<result name="success" type="dispatcher">/resulttype/success.jsp
</result>


- dispatcher 结果类型将把控制权转发给应用程序里的某个资源.

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

结果类型: redirect

redirect 结果类型将把响应重定向到另一个资源, 而不是转发给该资源.

redirect 结果类型接受下面这些参数:

location: 用来给出重定向的目的地

param: 用来表明是否把 location 参数的值视为一个 OGNL 表达式来解释. 默认值为 true

redirect 结果类型可以把响应重定向到一个外部资源

<result name="successredirect" type="redirect">
    <param name="location>/resulttype/success.jsp</param>
</result>
    ||
<result name="successredirect" type="redirect">/resulttype/success.jsp
</result>


结果类型: redirectAction

redirectAction 结果类型把响应重定向到另一个 Action

redirectAction 结果类型接受下面这些参数:

actionName: 指定 “目的地” 动作的名字. 它是默认属性

namespace: 用来指定 “目的地” 动作的命名空间. 如果没有配置该参数, Struts 会把当前 Action 所在的命名空间作为 “目的地” 的命名空间



<result name="successredirectAction" type="redirectAction">
    <param name="actionName">helloWorldAction</param>
    <param name="namespace">/primer</param>
</result>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: