Fork me on GitHub

【Struts】strust.xml中<result type="">所有类型详解

在默认时,<result>标签的type属性值是“dispatcher”(实际上就是转发,forward)。开发人员可以根据自己的需要指定不同的类型,如redirect、stream等。如下面代码所示: <result name="save" type="redirect"> /result.jsp </result>
这时result-type可以在struts2-core-2.0.11.1.jar包或struts2源代码中的struts-default.xml文件中找到,在这个文件中找到<result-types>标签,所有的result-type都在里面定义了。

已配置结果类型名

类 名

描 述

dispatcher org.apache.struts2.dispatcher.
ServletDispatcherResult  
默认结果类型,用来呈现JSP页面
chain com.opensymphony.xwork2.
ActionChainResult
将action和另外一个action链接起来
freemarker org.apache.struts2.views.freemarker.
FreemarkerResult
呈现Freemarker模板
httpheader org.apache.struts2.dispatcher.
HttpHeaderResult
返回一个已配置好的HTTP头信息响应
redirect org.apache.struts2.dispatcher.
ServletRedirectResult
将用户重定向到一个已配置好的URL
redirectAction org.apache.struts2.dispatcher.
ServletActionRedirectResult
将用户重定向到一个已定义好的action
stream org.apache.struts2.dispatcher.
StreamResult
将原始数据作为流传递回浏览器端,
该结果类型对下载的内容和图片非常有用
velocity org.apache.struts2.dispatcher.
VelocityResult
呈现Velocity模板
xslt org.apache.struts2.views.xslt.
XSLTResult
呈现XML到浏览器,
该XML可以通过XSL模板进行转换
plaintext org.apache.struts2.dispatcher.
PlainTextResult
返回普通文本类容

 

dispatcher:用于页面转发,页面跳转过程一直是同一个线程,Action中的数据一直保存在。
redirect:可用于返回一个页面、一个action、链接到一个网址。
   缺点:redirect把一个http返回码(SUCCESS)以及返回的页面位置一起重新发给web服务器,容纳后由web服务器产生一个新的HTTP请求,就会产生一个新的线程,保存在原来Action执行的线程中的数据就无法访问。 所以,result需要包含Action的数据,那么redirect不是一个可行的办法。因为新的HTTP请求时在Servlet容器的新的线程中处理的,ActionContext中的所有状态都不会存在。
chain:功能与redirect的action转发类似,不过与redirectaction转发功能不同的是它可以将Action中的数据一直保存在同一个HTTP请求中。
posted @ 2015-10-30 17:43  郑斌blog  阅读(2316)  评论(0编辑  收藏  举报