可以在struts2-core-{version}.jar中找到struts-default.xml,里面列举了当前可以使用的所有result-type以及对应的class
此处是struts2.2.3的
- <result-types>
- <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
- <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
- <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
- <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
- <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
- <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
- <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
- <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
- <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
- <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
- </result-types>
1.chain
该类型是请求转发给其他的action,如果为jsp则会报错
需要注意的就是与redirect的区别,请求转发是还在当前请求,
而redirect会响应一次浏览器然后浏览器再根据响应请求重定向的资源,注意看url的变化就明白了!
下面是apache给的示例,这个比较简单,没什么说的,
- <package name="public" extends="struts-default">
- <!-- Chain creatAccount to login, using the default parameter -->
- <action name="createAccount" class="...">
- <result type="chain">login</result>
- </action>
- <action name="login" class="...">
- <!-- Chain to another namespace -->
- <result type="chain">
- <param name="actionName">dashboard</param>
- <param name="namespace">/secure</param>
- </result>
- </action>
- </package>
- <package name="secure" extends="struts-default" namespace="/secure">
- <action name="dashboard" class="...">
- <result>dashboard.jsp</result>
- </action>
- </package>
- <result name="success" type="dispatcher">
- <param name="location">foo.jsp</param>
- </result>
- <action name="test" class="com.iss.action.TestAction">
- <result name="success" type="httpheader">
- <param name="status">400</param>
- </result>
- </action>
- private static final long serialVersionUID = 195648957144219214L;
- /** The default parameter */
- public static final String DEFAULT_PARAM = "status";
- private boolean parse = true;
- private Map<String,String> headers;
- private int status = -1;
- private int error = -1;
- private String errorMessage;
- public void execute(ActionInvocation invocation) throws Exception {
- HttpServletResponse response = ServletActionContext.getResponse();
- ValueStack stack = ActionContext.getContext().getValueStack();
- if (status != -1) {
- response.setStatus(status);
- } else if (error != -1) {
- if (errorMessage != null) {
- String finalMessage = parse ? TextParseUtil.translateVariables(
- errorMessage, stack) : errorMessage;
- response.sendError(error, finalMessage);
- } else
- response.sendError(error);
- }
- if (headers != null) {
- for (Iterator iterator = headers.entrySet().iterator();
- iterator.hasNext();) {
- Map.Entry entry = (Map.Entry) iterator.next();
- String value = (String) entry.getValue();
- String finalValue = parse ? TextParseUtil.translateVariables(value, stack) : value;
- response.addHeader((String) entry.getKey(), finalValue);
- }
- }
- }
重定向操作,与请求转发的区别看1的介绍
让客户端请求另外的网络资源,可以为action,也可以为视图资源
下面是apache的示例
- <result name="success" type="redirect">
- <param name="location">foo.jsp</param>
- <param name="parse">false</param>
- </result>
重定向至Action,与redirect的区别找了好久,但是也没发现比较权威的说明
最明显的区别当然是redirectAction只能请求action,如果请求视图资源会报错
然后还有个小区别就是redirectAction会为url添加.action后缀而redirect不会
有说redirectAction无法通过url传参,但是我测试时完全可以通过request获取到,
此处区别如果有哪位知道的麻烦告知,Thank you!
下面是apache的传参的示例
- <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=pie&width=100&height=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>
6.stream
这个返回类型主要用作下载文件或者在浏览器上显示PDF等文档
此处给一个显示PDF文档示例
项目web.xml中
- <mime-mapping>
- <extension>pdf</extension>
- <mime-type>application/pdf</mime-type>
- </mime-mapping>
struts.xml中
- <action name="test" class="com.iss.action.TestAction">
- <result name="success" type="stream">
- <param name="contentType">application/pdf</param>
- <param name="inputName">inputStream</param>
- <param name="contentDisposition">filename="a.pdf"</param>
- </result>
- </action>
TestAction.java中
- public class TestAction extends ActionSupport
- {
- private InputStream inputStream;
- public InputStream getInputStream()
- {
- return inputStream;
- }
- @Override
- public String execute() throws Exception
- {
- inputStream = new FileInputStream("xxxxx/a.pdf");//要下载或显示的文件路径
- return SUCCESS;
- }
- }
7.plainText
响应以plain形式返回给客户端
截取源码的最重要的一部分
- if (charset != null) {
- response.setContentType("text/plain; charset="+charSet);
- }
- else {
- response.setContentType("text/plain");
- }
8.freemarker,velocity,xslt
没有尝试过,有兴趣的可以尝试下