在对Struts2的Action学习之后,对Struts2的Result进行学习。主要对Struts2文档Guides中的Results分支进行学习,如下图:

1、Result Types(Result类型)

Struts2的Action处理用户请求后返回一个字符串,在struts.xml配置文件中进行字符串与实际视图的映射,在配置文件中是通过action的子元素result来完成此功能的,result的格式如下:

 <result name="字符串" type="视图类型">实际视图</result>

Struts2的result类型主要有以下几种:

对以上Result Type几种常见的进行学习,其中Freemarker、Velocity、XSL、JSON等几个暂不学习。

(1)Chain Result

这个在对Action进行学习的时候已经使用过,Chain Result有4个参数,actionName (default) 、namespace 、method 、skipActions ,关于Chain Result的学习已经在 菜鸟学习Struts2——Actions 记录。配置列子如下:

 <result type="chain">
<param name="actionName">chain</param>
<param name="namespace">/chain</param>
</result>

(2)Dispatcher Result

Dispatcher Result 负责转发到指定的JSP页面,效果跟<jsp:forword page=".."/>是一样的,这是Result默认的类型,所以可以在result中指定<result name="success">a.jsp</result>而不用指定其类型。Dispatcher Result有两个参数配置location (default) 、parse ,其中location执行jsp所在的位置,parse默认是true,如果将parse设置成false,则localtion的Url中挂着的参数将不会被解析到OGNL值栈中,配置列子如下:

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

(3)HttpHeader Result

HttpHeader Result允许开发自定义返回http header的参数或者返回一个错误信息,HttpHeader Result有5个参数status、parse(跟dispatcher的parse一样)、headers 、error、errorMessage ,配置列子如下:

 <result name="success" type="httpheader">
<param name="status">204</param>
<param name="headers.a">a custom header value</param>
<param name="headers.b">another custom header value</param>
</result> <result name="proxyRequired" type="httpheader">
<param name="error">305</param>
<param name="errorMessage">this action must be accessed through a proxy</param>
</result>

(4)Redirect Result

Redirect Result跟response.sendRedirect("")的效果是一致的,也就是重定向,页面重定向是一个新的请求,上一个request中的值都将消息,如果需要在重定向之后保持上一次请求的值,那个可以将上一次request的值放到session中,或者在result中配置param。 Redirect Result有3个参数location (default) 、parse (跟dispatcher的parse一样)、anchor(指定该值则会出现在url中,如http://www.xx.com#anchor),配置例子如下:

 <result name="success" type="redirect">
<param name="location">foo.jsp</param>
<param name="parse">false</param>
<param name="anchor">FRAGMENT</param>
</result>

上面的列子最后的url将会是:foo.jsp#FRAGMENT

 <result name="showReportResult" type="redirect">
<param name="location">generateReport.jsp</param>
<param name="reportType">pie</param>
<param name="width">100</param>
<param name="height">100</param>
<param name="parse">false</param>
<param name="anchor">summary</param>
</result>

上面的列子最后的url将会是:generateReport.jsp?reportType=pie&width=100&height=100#summary

(5)Redirect Action Result

Redirect Action Result与Redirect Result有点一样,Redirect通过location指定重定向的url,而Redirect Action Result通过actionName和namespace指定重定向的Url,Redirect Result不会将空值的param挂在Url上,但Redirect Action Result可以通过配置suppressEmptyParameters决定是否将空值的param挂在Url上(但是测试过程中,不管是将suppressEmptyParameters设置成为false,还是设置成为true,都不会将控制的param挂着在Url上,chrome浏览器环境!!!  后续在研究研究,还是自己理解错了??),Redirect Action Result 有5个参数配置actionName (default) 、namespace 、suppressEmptyParameters、parse 、anchor,配置列子如下:

 <result name="redirect" type="redirectAction">
<param name="actionName">result-target</param>
<param name="width"></param>
<param name="height">100</param>
<param name="suppressEmptyParameters">false</param>
</result>

(6)Stream Result

Stream Result用于返回一个InputStream,原始数据直接传递给HttpServletResponse,可以用于文件下载等,Stream Result有7个参数contentType 、contentLength、contentDisposition、inputName 、bufferSize 、allowCaching 、contentCharSet 。完整的列子如下:

 <!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="result" extends="struts-default" namespace="/result">
<action name="result-*" class="yaolin.core.action.ResultAction" method="{1}">
<result name="stream" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">stream</param>
<param name="contentDisposition">attachment;filename="yaolin.jpg"</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
 package yaolin.core.action;

 import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream; public class ResultAction { // 文件流
private InputStream stream;
// Stream Result
public String stream() throws Exception {
File img = new File("E:/yaolin.jpg");
stream = new FileInputStream(img);
return "stream";
} // getter and setter
public InputStream getStream() {
return stream;
}
}

(7)PlainText Result

PlainText Result是直接将JSP/HTML中的源代码内容输入到页面中,PlaintText Result有两个参数location、charset。配置例子如下:

 <result name="text" type="plainText">
<param name="location">/index.jsp</param>
<param name="charset">utf-8</param>
</result>

2、DispatcherListener

以下是官网DOC给的例子,暂时没搞明白怎么用?? 后续研究一下。

 //Use a DispatcherListener object to execute code when a Dispatcher is initalized or destroyed. A DispatcherListener is an easy way to associate customizable components like a ConfigurationManager with a Dispatcher.
static {
Dispatcher.addDispatcherListener(new DispatcherListener() {
public void dispatcherInitialized(Dispatcher du) {
// do something to Dispatcher after it is initialized eg.
du.setConfigurationManager(....);
} public void dispatcherDestroyed(Dispatcher du) {
// do some cleanup after Dispatcher is destroyed.
}
});
}

3、PreResultListener

PreResultListener暂时没有想到实际的用法,后续研究一下,以下是DOC中的说明&列子:

描述:A PreResultListener can affect an action invocation between the interceptor/action phase and the result phase. Typical uses include switching to a different Result or somehow modifying the Result or Action objects before the Result executes.

例子:

 // 在Action中使用
public class MyAction extends ActionSupport {
...
public String execute() throws Exception {
ActionInvocation invocation = ActionContext.getContext().getActionInvocation();
invocation.addPreResultListener(new PreResultListener() {
public void beforeResult(ActionInvocation invocation,
String resultCode) {
// perform operation necessary before Result execution
}
});
}
...
}
// 在拦截器中使用
public class MyInterceptor extends AbstractInterceptor {
...
public String intercept(ActionInvocation invocation) throws Exception {
invocation.addPreResultListener(new PreResultListener() {
public void beforeResult(ActionInvocation invocation,
String resultCode) {
// perform operation necessary before Result execution
}
});
}
...
}

对于Result这一块仍有DispatcherListener、PreResultListener需要进行研究。

未完,待续。

菜鸟学Struts2——Results的更多相关文章

  1. 菜鸟学Struts2——零配置(Convention )

    又是周末,继续Struts2的学习,之前学习了,Struts的原理,Actions以及Results,今天对对Struts的Convention Plugin进行学习,如下图: Struts Conv ...

  2. 菜鸟学Struts2——Interceptors

    昨天学习Struts2的Convention plugin,今天利用Convention plugin进行Interceptor学习,虽然是使用Convention plugin进行零配置开发,这只是 ...

  3. 菜鸟学Struts2——Actions

    在对Struts2的工作原理学习之后,对Struts2的Action进行学习.主要对Struts2文档Guides中的Action分支进行学习,如下图: 1.Model Driven(模型驱动) St ...

  4. 菜鸟学Struts2——Struts工作原理

    在完成Struts2的HelloWorld后,对Struts2的工作原理进行学习.Struts2框架可以按照模块来划分为Servlet Filters,Struts核心模块,拦截器和用户实现部分,其中 ...

  5. 菜鸟学Struts2——HelloWorld

    写在前面 自从工作后就过上了只有一个月记忆的生活,太健忘,很多学过的东西因为用得少便忘记了,第二次学习struts,为了以后便于查阅,开始自己的博客之旅.Struts的学习还是从Hello World ...

  6. 菜鸟学自动化测试(八)----selenium 2.0环境搭建(基于maven)

    菜鸟学自动化测试(八)----selenium 2.0环境搭建(基于maven) 2012-02-04 13:11 by 虫师, 11419 阅读, 5 评论, 收藏, 编辑 之前我就讲过一种方试来搭 ...

  7. 菜鸟学IT之四则运算升级版

     菜鸟学IT之四则运算升级版 本次作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2213 团队代码github远程仓库的 ...

  8. 菜鸟学Java(十五)——Java反射机制(二)

    上一篇博文<菜鸟学编程(九)——Java反射机制(一)>里面,向大家介绍了什么是Java的反射机制,以及Java的反射机制有什么用.上一篇比较偏重理论,理论的东西给人讲出来总感觉虚无缥缈, ...

  9. 菜鸟学Java(十四)——Java反射机制(一)

    说到反射,相信有过编程经验的人都不会陌生.反射机制让Java变得更加的灵活.反射机制在Java的众多特性中是非常重要的一个.下面就让我们一点一点了解它是怎么一回事. 什么是反射 在运行状态中,对于任意 ...

随机推荐

  1. Asp.net Boilerplate之AbpSession扩展

    当前Abp版本1.2,项目类型为MVC5. 以属性的形式扩展AbpSession,并在"记住我"后,下次自动登录也能获取到扩展属性的值,版权归"角落的白板报"所 ...

  2. JS里面Data日期格式转换

    var format = function(time, format){     var t = new Date(time);     var tf = function(i){return (i  ...

  3. JavaScript实现常用的排序算法

    ▓▓▓▓▓▓ 大致介绍 由于最近要考试复习,所以学习js的时间少了 -_-||,考试完还会继续的努力学习,这次用原生的JavaScript实现以前学习的常用的排序算法,有冒泡排序.快速排序.直接插入排 ...

  4. [.NET] 利用 async & await 的异步编程

    利用 async & await 的异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/5922573.html  目录 异步编程的简介 异 ...

  5. js参数arguments的理解

    原文地址:js参数arguments的理解 对于函数的参数而言,如下例子 function say(name, msg){ alert(name + 'say' + msg); } say('xiao ...

  6. TabLayout + ViewPager

    一.实现思路 1.在build.gradle中添加依赖,例如: compile 'com.android.support:support-v4:23.4.0'compile 'com.android. ...

  7. JavaScript中undefined与null的区别

    通常情况下, 当我们试图访问某个不存在的或者没有赋值的变量时,就会得到一个undefined值.Javascript会自动将声明是没有进行初始化的变量设为undifined. 如果一个变量根本不存在会 ...

  8. ES6+ 现在就用系列(二):let 命令

    系列目录 ES6+ 现在就用系列(一):为什么使用ES6+ ES6+ 现在就用系列(二):let 命令 ES6+ 现在就用系列(三):const 命令 ES6+ 现在就用系列(四):箭头函数 => ...

  9. 在 Linux 中安装 Oracle JDK 8 以及 JVM 的类加载机制

    参考资料 该文中的内容来源于 Oracle 的官方文档 Java SE Tools Reference .Oracle 在 Java 方面的文档是非常完善的.对 Java 8 感兴趣的朋友,可以直接找 ...

  10. 【Java并发编程实战】-----“J.U.C”:CLH队列锁

    在前面介绍的几篇博客中总是提到CLH队列,在AQS中CLH队列是维护一组线程的严格按照FIFO的队列.他能够确保无饥饿,严格的先来先服务的公平性.下图是CLH队列节点的示意图: 在CLH队列的节点QN ...