在对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. win10 环境 gitbash 显示中文乱码问题处理

    gitbash 是 windows 环境下非常好用的命令行终端,可以模拟一下linux下的命令如ls / mkdir 等等,如果使用过程中遇到中文显示不完整或乱码的情况,多半是因为编码问题导致的,修改 ...

  2. NodeJs之child_process

    一.child_process child_process是NodeJs的重要模块.帮助我们创建多进程任务,更好的利用了计算机的多核性能. 当然也支持线程间的通信. 二.child_process的几 ...

  3. 通过ProGet搭建一个内部的Nuget服务器

    .NET Core项目完全使用Nuget 管理组件之间的依赖关系,Nuget已经成为.NET 生态系统中不可或缺的一个组件,从项目角度,将项目中各种组件的引用统统交给NuGet,添加组件/删除组件/以 ...

  4. 7.让网站支持http和https的访问方式

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#iis 怎么让网站在本地支持SSL?http://www.c ...

  5. JS与APP原生控件交互

    "热更新"."热部署"相信对于混合式开发的童鞋一定不陌生,那么APP怎么避免每次升级都要在APP应用商店发布呢?这里就用到了混合式开发的概念,对于电商网站尤其显 ...

  6. 关于Layer弹出框初探

    layer至今仍作为layui的代表作,她的受众广泛并非偶然,而是这五年多的坚持,不断完善和维护.不断建设和提升社区服务,使得猿们纷纷自发传播,乃至于成为今天的Layui最强劲的源动力.目前,laye ...

  7. 构建通用的 React 和 Node 应用

    这是一篇非常优秀的 React 教程,这篇文章对 React 组件.React Router 以及 Node 做了很好的梳理.我是 9 月份读的该文章,当时跟着教程做了一遍,收获很大.但是由于时间原因 ...

  8. “fixed+relative==absolute”——对BFC的再次思考

    好久没写博客了,刚好今天跨年夜没约到什么妹子,在家宅着不如写点东西好了. 需求 昨天晚上,给公司年会做一个移动端的投票页面,遇到一个UI优化的问题: · 正文内容少于一屏时,投票提交按钮固定显示在页面 ...

  9. 通过微信小程序看前端

    前言 2016年9月22日凌晨,微信官方通过“微信公开课”公众号发布了关于微信小程序(微信应用号)的内测通知.整个朋友圈瞬间便像炸开了锅似的,各种揣测.介绍性文章在一夜里诞生.而真正收到内测邀请的公众 ...

  10. Log4net - 项目使用的一个简单Demo

    参考页面: http://www.yuanjiaocheng.net/entity/entitytypes.html http://www.yuanjiaocheng.net/entity/entit ...