Struts2系列——struts2的result
来源:互联网 发布:65535端口 编辑:程序博客网 时间:2024/05/23 18:37
在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
- Struts2系列——struts2的result
- Struts2系列——struts2的result
- Struts2——Result
- Result——Struts2的处理结果
- Struts2 —— Result机制
- struts2——result配置!!!
- Struts2 —— Result机制
- struts2——result类型
- Struts2— Result与ResultType
- struts2的result标签
- Struts2 的Result Type
- Struts2的Result Type
- Struts2的Result Type
- Struts2 的 Result
- Struts2 的 Result Type
- struts2的Result类型
- Struts2的Result Type
- IBM TSM 6.3学习笔记(1)
- AOJ-AHU-OJ-351 求最值之差
- SQL总结(一)SQL的分类
- HBase中数据的多版本特性潜在的意外
- 黑马程序员--------------JAVA基础(一)
- Struts2系列——struts2的result
- Installing teamviewer 9 on 64-bit Ubuntu 13.10
- C中联合(union)用法
- 【iOS】UITextField详解和键盘设置
- Struts有关的技术题
- MongoDB 副本集的搭建、应用
- Cocos2d-x创建项目和添加一些额外的类库
- IOS开发(一)真机调试
- C#(一)-抽象类与接口