struts2配置文件中result返回类型

    1.     <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>  
    2.     <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>  
    3.     <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>  
    4.     <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>  
    5.     <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>  
    6.     <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>  
    7.     <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>  
    8.     <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>  
    9.     <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>  
    10.     <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />  

<result name="success" type="dispatcher"> 
    <param name="location">/ThankYou.jsp</param> 
</result> 
由于type默认值是dispatcher,所以不需要定义,另外name的默认值为success。 
上述代码可以简写为: 
<result> 
    <param name="location">/ThankYou.jsp</param> 
</result> 
另外location参数也可以直接卸载result标签内部,所以上述代码的最简单的写法为: 
<result>/ThankYou.jsp</result> 

1.chain

该类型是请求转发给其他的action,如果为jsp则会报错

需要注意的就是与redirect的区别,请求转发是还在当前请求,

而redirect会响应一次浏览器然后浏览器再根据响应请求重定向的资源,注意看url的变化就明白了!

     
     
  1. <package name="public" extends="struts-default">  
  2.     <!-- Chain creatAccount to login, using the default parameter -->  
  3.     <action name="createAccount" class="...">  
  4.         <result type="chain">login</result>  
  5.     </action>  
  6.   
  7.     <action name="login" class="...">  
  8.         <!-- Chain to another namespace -->  
  9.         <result type="chain">  
  10.             <param name="actionName">dashboard</param>  
  11.             <param name="namespace">/secure</param>  
  12.         </result>  
  13.     </action>  
  14. </package>   
2.dispatcher
请求转发视图资源,比如jsp,html
  
  
  1. <result name="success" type="dispatcher">  
  2.   <param name="location">foo.jsp</param>  
  3. </result>  
3.httpheader
可以随便更改响应状态  参考此链接
  
  
  1. <action name="test" class="com.iss.action.TestAction">  
  2.         <result name="success" type="httpheader">  
  3.         <param name="status">400</param>  
  4.     </result>  
  5.     </action>  
4.redirect

重定向操作,让客户端请求另外的网络资源,可以为action,也可以为视图资源

  1. <result name="success" type="redirect">  
  2.   <param name="location">foo.jsp</param>  
  3.   <param name="parse">false</param>  
  4. </result>  
  5. <!-- 
    重定向 
    参数:location:重定向的目的地
         parse:   表明是否把location参数的值视为一个OGNL表达式来解释,默认值为true
    -->
    <action name="..." class="...">
      <result name="success" type="redirect">
        <!-- 内部资源 -->
        /jsp/Product.jsp
        <!-- Action带动态参数(${userName}值为本Action中的userName属性值) -->
        UserAction.action?userName=${userName}
        <!-- 
         外部资源(如果需要使用&和+之类的特殊字符必须使用转义序列.如:&改成&amp;) 
         http://www.google.com?user=1&site=4
         转成:http://www.google.com?user=1&amp;site=4
        -->
        http://www.google.com
      </result>
    </action>

5.redirectAction

重定向至Action,与redirect的区别是redirectAction只能请求action,至于其他的未知

  1. <!-- 
    重定向到一个Action 
    参数:actionName:指定重定向Action的名字
         namespace: 指定重定向Action的命名空间(没有此参数,与本action同一个命名空间)
    -->
  2. <package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">  
  3.    <-- Pass parameters (reportType, width and height) -->  
  4.    <!--  
  5.    The redirect-action url generated will be :  
  6.    /genReport/generateReport.action?reportType=pie&width=100&height=100  
  7.    -->  
  8.    <action name="gatherReportInfo" class="...">  
  9.       <result name="showReportResult" type="redirect-action">  
  10.          <param name="actionName">generateReport</param>  
  11.          <param name="namespace">/genReport</param>  
  12.          <param name="reportType">pie</param>  
  13.          <param name="width">100</param>  
  14.          <param name="height">100</param>  
  15.       </result>  
  16.    </action>  
  17. </package>  
6.stream

这个返回类型主要用作下载文件或者在浏览器上显示PDF等文档

此处给一个显示PDF文档示例

项目web.xml中

  1. <mime-mapping>    
  2.       <extension>pdf</extension>    
  3.       <mime-type>application/pdf</mime-type>    
  4.   </mime-mapping>    
struts.xml中
  1. <action name="test" class="com.iss.action.TestAction">  
  2.             <result name="success" type="stream">  
  3.                 <param name="contentType">application/pdf</param>  
  4.                 <param name="inputName">inputStream</param>  
  5.                 <param name="contentDisposition">filename="a.pdf"</param>  
  6.             </result>  
  7.         </action>  
TestAction.java中
(需要注意的地方是struts.xml中inputName的值要与TestAction中的get方法名相关,如在此处是getInputStream,inputName则为inputStream)
  1. public class TestAction extends ActionSupport  
  2. {  
  3.       
  4.     private InputStream inputStream;  
  5.   
  6.   
  7.     public InputStream getInputStream()  
  8.     {  
  9.         return inputStream;  
  10.     }  
  11.   
  12.     @Override  
  13.     public String execute() throws Exception  
  14.     {  
  15.         inputStream = new FileInputStream("xxxxx/a.pdf");//要下载或显示的文件路径,最好是工程底下的,至于其他的文件好像下载不下来, 
  16.         return SUCCESS;  
  17.     }  

7.plainText

响应以plain形式返回给客户端

<!-- 通常被用来发送JSP页面的源代码 -->
<action name="source_show" class="...">
   <result name="success" type="plaintext">/jsp/Menu.jsp</result>
</action>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值