菜鸟学Struts2——Results

  在对Struts2的Action学习之后,对Struts2的Result进行学习。主要对Struts2文档Guides中的Results分支进行学习,如下图:

  1、Result Types(Result类型)

Struts2的Action处理用户请求后返回一个字符串,在struts.xml配置文件中进行字符串与实际视图的映射,在配置文件中是通过action的子元素result来完成此功能的,result的格式如下:

1 <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 记录。配置列子如下: 

1 <result type="chain">
2      <param name="actionName">chain</param>
3      <param name="namespace">/chain</param>
4 </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值栈中,配置列子如下: 

1 <result name="input">/index.jsp</result>

 (3)HttpHeader Result

HttpHeader Result允许开发自定义返回http header的参数或者返回一个错误信息,HttpHeader Result有5个参数status、parse(跟dispatcher的parse一样)、headers 、error、errorMessage ,配置列子如下: 

 1 <result name="success" type="httpheader">
 2   <param name="status">204</param>
 3   <param name="headers.a">a custom header value</param>
 4   <param name="headers.b">another custom header value</param>
 5 </result>
 6  
 7 <result name="proxyRequired" type="httpheader">
 8   <param name="error">305</param>
 9   <param name="errorMessage">this action must be accessed through a proxy</param>
10 </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),配置例子如下:

1 <result name="success" type="redirect">
2   <param name="location">foo.jsp</param>
3   <param name="parse">false</param>
4   <param name="anchor">FRAGMENT</param>
5 </result>

 上面的列子最后的url将会是:foo.jsp#FRAGMENT

1 <result name="showReportResult" type="redirect">
2     <param name="location">generateReport.jsp</param>
3     <param name="reportType">pie</param>
4     <param name="width">100</param>
5     <param name="height">100</param>
6     <param name="parse">false</param>
7     <param name="anchor">summary</param>
8 </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,配置列子如下:

1 <result name="redirect" type="redirectAction">
2     <param name="actionName">result-target</param>
3     <param name="width"></param>
4     <param name="height">100</param>
5     <param name="suppressEmptyParameters">false</param>
6 </result>

 (6)Stream Result

Stream Result用于返回一个InputStream,原始数据直接传递给HttpServletResponse,可以用于文件下载等,Stream Result有7个参数contentType 、contentLength、contentDisposition、inputName 、bufferSize 、allowCaching 、contentCharSet 。完整的列子如下:

 1 <!DOCTYPE struts PUBLIC
 2     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 3     "http://struts.apache.org/dtds/struts-2.3.dtd">
 4 <struts>
 5     <package name="result" extends="struts-default" namespace="/result">
 6         <action name="result-*" class="yaolin.core.action.ResultAction" method="{1}">
 7             <result name="stream" type="stream">
 8                 <param name="contentType">image/jpeg</param>
 9                 <param name="inputName">stream</param>
10                 <param name="contentDisposition">attachment;filename="yaolin.jpg"</param>
11                 <param name="bufferSize">1024</param>
12             </result>
13         </action>
14     </package>
15 </struts>
 1 package yaolin.core.action;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.InputStream;
 6 
 7 public class ResultAction {
 8 
 9     // 文件流
10     private InputStream stream;
11     // Stream Result
12     public String stream() throws Exception {
13         File img = new File("E:/yaolin.jpg");
14         stream = new FileInputStream(img);
15         return "stream";
16     }
17 
18     // getter and setter
19     public InputStream getStream() {
20         return stream;
21     }
22 }

 (7)PlainText Result

PlainText Result是直接将JSP/HTML中的源代码内容输入到页面中,PlaintText Result有两个参数location、charset。配置例子如下:

1 <result name="text" type="plainText">
2     <param name="location">/index.jsp</param>
3     <param name="charset">utf-8</param>
4 </result>

 2、DispatcherListener

 以下是官网DOC给的例子,暂时没搞明白怎么用?? 后续研究一下。

 1 //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.
 2 static {
 3    Dispatcher.addDispatcherListener(new DispatcherListener() {
 4        public void dispatcherInitialized(Dispatcher du) {
 5           // do something to Dispatcher after it is initialized eg.
 6           du.setConfigurationManager(....);
 7        }
 8  
 9        public void dispatcherDestroyed(Dispatcher du) {
10           // do some cleanup after Dispatcher is destroyed.
11        }
12    });
13 }

 

 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.

例子:

 1 // 在Action中使用
 2 public class MyAction extends ActionSupport {
 3    ...
 4    public String execute() throws Exception {
 5        ActionInvocation invocation = ActionContext.getContext().getActionInvocation();
 6        invocation.addPreResultListener(new PreResultListener() {
 7             public void beforeResult(ActionInvocation invocation, 
 8                                      String resultCode) {
 9                 // perform operation necessary before Result execution
10             }
11        });
12    }
13    ...
14 }

 

// 在拦截器中使用
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需要进行研究。

未完,待续。

转载于:https://www.cnblogs.com/niloay/p/struts2-result.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值