Struts2 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都在里面定义了。代码如下:

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

        <!-- Deprecated name form scheduled for removal in Struts 2.1.0. The camelCase versions are preferred. See ww-1707 -->

        <result-type name="redirect-action" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>

        <result-type name="plaintext" class="org.apache.struts2.dispatcher.PlainTextResult" />

</result-types>

 

chain    
   
    用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息。    
   
    com.opensymphony.xwork2.ActionChainResult    
   
dispatcher    
   
    用来转向页面,通常处理JSP    
   
    org.apache.struts2.dispatcher.ServletDispatcherResult    
   
freemaker    
   
    处理FreeMarker模板    
   
    org.apache.struts2.views.freemarker.FreemarkerResult    
   
httpheader    
   
    控制特殊HTTP行为的结果类型    
   
    org.apache.struts2.dispatcher.HttpHeaderResult  

stream    
   
    向浏览器发送InputSream对象,通常用来处理文件下载,还可用于返回AJAX数据    
   
    org.apache.struts2.dispatcher.StreamResult    
   
velocity    
   
    处理Velocity模板    
   
    org.apache.struts2.dispatcher.VelocityResult    
   
xslt    
   
    处理XML/XLST模板    
   
    org.apache.struts2.views.xslt.XSLTResult    
   
plainText    
   
    显示原始文件内容,例如文件源代码    
   
    org.apache.struts2.dispatcher.PlainTextResult    
   
plaintext    
   
    显示原始文件内容,例如文件源代码    
   
    org.apache.struts2.dispatcher.PlainTextResult

redirect    
   
    重定向到一个URL ,被跳转的页面中丢失传递的信息,如request   
   
    org.apache.struts2.dispatcher.ServletRedirectResult    
   
redirectAction    
   
    重定向到一个Action ,跳转的页面中丢失传递的信息,如request      
   
    org.apache.struts2.dispatcher.ServletActionRedirectResult    
   
redirect-action    
   
    重定向到一个Action ,跳转的页面中丢失传递的信息,如request      
   
    org.apache.struts2.dispatcher.ServletActionRedirectResult

注:redirect与redirect-action区别

一、使用redirect需要后缀名 使用redirect-action不需要后缀名 
二、type="redirect" 的值可以转到其它命名空间下的action,而redirect-action只能转到同一命名空下的 action,因此它可以省略.action的后缀直接写action的名称。

如:

<result name="success" type="redirect">viewTask.action</result> 
<result name="success" type="redirect-action">viewTask</result>

附:redirect-action 传递参数

Xml代码
< action   name = "enterpreinfo"   class = "preinfoBusinessAction"      method = "enterPreinfoSub" >    
   < result   name = "success"   type = "redirect -action" >    
     showpreinfo? preinfo.order_number =${preinfo.order_number}&amp; preinfo.company_name =${preinfo.company_name}    
   </ result >    
< result   name = "error"   type = "redirect " >    
     < param   name = "location" > /error.jsp </ param >    
</ result >    
</ action >

   因为使用了redirect -action,所以要注意不能将 showpreinf?preinfo.order_number=${preinfo.order_number}写成 showpreinf.action?preinfo.order_number=${preinfo.order_number}

其中${}为EL表达式,获取action:enterpreinfo中 属性的值 ; 在这个配置文件里,多个参数的连接符使用了"&amp;",但XML的语法规范,应该使用"&amp;"代替"&",原理和 HTML中的转义相同,开始没有注意,在struts 分析配置文件时,总是报出这样的错误:

The reference to entity "preinfo" must end with the ';' delimiter.   
The reference to entity "preinfo" must end with the ';' delimiter.

 

 

简单说明一下result的name属性和type属性:
SUCCESS:Action正确的执行完成,返回相应的视图,success是name属性的默认值;
NONE:表示Action正确的执行完成,但并不返回任何视图;
ERROR:表示Action执行失败,返回到错误处理视图;
INPUT:Action的执行,需要从前端界面获取参数,INPUT就是代表这个参数输入的界面,一般在应用中,会对这些参数进行验证,如果验证没有通过,将自动返回到该视图;
LOGIN:Action因为用户没有登陆的原因没有正确执行,将返回该登陆视图,要求用户进行登陆验证。
dispatcher: 请求转发,底层调用RequestDispatcher的forward()或include()方法,dispatcher是 type属性的默认值,通常用于转向一个JSP,localtion指定JSP的位置,parse如果为false表示location的值不会被当作 OGNL解析,默认为true;
redirect:重定向,新页面无法显示Action中的数据,因为底层调用response.sendRedirect("")方法,无法共享请求范围内的数据,参数与dispatcher用法相同;
redirect- action:重定向到另一个Action,参数与chain用法相同,允许将原Action中的属性指定新名称带入新Action 中,可以在Result标签中添加 <param name=”b”>${a} </param>,这表示原Action中的变量a的值被转给b,下一个Action可以在值栈中使用b来操作,注意如果值是中文,需要做一 些编码处理,因为Tomcat默认是不支持URL直接传递中文的!
velocity:使用velocity模板输出结果,location指定模板的位置(*.vm),parse如果为false,location不被OGNL解析,默认为true;
xslt: 使用XSLT将结果转换为xml输出,location指定*.xslt文件的位置,parse如果为false,location不被 OGNL解析,默认为true,matchingPattern指定想要的元素模式,excludePattern指定拒绝的元素模式,支持正则表达式, 默认为接受所有元素;
httpheader:根据值栈返回自定义的HttpHeader,status指定响应状态(就是指 response.sendError(int i)重定向到500等服务器的状态页),parse如果为false,header的值不会被OGNL解析,headers,加入到header中的值, 例如: <param name=”headers.a”>HelloWorld </param>,可以加多个,这些键-值组成HashMap;
freemaker:用freemaker模板引擎呈现视图,location指定模板(*.ftl)的位置,parse如果为false,location的值不会被OGNL解析,contentType指定以何中类型解析,默认为text/html;
chain: 将action的带着原来的状态请求转发到新的action,两个action共享一个ActionContext,actionName指定转向的新的 Action的名字,method指定转向哪个方法,namespace指定新的Action的名称空间,不写表示与原Action在相同的名称空 间;skipActions指定一个使用 , 连接的Action的name组成的集合,一般不建议使用这种类型的结果;
stream:直接向 响应中发送原始数据,通常在用户下载时使用,contentType指定流的类型,默认为 text/plain,contentLength以byte计算流的长度,contentDisposition指定文件的位置,通常为 filename=”文件的位置”,input指定InputStream的名字,例如:imageStream,bufferSize指定缓冲区大小, 默认为1024字节;
plaintext:以原始文本显示JSP或者HTML,location指定文件的位置,charSet指定字符集;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值