在action的指定方法执行完毕后总会返回一个字符串,struts2根据返回的字符串去action的配置中的result去找匹配的名字,根据配置执行下一步的操作。

在ActionSupport基类中定义了五个标准的返回值

String SUCCESS       = "success";

String NONE     = "none";

String ERROR    = "error";

String INPUT    = "input";String LOGIN    = "login";
当然我们可以自己随意定义返回的名字

result元素有两个用处,首先它提供一个逻辑上名字。一个action可以单纯的返回success或input而不用理会之后的具体细节。第二,result元素提供type属性,可以不仅仅的返回一个jsp页面,而可以实现更多有意思的功能。

每个包可以有自己的默认result type,当一个result没指定type时将使用默认。正常情况下默认的result type为dispatcher。如果一个包继承另一个包,这个包可以实现自己的默认result也可以继承父包的默认result。手动指定一个默认的result如下

<result-types>
   <result-type name="dispatcher" default="true"
                class="org.apache.struts2.dispatcher.ServletDispatcherResult" />
</result-types>

同时,一个result的name属性也有默认值success。最平常的情况下,一个result如下

<result>
    /ThankYou.jsp
</result>

一个action可以有多个不同的result

<action name="Hello">
    <result>/hello/Result.jsp</result>
    <result name="error">/hello/Error.jsp</result>
    <result name="input">/hello/Input.jsp</result>
</action>
全局result

有些时候,一些result是可以为所有的action服务的,例如出错页面的result,登陆页面的result

我们可以定义一些全局result供同一个包的所有actoin共享。注意首先struts2会先搜索局部result,如果没找到则会去全局result寻找匹配的result

<global-results>
    <result name="error">/Error.jsp</result>
    <result name="invalid.token">/Error.jsp</result>
    <result name="login" type="redirectAction">Logon!input</result>
</global-results>

result 的动态参数配置

有些时候我们需要从一个action转向另一个action,但是参数却是运行才能知道,可以用一下的方法实现。下面用一个例子来说明

<struts>
....
   <package name="somePackage" namespace="/myNamespace" extends="struts-default">
      <action name="myAction" class="com.project.MyAction">
         <result name="success" type="redirectAction">otherAction?id=${id}</result>
         <result name="back" type="redirect">${redirectURL}</result>
      </action>

<action name="otherAction" class="com.project.MyOtherAction">
         ...
      </action>      
   </package>
....
</struts>

在action中必须有id,redirectURL属性以及它们的get方法

public class MyAction extends ActionSupport {
   private int id;
   private String redirectURL;
   ...
   public String execute() {
       ...
      if (someCondition) {
         this.redirectURL = "/the/target/page.action";
         return "back";
      }

this.id = 123;
      return SUCCESS; 
   }

public int getId() { return this.id; }
   public void setId(int id) { this.id = id; }
   public String getRedirectURL() { return this.redirectURL; }
   public void setRedirectURL(String redirectURL){this.redirectURL=redirectURL;}
   ...
}

如果返回success的话将会转到/<app-prefix>/myNamespace/otherAction.action?id=123

当一个result有多个参数时可以通过param子属性指定,在后面会有例子

result 的types

result有许多不同的types,用来实现不同的功能,struts2默认的types有如下几个

Dispatcher 转到一个视图页面,通常为jsp页面。这个是默认的type值。

<result>
    /ThankYou.jsp
</result>

Stream 将原始数据字节发送给浏览器,通常用于下载文件

contentType 发送给浏览器的流的mime-type (默认text/plain).

contentLength- 流的长度 (便于浏览器显示下载进度).

contentDispostion- 设置响应头contentDispostion的值(默认inline)

这个我不太清楚是什么意思,Google了一下也没什么好的解释

InputName action提供的输入流的属性名称(默认inputStream).

bufferSize从输入流写入到输出流的缓存大小(默认1024字节).

<result name="success" type="stream">
  <param name="contentType">image/jpeg</param>
  <param name="inputName">imageStream</param>
  <param name="bufferSize">1024</param>
</result>

PlainText 一般用来显示一个jsp或html页面的原始内容

<action name="displayJspRawContent" >
  <result type="plaintext">/myJspFile.jsp</result>
</action>

redirectAction 定向到另一个action 觉得没什么太大的用处,举个例子留作参考吧

<package name="public" extends="struts-default">
    <action name="login" class="...">
        <!-- Redirect to another namespace -->
        <result type="redirect-action">
            <param name="actionName">dashboard</param>
            <param name="namespace">/secure</param>
        </result>
    </action>
</package>

<package name="secure" extends="struts-default" namespace="/secure">
    <-- Redirect to an action in the same namespace -->
    <action name="dashboard" class="...">
        <result>dashboard.jsp</result>
        <result name="error" type="redirect-action">error</result>
    </action>

<action name="error" class="...">
        <result>error.jsp</result>
    </action>
</package>

<package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
   <-- Pass parameters (reportType, width and height) -->
   <!--
   The redirect-action url generated will be :
   /genReport/generateReport.action?reportType=piewidth=100height=100
   -->
   <action name="gatherReportInfo" class="...">
      <result name="showReportResult" type="redirect-action">
         <param name="actionName">generateReport</param>
         <param name="namespace">/genReport</param>
         <param name="reportType">pie</param>
         <param name="width">100</param>
         <param name="height">100</param>
      </result>
   </action>
</package>

感觉type里就这几个用处多一些,其他的几个就不写了。以后有用的时候另外再写

Struts2系列——struts2的result的更多相关文章

  1. 【SSH框架】之Struts2系列(二)

    微信公众号:compassblog 欢迎关注.转发,互相学习,共同进步! 有任何问题,请后台留言联 1.Struts2常量配置 (1).Struts2默认常量配置文件路径,如下图: (2).Strut ...

  2. 【SSH框架】之Struts2系列(一)

    微信公众号:compassblog 欢迎关注.转发,互相学习,共同进步! 有任何问题,请后台留言联系 1.Struts2框架概述 (1).什么是Struts2 Struts2是一种基于MVC模式的轻量 ...

  3. Struts2系列漏洞起始篇

    前言 到目前位置struts2的漏洞编号已经到了S2-057,一直想系统的学习下Struts2的漏洞,但由于工作量较大,一直搁浅.最近由于工作需要,借此机会来填下坑.个人认为一个框架漏洞出来了仅仅看下 ...

  4. struts2学习笔记之七:Result类型

    一:关于Struts2的type类型,也就是Result类型,他们都实现了共同的接口Result,都实现了execute方法 他们体现了策略模式,具体Result类型参见:struts-default ...

  5. struts2系列笔记(1)

    struts2框架 如果你之前在MVC模式的时候一直都是通过servlet,获取和返回数据,那么现在开始学习struts2框架, Struts是一个实现MVC设计模式的优秀的框架.它的许多优点我就不说 ...

  6. Struts2系列笔记(7)---Struts2类型转换

    Struts2类型转换      struts2中内置了大量的类型转换器用来完成数据类型转换的问题,这篇随笔主要通过两个方面来写Struts类型转换 1:Struts2内置的类型转换器 2:如何自定义 ...

  7. struts2系列(二):struts2参数传递错误、struts2的输入错误验证

    一.struts2参数传递错误 1. 基本数据类型的传递最好使用包装类,原因是struts 2.1之后使用基本数据类型如果参数为空会报错2. 日期参数的传递最好定义一个区域的属性(定义locale), ...

  8. 基于struts2注解@action的@Result跳转问题——跳转到另一个action

    初学ssh 基于注解的方式简单灵活,但是做一个例子的时候,添加用户AddUser 完成后 想页面跳转到 ListUser 这个action, 然后action 成功后 会跳转到list.jsp 显示 ...

  9. struts2(一) struts2入门

    首先推荐一本书,虽然我还没看过,但是我以后肯定会看的,<Struts+技术内幕>提取密码:kg6w .现在只是停留在会使用struts2的层次,自己也想继续深入研究,但是感觉自己的知识面还 ...

随机推荐

  1. 使用GitHub建立自己的个人主页

    1.建仓库 在自己的库里建一个hujun123qwe.github.io的库 即可以使用这个名字当网址访问. 2.写内容 在库里建一个首页文件 index.html 这个个人主页只支持静态的内容,像p ...

  2. UML类图关系大全-转

    1.关联 双向关联: C1-C2:指双方都知道对方的存在,都可以调用对方的公共属性和方法. 在GOF的设计模式书上是这样描述的:虽然在分析阶段这种关系是适用的,但我们觉得它对于描述设计模式内的类关系来 ...

  3. 【DP/二分】BZOJ 1863:[Zjoi2006]trouble 皇帝的烦恼

    863: [Zjoi2006]trouble 皇帝的烦恼 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 465  Solved: 240[Submit][ ...

  4. GhostDoc:生成.NET API文档的工具 (帮忙文档)

    在 Sandcastle:生成.NET API文档的工具 (帮忙文档) 后提供另一个生成API文档的工具.   1) 准备工作 安装GhostDoc Proc. 收费的哦.... 这个工具的优势是不像 ...

  5. synergy--共享你的键鼠

    synergy用来分享一套键鼠,作用于多台主机.可作用于linux.Windows和mac平台.工作方式是:将一台主机作为服务器端,然后服务器端将会分享自己的键鼠,另一台主机作为客户端连接服务端就可以 ...

  6. HTTP/2 对 Web 性能的影响(上)

    一.前言 HTTP/2 于 2015 年 5 月正式推出.自诞生以来,它就一直在影响着网络性能最佳实践.在本篇文章中,我们将讨论 HTTP/2 的二进制帧.延迟削减.潜在利弊以及相应的应对措施. 超文 ...

  7. 谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持

    谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持 在本篇文章上一部分Order Processing的例 ...

  8. Lua 简单的IO交互 和迷宫代码

    function room1 () print("in room1") local move = io.read() if move == "south" th ...

  9. hdoj 2112 HDU Today

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=2112 分析:多了一个地方的条件,用map来映射地点编号,Dijkstra求解即可 //2013-10- ...

  10. POJ 1456 Supermarket(贪心+并查集优化)

    一开始思路弄错了,刚开始想的时候误把所有截止时间为2的不一定一定要在2的时候买,而是可以在1的时候买. 举个例子: 50 2  10 1   20 2   10 1    50+20 50 2  40 ...