Struts2中 Result类型配置详解
一个result代表了一个可能的输出。当Action类的方法执行完成时,它返回一个字符串类型的结果码,框架根据这个结果码选择对应的result,向用户输出。
在com.opensymphony.xwork2.Action接口中定义了一组标准的结果代码,可供开发人员使用,当然了只有我们的action继承ActionSupport这个类才可以使用下面的结果代码,如下所示:
public interface Action
{
publicstatic final String SUCCESS = “success”;
publicstatic final String NONE = “none”;
publicstatic final String ERROR = “error”;
publicstatic final String INPUT = “input”;
publicstatic final String LOGIN = “login”;
}
其中 Struts2应用在运行过程中若发现addFieldError()中有信息或者类型转换失败或着输入校验失败等情况
那么它会自动跳转到name为input的<result/>,然后转到INPUT所对应的页面
若JSP页面中表单是用普通<form>编写的,发生错误而返回该页面时,则原数据将消失
若JSP页面中表单是用<s:form/>编写的,发生错误而返回该页面时,则原数据仍存在
若没有提供name值为input的<result/>,那么发生错误时,将直接在浏览器中提示404错误
除了这些预定义的结果码外,开发人员也可以定义其它的结果码来满足自身应用程序的需
要。
Result配置由两部分组成:一部分是result映射,另一部分是result类型。下面我们分别对
这两部分进行介绍。
一、配置 result映射
在result映射的配置中,在指定实际资源的位置时,可以使用绝对路径,也可以使用相对路径。
绝对路径以斜杠(/)开头,相对于当前的Web应用程序的上下文路径;
相对路径不以斜杠(/)开头,相对于当前执行的action的路径,也就是namespace指定的路径。
例如:
<package name="default"extends="struts-default" namespace="/admin">
<action name="login"class="com.ibm.LoginAction">
<result>success.jsp</result>
<resultname="error">/error.jsp</result>
</action>
</package>
如果当前Web应用程序的上下文路径是/Shop,那么请求/Shop/admin/login.action,执行成功后,转向的页面路径为:/Shop/admin/success.jsp;执行失败后,转向的页面路径为/Shop/error.jsp.
二、result结果类型详解
type 所有类型 :(在struts2-core.jar/struts-default.xml中可以找到)
Type类型值 |
作用说明 |
对应类 |
chain |
用来处理Action 链 |
com.opensymphony.xwork2.ActionChainResult |
dispatcher(默认值) |
用来转向页面,通常处理 JSP |
org.apache.struts2.dispatcher.ServletDispatcherResult |
redirect |
重定向到一个URL |
org.apache.struts2.dispatcher.ServletRedirectResult |
redirectAction |
重定向到一个 Action |
org.apache.struts2.dispatcher.ServletActionRedirectResult |
plainText |
显示源文件内容,如文件源码 |
org.apache.struts2.dispatcher.PlainTextResult |
freemarker |
处理 FreeMarker 模板 |
org.apache.struts2.views.freemarker.FreemarkerResult |
httpheader |
控制特殊 http 行为的结果类型 |
org.apache.struts2.dispatcher.HttpHeaderResult |
stream |
向浏览器发送 InputSream 对象,通常用来处理文件下载,还可用于返回 AJAX 数据。 |
org.apache.struts2.dispatcher.StreamResult |
velocity |
处理 Velocity 模板 |
org.apache.struts2.dispatcher.VelocityResult |
xslt |
处理 XML/XLST 模板 |
org.apache.struts2.views.xslt.XSLTResult |
1、dispatcher结果类型
Struts2在后台使用Servlet API的RequestDispatcher来转发请求,因此在用户的整个请求/响应过程中,目标Servlet/JSP接收到的request/response对象,与最初的Servlet/JSP相同。
Dispatcher结果类型的实现是org.apache.struts2.dispatcher.ServletDispatcherResult,该类的二个属性(property):location和parse,这两个属性可以通过struts.xml配置文件中的result元素的param子元素来设置。param元素的name属性指定结果类型实现类的属性名,param元素的内容是属性的值。例如:
<resultname=“success” type=“dispatcher”>
<param name=“location”>/success.jsp</param>
<param name=“parse”>true</param>
</result>
说明:
A、location参数用于指定action执行完毕后要转向的目标资源,parse属性是一个布尔类型的值,如果为true,则解析location参数中的OGNL表达式;如果为false,则不解析。parse属性的默认值就是true.
location参数是默认的参数,在所有的Result实现类中,都定义了一个字符串类型的DEFAULT_PARAM静态常量,专门用于指定默认的参数名。DEFAULT_PARAM常量的定义:public static final StringDEFAULT_PARAM=“location”;
B、在设置location参数时,可以在参数值中使用OGNL表达式。
<action name=“viewNews”class=“com.ibm.ViewNewsAction”
<result name=“success” type=“dispatcher”>
<!-- 如果参数是中文:请参看最底部例子-->
<param name=“location”>/viewNews.jsp?id=${id}</param>
<param name=“parse”>true</param>
</result>
</action>
考虑到默认值的使用(dispatcher和location都是默认值),上述可以简化为:
<action name=“viewNews”class=“com.ibm.ViewNewsAction”>
<result name=“success”>viewNews.jsp?id=${id}</result>
</action>
2、redirect结果类型(重定向到一个Url,也可以是Action或一个页面)
Redirect结果类型在后台使用HttpServletResponse的sendRedirect方法将请求重定向到指定的URL,它的实现类是org.apache.struts2.dispatcher.ServletRedirectResult.该 类同样有二个属性(property):location和parse,在使用redirect结果类型的场景中,用户要完成一次与服务器之间的交互,浏览器需要完成两次请求,因此第一次请求中的数据在第二次请求中是不可用的,这意味在目标资源中是不能访问action实例、action错误以及错误等。
如果有某些数据需要在目标资源中访问,
i、一种方式是将数据保存到Session中,
ii、另一种方式是通过请求参数来传递数据。
示例(1)、<result name="success" type="redirect">
<paramname="location">foo.jsp</param>
<paramname="parse">false</param><!--不解析OGNL-->
</result>
示例(2)、
<packagename="passingRequestParameters"extends="struts-default"namespace="/passingRequestParameters">
<-- Passparameters (reportType, widthand height),重定向到Url并且传参 ,如果参数是中文:请参看最底部例子-->
<!--
The redirect-action url generated will be:
/genReport/generateReport.jsp?reportType=pie&width=100&height=100
-->
<actionname="gatherReportInfo"class="...">
<resultname="showReportResult"type="redirect">
<paramname="location">generateReport.jsp</param>
<paramname="namespace">/genReport</param>
<paramname="reportType">pie</param>
<paramname="width">100</param>
<paramname="height">100</param>
</result>
</action>
</package>
3、redirectAction结果类型(重定向到一个Action)
他经常用于防止表单重复提交,比方说在增加完用户之后要显示列表
redirectAction结果类型的实现类是org.apache.struts2.dispatcher.ServletActionRedirectResult,该类是ServletRedirectResult的子类,因此我们也就可以判断出redirectAction结果类型和redirect结果类型的后台工作原理是一样的,即都是利用HttpServletResponse的sendRedirect方法将请求重定向到指定的URL。
示例、
<packagename="public"extends="struts-default">
<actionname="login"class="...">
<!--Redirect to anothernamespace重定向到不同命名空间下的 action -->
<resulttype="redirectAction">
<param name="actionName">dashboard</param>
<param name="namespace">/secure</param>
</result>
</action>
</package>
<package name="secure"extends="struts-default"namespace="/secure">
<-- Redirectto an action in the samenamespace,重定向到同一命名空间下的action -->
<action name="dashboard"class="...">
<result>dashboard.jsp</result>
<resultname="error"type="redirectAction">error</result>
</action>
<action name="error"class="...">
<result>error.jsp</result>
</action>
</package>
<packagename="passingRequestParameters"extends="struts-default"namespace="/passingRequestParameters">
<-- Passparameters (reportType, width andheight),重定向到Action并且传参 ,如果参数是中文:请参看最底部例子-->
<!--
TheredirectAction urlgenerated will be:
/genReport/generateReport.action?reportType=pie&width=100&height=100
-->
<actionname="gatherReportInfo"class="...">
<resultname="showReportResult" type="redirectAction">
<paramname="actionName">generateReport</param>
<paramname="namespace">/genReport</param>
<paramname="reportType">pie</param>
<paramname="width">100</param>
<paramname="height">100</param>
<paramname="empty"></param>
<paramname="supressEmptyParameters">true</param>
</result>
</action>
</package>
4、链接类型 result:chain(从一个Action转发到另一个Action)
chain结果类型有4个属性,分别是:
actionName (default) - the name of the action that will bechained to
namespace - used to determine which namespace the Action isin that we're chaining. If namespace is null, this defaults to thecurrent namespace
method - used to specify another method on target action tobe invoked. If null, this defaults to execute method
skipActions - (optional) the list of comma separated actionnames for the actions that could be chained to
示例、
<packagename="public"extends="struts-default">
<!-- ChaincreatAccount to login, using the default parameter,链接到同一命名空间下的Action,-->
<action name="createAccount"class="...">
<resulttype="chain">login</result>
</action>
<actionname="login"class="...">
<!--Chain to anothernamespace -->
<resulttype="chain">
<paramname="actionName">dashboard</param>
<paramname="namespace">/secure</param>
</result>
</action>
</package>
<package name="secure"extends="struts-default"namespace="/secure">
<actionname="dashboard"class="...">
<result>dashboard.jsp</result>
</action>
</package>
5、HttpHeaderResult: HttpHeader(用来控制特殊的Http行为)
httpheader结果类型很少使用到,它实际上是返回一个HTTP响应的头信息
示例:<resultname="success"type="httpheader">
<paramname="status">204</param>
<paramname="headers.a">a customheadervalue</param> <paramname="headers.b">another customheadervalue</param>
</result>
<resultname="proxyRequired"type="httpheader">
<paramname="error">305</param>
<paramname="errorMessage">this actionmust be accessed throughaprozy</param>
</result>
6、StreamResult(向浏览器发送InputSream对象,通常用来处理文件下载)
<resultname="success"type="stream">
<paramname="contentType">image/jpeg</param>
<paramname="inputName">imageStream</param>
<paramname="contentDisposition">attachment;filename="document.pdf"</param>
<paramname="bufferSize">1024</param>
</result>
7、PlainTextResult( 显示原始文件内容,例如文件源代码)
<actionname="displayJspRawContent">
<resulttype="plaintext">/myJspFile.jsp</result>
</action>
<actionname="displayJspRawContent">
<resulttype="plaintext">
<paramname="location">/myJspFile.jsp</param>
<paramname="charSet">UTF-8</param>
</result>
</action>
若仅设置type="plainText"的话,页面中显示中文时会乱码,这时就可以借助它的charSet属性以解决中文显示时的乱码问题,如果不设置charSet属性,反而去配置struts.i18n.encoding全局属性,是不能解决问题的
设置charSet属性的目的就是让JSP页面的编码与明文显示时的编码一致
8、VelocityResult(处理Velocity模板)
<resultname="success"type="velocity">
<paramname="location">foo.vm</param>
</result>
9、XLSResult(处理XML/XLST模板)
<resultname="success"type="xslt">
<paramname="location">foo.xslt</param>
<paramname="matchingPattern">^/result/[^/*]$</param>
<paramname="excludingPattern">.*(hugeCollection).*</param>
</result>
10、FreeMarkerResult (处理FreeMarker模板)
<resultname="success"type="freemarker">foo.ftl</result>
附、另外第三方的Result类型还包括JasperReportsPlugin,专门用来处理JasperReport类型的报表输出。
<%@ tagliburi="http://tiles.apache.org/tags-tiles"prefix="tiles"%>
<%@ taglib prefix="s"uri="/struts-tags"%>
<%-- Show usage; Used in Header--%>
<tiles:importAttributename="title"scope="request"/>
<html>
<head><title><tiles:getAsStringname="title"/></title></head>
<body>
<tiles:insertAttributename="header"/>
<pid="body">
<tiles:insertAttributename="body"/>
</p>
<p>Noticethat this is a layout madein JSP</p>
</body>
</html>
注意!!!!.传递中文
记住永远不要在浏览器的地址栏中传递中文。在传递中文前先进行编码
A.action中
public class User extendsActionSupport{
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated methodstub
username=URLEncoder.encode("郭蕾","utf-8");//先进行编码
System.out.println(username);
return "redirect";
}
}
B.struts.xml中
<action name="redirect"class="action.User">
<result type="redirect"name="redirect">
/redirect.jsp?username=${username}//如果要传递两个参数,中间用&代替&</result>
</action>
在这里使用了类似于el表达式的方式传值,${username}其中username为action中定义的
C.redirect.jsp中
<body>
重定向
<%Strings=request.getParameter("username");
s=newString(s.getBytes("iso8859-1"),"utf-8");
s=URLDecoder.decode(s,"utf-8");
out.println(s);
%>
</body>
重定向中传递中文先进行编码,在jsp页面中先接受参数,然后对其进行字节分解,然后进行解码。
Struts2中 Result类型配置详解相关推荐
- struts2中的constant配置详解
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...
- Ehcache 中ehcache.xml 配置详解和示例
EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存 ...
- vscode中setting.json配置详解
vscode中的setting.json配置文件配置详解 话不多说上配置文件 大家按需复制到自己的setting.json配置文件中即可 [{// 控制是否在编辑器中显示 CodeLens." ...
- Android Studio项目中Build.gradlle配置详解
许多对gradlle不了解的宝宝们总是会在配置的过程中遇到一系列的问题,大体都是对gradlle不够了解.所以在此详细说明gradlle中每一项的用处,如下 1. apply plugin: 'com ...
- java登录中用户类型分类_基于用户登陆的struts2中action的分类详解
在struts2中action的分类有:继承 ActionSupport 实现 Action,模型驱动(ModelDriven)的 Action,多方法的 Action三种方式. 1.继承 Actio ...
- Struts2中Result类型介绍
1.在Struts2中,Result类型有12种,分别为dispatcher,redirect,chain,redirectAction,freemarker,httpheader,stream,ve ...
- struts2的json插件配置详解
2019独角兽企业重金招聘Python工程师标准>>> 为了方便ajax调用传输数据,在struts2中加入的json插件用来做对象的序列化和反序列化,json插件的下载地址 htt ...
- servlet中web.xml配置详解
Web.xml常用元素 <web-app> <display-name></display-name> 定义了WEB应用的名字 <description ...
- python中byte类型_详解python string类型 bytes类型 bytearray类型
搜索热词 一.python3对文本和二进制数据做了区分.文本是Unicode编码,str类型,用于显示.二进制类型是bytes类型,用于存储和传输.bytes是byte的序列,而str是unicode ...
最新文章
- 架构模式:MVC与MVVM
- Vue安装配置以及入门案例
- android游戏加载,Android 游戏引擎libgdx 资源加载进度百分比显示案例分析
- Angular 开发中的 Source Map
- 链表之判断一个链表是否为回文结构(一)
- 机器学习线性回归_机器学习实例--线性回归
- python 系统策略禁止这个安装_电脑无法安装软件提示设置了系统策略禁止此项安装怎么办?...
- goroutine和channel机制与C#类库功能类比
- VS编程,几个好用的Visual Studio插件推荐(一)
- 算法实践:波兰表达式
- 价值工程杂志价值工程杂志社价值工程编辑部2022年第23期目录
- 成都Uber优步司机奖励政策(3月4日)
- 使用selenium爬取百合网
- linux 关于回程路由相关问题
- opencv 拍摄并保存照片
- 小波分析中db1,db2,db3小波有何不同?
- EIP-3523:半同质代币介绍
- 炫酷超拽!推荐一款Vue开发的OA系统,功能还不错哟!!!
- 玄学:那些年我为之疯狂的超能力(有音频版)
- 开发苹果手机 APP,如何保持iOS页面流畅技巧
热门文章
- C语言经典例16-最大公约数和最小公倍数
- 【Java 并发编程】线程锁机制 ( 悲观锁 | 乐观锁 | CAS 三大问题 | ABA 问题 | 循环时间长问题 | 多个共享变量原子性问题 )
- 【Flutter】ExpansionTile 可折叠列表
- 【计算理论】计算复杂性 ( 阶段总结 | 计算理论内容概览 | 计算问题的有效性 | 语言与算法模型 | 可计算性与可判定性 | 可判定性与有效性 | 语言分类 ) ★
- 20180724 (面向对象:类的命名空间和查询顺序丶组合)
- 大话Web-Audio-Api
- http://tpl.amazeui.org/
- 可伸缩搜索框 旋转实现loading
- 关于ASP.NET 中站点地图sitemap 的使用
- SharePoint判断页面(或WebPart)是否处于编辑模式.