Result相关

本文大部分copy api。仅作平时查找,质量不高。

内置Result实现类:

chain

com.opensymphony.xwork2.ActionChainResult

dispatcher默认

org.apache.struts2.dispatcher.ServletDispatcherResult

freemarker

org.apache.struts2.views.freemarker.FreemarkerResult

httpheader

org.apache.struts2.dispatcher.HttpHeaderResult

redirect

org.apache.struts2.dispatcher.ServletRedirectResult

redirectAction

org.apache.struts2.dispatcher.ServletActionRedirectResult

stream

org.apache.struts2.dispatcher.StreamResult

velocity

org.apache.struts2.dispatcher.VelocityResult

xslt

org.apache.struts2.views.xslt.XSLTResult

plainText

org.apache.struts2.dispatcher.PlainTextResult

 

chain

Action链式处理的结果类型

1This result invokes an entire other action, complete with it's own interceptor stack and result.

2)代码中有一句actionProxy.execute()连接下一个[action流程](包括拦截器,actionpreResultListenersresult)。下一次[action流程]使用上一次[action流程]ActionContext(这么多action属于一次http请求,且是同一个线程)。

3HistoryAction用于防止循环调用actionskipActions用于标记哪些[action流程]不用检查是否已经运行过。

4)可用来forward action,下面的dispatch类型不能forward action,redirect[action]类型是redirect action。重新创建一个ActionProxy再跑一次xwork。

【参数】

actionName

默认,接着要串联的action

namespace

used to determine which namespace the Action is in that we're chaining. If namespace is null, this defaults to the current namespace

Method

used to specify another method on target action to be invoked. If null, this defaults to execute method

skipActions

可选,the list of comma separated action names for the actions that could be chained to

【例子 】

<package name="public" extends="webwork-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="webwork-default" namespace="/secure">
     <action name="dashboard" class="...">
         <result>dashboard.jsp</result>
     </action>
 </package>

 

 

dispatcher

Includes or forwards to a view (usually a jsp).使用RequestDispatch的forward或include方法转发到another resource (servlet, JSP file, or HTML file) on the server

【参数】

location :默认 the location to go to after execution (ex. jsp).

parse true by default. If set to false, the location param will not be parsed for Ognl expressions. 该参数指定是否允许杂实际视图名字中使用OGNL表达式,该参数默认为true。如果设置该参数为false,则不允许在实际视图名中使用表达式。通常无须修改该属性。

 

stream

A custom Result type for send raw data (via an InputStream) directly to the HttpServletResponse. Very useful for allowing users to download content. 。这些参数也可在valuestack上定义

【参数】

contentTypethe stream mime-type as sent to the web browser (default = text/plain).

contentLengththe stream length in bytes (the browser displays a progress bar).

contentDispostion the content disposition header value for specifing the file name (default = inline, values are typically filename="document.pdf".

inputNamethe name of the InputStream property from the chained action (default = inputStream). ,或在the invocation variable stack

bufferSize - the size of the buffer to copy from input to output (default = 1024).

使用stream类型返回比使用[action中返回void并使用response控制输出]在某些情况下有用,比如在客户端显示返回内容之前要作检查,类似PreResultListener的功能。

【例】

   

<action name="getNations" class="action.PlayAmountAction" method="getNations">   
<result name="success" type="stream"></result>   
</action>   
  
protected void configureStreamResult(String content)   
{   
    /**  
     *  protected String contentType = "text/plain";  
     *  protected String contentLength;  
     *  protected String contentDisposition = "inline";  
     *  protected String contentCharSet ;  
     *  protected String inputName = "inputStream";  
     *  protected InputStream inputStream;  
     *  protected int bufferSize = 1024;  
     *  protected boolean allowCaching = true;  
     */  
    ValueStack vs=ActionContext.getContext().getValueStack();   
    ByteArrayInputStream bais=null;   
    try {   
        bais = new ByteArrayInputStream(content.getBytes("UTF-8"));   
    } catch (UnsupportedEncodingException e) {   
        // TODO Auto-generated catch block   
        e.printStackTrace();   
    }   
    vs.set("inputStream", bais);   
    vs.set("contentCharSet", "UTF-8");   
    vs.set("contentType", "application/json");   
    vs.set("contentLength",bais.available());   
    vs.set("bufferSize", bais.available());   
    vs.set("allowCaching", false);         
}  

 

 

freemarker

使用Freemarker模板引擎渲染视图。Ftl数据如何绑定在ActionContext那一篇文中。

FreemarkerManager类用于配置模板加载路径:可以是以下2

1relative to the web root folder. eg /WEB-INF/views/home.ftl

2A classpath resuorce. eg com/company/web/views/home.ftl

【参数】

This result type takes the following parameters:

location :默认 the location of the template to process.

Parsetrue by default. If set to false, the location param will not be parsed for Ognl expressions.

contentTypedefaults to "text/html" unless specified.

httpheader

用于控制特殊的HTTP行为。A custom Result type for setting HTTP headers and status by optionally evaluating against the ValueStack.

【参数】

status the http servlet response status code that should be set on a response.

parse true by default. If set to false, the headers param will not be parsed for Ognl expressions.

headers header values.

【例子】

 <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>

redirect

Calls the sendRedirect method to the location specified. The response is told to redirect the browser to the specified location (a new request from the client). The consequence of doing this means that the action (action instance, action errors, field errors, etc) that was just executed is lost and no longer available. This is because actions are built on a single-thread model. The only way to pass data is through the session or with web parameters (url?name=value) which can be OGNL expressions.

【参数】

location :默认 the location to go to after execution.

Parsetrue by default. If set to false, the location param will not be parsed for Ognl expressions.

redirectAction

This result uses the ActionMapper provided by the ActionMapperFactory to redirect the browser to a URL that invokes the specified action and (optional) namespace. This is better than the ServletRedirectResult because it does not require you to encode the URL patterns processed by the ActionMapper in to your struts.xml configuration files. This means you can change your URL patterns at any point and your application will still work. It is strongly recommended that if you are redirecting to another action, you use this result rather than the standard redirect result.

 

velocity

处理velocity模板

xslt

处理xslt模板

plainText

不处理,直接显示文件

PreResultListener

PreResultLIstener是一个监听接口,可以在Action完成控制处理后,系统转入实际的物理视图之间被回调

Struts2应用可由Action、拦截器添加PreResultListener监听器,添加PreResultListener监听器通过ActionInvocationaddPreResultListener()方法完成

1Action添加了PreResultListener监听器,该监听器就可以在应用转入实际物理视图之前回调该监听器的beforeResult()方法;

2)拦截器添加了PreResultListener监听器,该监听器会对该拦截器设置可以拦截的所有Action起作用。

beforeResult(ActionInvocation invocation, String resultCode)

resultCode是指<result name=””>name的值,即视图的逻辑名。

Interceptor(s)àactionàpreResultListeneràresultàinterceptor(s)

如果action中的方法返回void,这里的resultCode 就是null

 

 

猜你喜欢

转载自bravecs.iteye.com/blog/1731321