【Struts2】结果类型
来源:互联网 发布:一个c语言程序的编写 编辑:程序博客网 时间:2024/06/06 04:54
在Struts2中默认提供了一系列的结果类型,我们可以在struts-default.xml中看到以下配置代码:
<result-types> <!--Action链式处理的结果类型--> <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> <!--用于与JSP整合的结果类型,作为默认的结果类型--> <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> <!--用于与FreeMarket整合的结果类型--> <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> <!--用户控制特殊的HTTP行为的结果类型--> <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/> <!--用于直接跳转到其它URL的结果类型--> <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> <!--用于直接跳转到其它Action的结果类型--> <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> <!--用于向浏览器返回一个InputStream的结果类型--> <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/> <!--用于整合Velocity的结果类型--> <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> <!--用于整合XML/XSLT的结果类型--> <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/> <!--用于显示某个页面原始代码的结果类型--> <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /> <!--将请求参数以form的形式提交到指定地点的结果类型--> <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" /></result-types>
虽然Struts2框架默认提供了这么多种的结果类型,但是并不是每一种我们在开发中都会经常使用,接下来就对开发中经常使用的以下几种结果类型进行总结。
【1. dispatcher】
· 概述
dispatcher 结果类型是Struts2框架中默认的结果类型, dispatcher 主要用于返回JSP,HTML等以页面为基础View视图。在使用 dispatcher 时,唯一需要指定的是JSP或者HTML页面的位置,这个位置将被用于定位返回的页面。
· 配置
i. 简单形式
<action name="login" class="..."> <result name="default">/default.jsp</result></action>
ii. 原始写法
<action name="login" class="..."> <result name="success" type="dispatcher"> <param name="location">/success.jsp</param> </result></action>
· 注
若没有指定location参数,则将result中间的字符串当成实际逻辑视图资源;若没有指定name属性,则采用默认值success;若没有指定type属性,则使用默认结果集dispatcher。
【2. redirect】
· 概述
redirect 和 dispatcher 结果类型是相对的, dispatcher 结果类型是将请求forward(转发)到指定的URL(可以是JSP,也可以是Action);而 redirect 结果类型,则意味着将请求redirect(重定向)到指定的视图资源。
dispatcher 和 redirect 的主要差别就是转发和重定向的差别:重定向会丢失所有的请求参数、请求属性——当然也丢失了Action的处理结果。
使用 redirect 结果类型的效果是,系统将调用 HttpServletResponse 的 sendRedirect(String) 方法来重定向指定视图资源,这种重定向的效果就是重新产生一个请求,因此所有的请求参数、请求属性、Action实例和Action中封装的属性全部丢失。
· 配置
i. 不带参数
<action name="login" class="com.hzt.struts2.action.LoginAction" method="myExecute"> <result name="success" type="redirect">/result.jsp</result></action>
ii. 带参数
<action name="action1" class="com.hzt.struts2.action.Action1"> <result name="success" type="redirect"> <param name="location">/action2.jsp</param> <param name="username">${username}</param> <param name="password">${password}</param> <param name="usernameAndPassword">${usernameAndPassword}</param> </result></action>
· 注
重新产生一个请求,因此所有的请求参数、请求属性、Action实例和Action中封装的属性全部丢失。
【3. redirectAction】
· 概述
当需要让一个Action处理结束后,直接将请求重定向(是重定向,而不是转发)到另一个Action时,我们应该使用 redirectAction 这种结果类型。
· 配置
i. 不带参数
<result name="success" type="redirectAction">action2</result>
ii. 带参数
<result name="success" type="redirectAction"> <param name="actionName">action2</param> <param name="username">${username}</param> <param name="password">${password}</param> <param name="usernameAndPassword">${usernameAndPassword}</param> </result>
【4. chain】
· 概述
chain 结果类型是将在一个Action处理完成以后,将请求再转发至另一个Action,一定要注意的是转发,转发就意味着请求参数都不会丢失。
· 配置
i. 不带参数
<result name="success" type="chain">action2</result>
ii. 带参数
<result name="success" type="chain"> <param name="actionName">action2</param> <param name="username">${username}</param> <param name="password">${password}</param> <param name="usernameAndPassword">${usernameAndPassword}</param></result>
- Struts2 结果和结果类型
- Struts2-结果和结果类型
- struts2结果类型
- struts2结果类型
- Struts2 结果类型
- Struts2结果类型
- struts2 结果类型
- struts2结果类型分析
- struts2结果类型分析
- Struts2结果类型
- struts2结果类型
- struts2结果类型
- Struts2:Result结果类型
- Struts2中的结果类型
- struts2处理结果类型
- struts2结果类型
- struts2 处理结果类型
- Struts2 自定义结果类型
- express-6-路由对象
- 几个基础算法介绍和实现——基本概念
- int char String 之间的转换
- Map的相关知识点整理
- BZOJ 3158: 千钧一发
- 【Struts2】结果类型
- django-simple-captcha 验证码插件 和自己实现验证码的
- C++之Json的一些使用心得
- 升序输出
- (OK) Android-x86-7.1.1/ kernel 4.4.62
- 代理服务器anyproxy的安装/HTTPS/规则
- Lua 多状态的迭代器
- 设计模式(Design Pattern)
- 《小白兔到大黑牛》第五篇手写Json