博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Result相关
阅读量:6854 次
发布时间:2019-06-26

本文共 6755 字,大约阅读时间需要 22 分钟。

hot3.png

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

内置Result实现类:

chain

com.opensymphony.xwork2.ActionChainResult

dispatcher默认

org.apache.struts2.dispatcher.

freemarker

org.apache.struts2.views.freemarker.

org.apache.struts2.dispatcher.

redirect

org.apache.struts2.dispatcher.

redirectAction

org.apache.struts2.dispatcher.

stream

org.apache.struts2.dispatcher.

org.apache.struts2.dispatcher.VelocityResult

xslt

org.apache.struts2.views.xslt.

plainText

org.apache.struts2.dispatcher.

 

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

【例子 】

login
dashboard
/secure
dashboard.jsp

 

 

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的功能。

【例】

   

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 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,  resultCode)

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

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

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

 

 

转载于:https://my.oschina.net/braveCS/blog/548790

你可能感兴趣的文章
Spring系列之二——Spring初体验
查看>>
【Web动画】SVG 线条动画入门
查看>>
ftp服务器搭建(离线安装vsftpd),配置
查看>>
RequestMapping_请求方式
查看>>
bootstrap-datetimepicker时间控件
查看>>
第三十九天
查看>>
linux内核驱动module_init解析(2)
查看>>
代码优化中 主窗口 问题 解决
查看>>
POJ1091 跳蚤
查看>>
DUBBO本地搭建及小案例 (转)
查看>>
RabbitMQ指南之二:工作队列(Work Queues)
查看>>
软件测试2019:第八次作业—— 缺陷管理(含缺陷管理工具的配置实验)
查看>>
Go:slice
查看>>
一个android应用开发的感悟
查看>>
Qt Clipboard剪贴板简单使用
查看>>
使用UIElement.AddHandler捕获已被处理的RoutedEvent
查看>>
12.21站立会议
查看>>
SQL server 统计数据库表数量和列出所有表名称
查看>>
遍历DOM树,过滤节点
查看>>
XAML实例教程系列 - XAML传递参数到值转换类实例
查看>>