菜鸟学Struts2——Results
在对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的更多相关文章
- 菜鸟学Struts2——零配置(Convention )
又是周末,继续Struts2的学习,之前学习了,Struts的原理,Actions以及Results,今天对对Struts的Convention Plugin进行学习,如下图: Struts Conv ...
- 菜鸟学Struts2——Interceptors
昨天学习Struts2的Convention plugin,今天利用Convention plugin进行Interceptor学习,虽然是使用Convention plugin进行零配置开发,这只是 ...
- 菜鸟学Struts2——Actions
在对Struts2的工作原理学习之后,对Struts2的Action进行学习.主要对Struts2文档Guides中的Action分支进行学习,如下图: 1.Model Driven(模型驱动) St ...
- 菜鸟学Struts2——Struts工作原理
在完成Struts2的HelloWorld后,对Struts2的工作原理进行学习.Struts2框架可以按照模块来划分为Servlet Filters,Struts核心模块,拦截器和用户实现部分,其中 ...
- 菜鸟学Struts2——HelloWorld
写在前面 自从工作后就过上了只有一个月记忆的生活,太健忘,很多学过的东西因为用得少便忘记了,第二次学习struts,为了以后便于查阅,开始自己的博客之旅.Struts的学习还是从Hello World ...
- 菜鸟学自动化测试(八)----selenium 2.0环境搭建(基于maven)
菜鸟学自动化测试(八)----selenium 2.0环境搭建(基于maven) 2012-02-04 13:11 by 虫师, 11419 阅读, 5 评论, 收藏, 编辑 之前我就讲过一种方试来搭 ...
- 菜鸟学IT之四则运算升级版
菜鸟学IT之四则运算升级版 本次作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2213 团队代码github远程仓库的 ...
- 菜鸟学Java(十五)——Java反射机制(二)
上一篇博文<菜鸟学编程(九)——Java反射机制(一)>里面,向大家介绍了什么是Java的反射机制,以及Java的反射机制有什么用.上一篇比较偏重理论,理论的东西给人讲出来总感觉虚无缥缈, ...
- 菜鸟学Java(十四)——Java反射机制(一)
说到反射,相信有过编程经验的人都不会陌生.反射机制让Java变得更加的灵活.反射机制在Java的众多特性中是非常重要的一个.下面就让我们一点一点了解它是怎么一回事. 什么是反射 在运行状态中,对于任意 ...
随机推荐
- MySQL高级知识- MySQL的架构介绍
[TOC] 1.MySQL 简介 概述 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,将数据保存在不同的表中,而 ...
- Gradle配置APK自动签名完整流程
转载请注明出处:http://www.cnblogs.com/LT5505/p/6256683.html 一.生成签名 1.命令行生成签名,输入命令keytool -genkey -v -keysto ...
- Matlab数值计算示例: 牛顿插值法、LU分解法、拉格朗日插值法、牛顿插值法
本文源于一次课题作业,部分自己写的,部分借用了网上的demo 牛顿迭代法(1) x=1:0.01:2; y=x.^3-x.^2+sin(x)-1; plot(x,y,'linewidth',2);gr ...
- 装饰者模式 Decoration
1.什么是装饰者模式 动态给对象增加功能,从一个对象的外部来给对象添加功能,相当于改变了对象的外观,比用继承的方式更加的灵活.当使用装饰后,从外部系统的角度看,就不再是原来的那个对象了,而是使用一系列 ...
- BPM配置故事之案例11-操作外部数据源
小明:可以获取ERP数据了-- 老李:哦,这么快?小伙子,我非常看好你,来来,别急着走,再陪我聊会-- 小明:--您老人家不是又要改流程吧? 老李:没有没有,哎嘿嘿嘿,我们这不都是为公司效率着想嘛,这 ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- VS2015墙内创建ionic2
开始学习ionic2,试验各种方法,感觉以下是紧跟rc版本的最佳方案 STEP1 设置cnpm npm install -g cnpm --registry=https://registry.npm. ...
- React Native环境配置之Windows版本搭建
接近年底了,回想这一年都做了啥,学习了啥,然后突然发现,这一年买了不少书,看是看了,就没有完整看完的.悲催. 然后,最近项目也不是很紧了,所以抽空学习了H5.自学啃书还是很无趣的,虽然Head Fir ...
- [Unity3D]利用Raycast实现物体的选择与操作
本文系作者原创 转载请注明出处 如果是一个2D的平面项目或者说需要在三维空间选择一个物体时(经常表现为抓取物件),我们需要用到Raycast事件 那么首先先说说什么是Raycast 按照字面上来理解的 ...
- osi(open system interconnection)模型的通俗理解
OSI模型的理解: 以你和你女朋友以书信的方式进行通信为例. 1.物理层:运输工具,比如火车.汽车. 2.数据链路层:相当于货物核对单,表明里面有些什么东西,接受的时候确认一下是否正确(CRC检验). ...