zoukankan      html  css  js  c++  java
  • struts2 result-type

    Struts2中默认的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" />
    </result-types>

    Result的首要职责,是封装Action层到View层的跳转逻辑。Struts2的Action是一个与Web容器无关的POJO。所以,在Action执行完毕之后,框架需要把代码的执行权重新交还给Web容器,并转向到相应的页面或者其他类型的View层。而这个跳转逻辑,就由Result来完成。这样,对Action屏蔽任何Web容器的相关信息,使得每个层次更加清晰。

    View层的显示类型非常多,有最常见的JSP、Freemarker/Velocity模板、Redirect到一个新的地址、文本流、图片流、甚至是JSON对象等等。所以Result层的独立存在,就能够对这些显示类型进行区分,并封装合理的跳转逻辑。

    1、dispatcher主要用于返回JSP,HTML等以页面为基础View视图,这个也是Struts2默认的Result类型。在使用dispatcher时,唯一需要指定的,是JSP或者HTML页面的位置,这个位置将被用于定位返回的页面:

    <result name="success">/index.jsp</result> 

    而Struts2本身也没有对dispatcher做出什么特殊的处理,只是简单的使用Servlet API进行forward。

    2、freemarker/velocity等指定视图类型

    <result name="success" type="freemarker">foo.ftl</result>
    <result name="success" type="velocity">
      <param name="location">foo.vm</param>
    </result>

    3、redirect重定向,源地址与目标地址之间是2个不同的HttpServletRequest。所以目标地址将无法通过ValueStack等Struts2的特性来获取源Action中的数据。如果你需要对目标地址传递参数,那么需要在目标地址url或者配置文件中指出: 

       <action name="gatherReportInfo" class="..."> 
          <result name="showReportResult" type="redirect"> 
             <param name="location">generateReport.jsp</param> 
             <param name="namespace">/genReport</param> 
             <param name="reportType">pie</param> 
             <param name="width">${width}</param> 
             <param name="height">${height}</param> 
          </result> 
       </action> 

    Redirect的Result支持在配置文件中,读取并解析源Action中ValueStack的值,并成为参数传递到Redirect的地址中。上面给出的例子中,width和height就是ValueStack中的值。 

    4、chain其实是在一个action执行完毕之后,forward到另外一个action,所以他们之间是共享HttpServletRequest的。在使用chain作为Result时,往往会配合使用ChainingInterceptor。有关ChainingInterceptor,Struts2的Reference说明了其作用:

    If you need to copy the properties from your previous Actions in the chain to the current action, you should apply the ChainingInterceptor. The Interceptor will copy the original parameters from the request, and the ValueStack is passed in to the target Action. The source Action is remembered by the ValueStack, allowing the target Action to access the properties of the preceding Action(s) using the ValueStack, and also makes these properties available to the final result of the chain, such as the JSP or Velocity page.

    也就是说,ChainingInterceptor的作用是在Action直接传递数据。事实上,源Action中ValueStack的数据会被做一次Copy,这样,2个Action中的数据都在ValueStack中,使得对于前台来说,通过ValueStack来取数据,是透明而共享的。chain这个Result有一些常用的使用情景,这点在Struts2的Reference中也有说明: 

    One common use of Action chaining is to provide lookup lists (like for a dropdown list of states). Since these Actions get put on the ValueStack, their properties will be available in the view. This functionality can also be done using the ActionTag to execute an Action from the display page. You may also use the Redirect Action Result to accomplish this. 

    比如说,一张页面中,你可能有许多数据要显示,而某些数据的获取方式可能被很多不同的页面共享(典型来说,“推荐文章”这个小栏目的数据获取,可能会被很多页面所共享)。这种情况下,可以把这部分逻辑抽取到一个独立Action中,并使用chain,将这个Action与主Action串联起来。这样,最后到达页面的时候,页面始终可以得到每个Action中的数据。

    不过chain这种Result,是在使用时需要慎重考虑的一种Result: 

    Experience shows that chaining should be used with care. If chaining is overused, an application can turn into "spaghetti code". Actions should be treated as a Transaction Script, rather than as methods in aBusiness Facade. Be sure to ask yourself why you need to chain from one Action to another. Is a navigational issue, or could the logic in Action2 be pushed back to a support class or business facade so that Action1 can call it too?
    Ideally, Action classes should be as short as possible. All the core logic should be pushed back to a support class or a business facade, so that Actions only call methods. Actions are best used as adapters, rather than as a class where coding logic is defined.

    从实战上将,使用chain作为Result也的确存在着上面所说的许多问题,所以,谨慎使用chain作为你的Result,应该成为一条最佳实践。 

    5、StreamResult等价于在Servlet中直接输出Stream流。这种Result被经常使用于输出图片、文档等二进制流到客户端。通过使用StreamResult,我们只需要在Action中准备好需要输出的InputStream即可。

    <result name="success" type="stream"> 
        <param name="contentType">image/jpeg</param> 
        <param name="inputName">imageStream</param> 
        <param name="contentDisposition">filename="document.pdf"</param> 
        <param name="bufferSize">1024</param> 
    </result> 
    package com.mycompany.webapp.actions;
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
     
    import org.apache.struts2.convention.annotation.Result;
     
    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionSupport;
     
    @Result(
        name = "success",
        type = "stream",
        params = {
            "contentType", "${type}",
            "inputName", "stream",
            "bufferSize", "1024",
            "contentDisposition", "attachment;filename="${filename}""
        }
    )
    public class FileDisplay extends ActionSupport {
     
        private String type = "image/jpeg";
        private String filename;
        private InputStream stream;
     
        public String execute() throws Exception {
     
            filename = "myimage.jpg";
            File img = new File("/path/to/image/image.jpg");
            stream = new FileInputStream(img);
     
            return Action.SUCCESS;
        }
         
        private String getType() {
            return this.type;
        }
         
        private String getFilename() {
            return this.filename;
        }
         
        private String getStream() {
            return this.stream;
        }
    }
    Convention插件

    6、XSLTResult uses XSLT to transform an action object to XML,即返回结果为xml

    <result name="success" type="xslt">
      <param name="location">foo.xslt</param>
      <param name="matchingPattern">^/result/[^/*]$</param>
      <param name="excludingPattern">.*(hugeCollection).*</param>
    </result>

    7、plainText通常用于返回原始文件内容jsp、html文件等

    <action name="displayJspRawContent" >
      <result type="plainText">
         <param name="location">/myJspFile.jsp</param>
         <param name="charSet">UTF-8</param>
      </result>
    </action>

     参见:http://struts.apache.org/release/2.3.x/docs/result-types.html

  • 相关阅读:
    R语言 which() 、 which.min() 、 which.max() 函数
    R rep() 函数
    R语言 一个向量的值分派给另一个向量
    R语言 sample抽样函数
    超参数 hyperparameters
    随机游走模型(Random Walk)
    随机数
    Lambda 函数与表达式
    static
    变量的申明定义
  • 原文地址:https://www.cnblogs.com/ikuman/p/3408776.html
Copyright © 2011-2022 走看看