strut2配置文件中Action result type简述

strut2配置文件中Action result type简述

 

 

1 <action name="" class="" method="">
2     <result name="" type=""></result>
3 </action>

type指定为freemarker的意义是指定user_addPage.ftl用freemarker处理

下面是其他类型的意义:

chain

用来处理Action链

com.opensymphony.xwork2.ActionChainResult

dispatcher

用来转向页面,通常处理JSP

org.apache.struts2.dispatcher.ServletDispatcherResult

freemaker

处理FreeMarker模板

org.apache.struts2.views.freemarker.FreemarkerResult

httpheader

控制特殊HTTP行为的结果类型

org.apache.struts2.dispatcher.HttpHeaderResult

redirect

重定向到一个URL

org.apache.struts2.dispatcher.ServletRedirectResult

redirectAction

重定向到一个Action

org.apache.struts2.dispatcher.ServletActionRedirectResult

stream

向浏览器发送InputSream对象,通常用来处理文件下载,还可用于返回AJAX数据

org.apache.struts2.dispatcher.StreamResult

velocity

处理Velocity模板

org.apache.struts2.dispatcher.VelocityResult

xslt

处理XML/XLST模板

org.apache.struts2.views.xslt.XSLTResult

plainText

显示原始文件内容,例如文件源代码

org.apache.struts2.dispatcher.PlainTextResult

redirect-action

重定向到一个Action

org.apache.struts2.dispatcher.ServletActionRedirectResult

plaintext

显示原始文件内容,例如文件源代码

org.apache.struts2.dispatcher.PlainTextResult

 

简单说明一下result的name属性和type属性:

SUCCESS:Action正确的执行完成,返回相应的视图,success是name属性的默认值;

NONE:表示Action正确的执行完成,但并不返回任何视图;

ERROR:表示Action执行失败,返回到错误处理视图;

INPUT:Action的执行,需要从前端界面获取参数,INPUT就是代表这个参数输入的界面,一般在应用中,会对这些参数进行验证,如果验证没有通过,将自动返回到该视图;

LOGIN:Action因为用户没有登陆的原因没有正确执行,将返回该登陆视图,要求用户进行登陆验证。
dispatcher:请求转发,底层调用RequestDispatcher的forward()或include()方法,dispatcher是 type属性的默认值,通常用于转向一个JSP,localtion指定JSP的位置,parse如果为false表示location的值不会被当作 OGNL解析,默认为true;

redirect:重定向,新页面无法显示Action中的数据,因为底层调用response.sendRedirect("")方法,无法共享请求范围内的数据,参数与dispatcher用法相同;

redirect-action:重定向到另一个Action,参数与chain用法相同,允许将原Action中的属性指定新名称带入新Action 中,可以在Result标签中添加 <param name=”b”>${a} </param>,这表示原Action中的变量a的值被转给b,下一个Action可以在值栈中使用b来操作,注意如果值是中文,需要做一些编码处理,因为Tomcat默认是不支持URL直接传递中文的!

velocity:使用velocity模板输出结果,location指定模板的位置(*.vm),parse如果为false,location不被OGNL解析,默认为true;

xslt:使用XSLT将结果转换为xml输出,location指定*.xslt文件的位置,parse如果为false,location不被 OGNL解析,默认为true,matchingPattern指定想要的元素模式,excludePattern指定拒绝的元素模式,支持正则表达式,默认为接受所有元素;

httpheader:根据值栈返回自定义的HttpHeader,status指定响应状态(就是指response.sendError(int i)重定向到500等服务器的状态页),parse如果为false,header的值不会被OGNL解析,headers,加入到header中的值,例如: <param name=”headers.a”>HelloWorld </param>,可以加多个,这些键-值组成HashMap;

freemaker:用freemaker模板引擎呈现视图,location指定模板(*.ftl)的位置,parse如果为false,location的值不会被OGNL解析,contentType指定以何中类型解析,默认为text/html;

chain:将action的带着原来的状态请求转发到新的action,两个action共享一个ActionContext,actionName指定转向的新的Action的名字,method指定转向哪个方法,namespace指定新的Action的名称空间,不写表示与原Action在相同的名称空间;skipActions指定一个使用 , 连接的Action的name组成的集合,一般不建议使用这种类型的结果;

stream:直接向响应中发送原始数据,通常在用户下载时使用,contentType指定流的类型,默认为 text/plain,contentLength以byte计算流的长度,contentDisposition指定文件的位置,通常为 filename=”文件的位置”,input指定InputStream的名字,例如:imageStream,bufferSize指定缓冲区大小,默认为1024字节;
plaintext:以原始文本显示JSP或者HTML,location指定文件的位置,charSet指定字符集;

 
 
<package name="struts" extends="struts-default">
<action name="login" class="com.ebizprise.commons.acl.web.action.LoginAction">
<result name="success" type="chain" >action 名称 </result>
<result name="error">/index.jsp </result>
</action>
</package>

Struts2 中 Result的 Chain Result,Redirect Action Result,Redirect Result 三者之间的区别

Chain Result:

这个result调用另外的一个action,连接自己的拦截器栈和result。


•actionName (默认) - 被调用的action的名字
•namespace - 被调用的action的名称空间. 如果名称空间为空,这默认为当前名称空间
•method - 用于指定目标action的另一个方法被调用. 如果空,默认为excute方法
Redirect Action Result:

这个Result使用ActionMapperFactory提供的ActionMapper来重定位浏览器的URL来调用指定的action和(可选的)namespace. 这个Result比ServletRedirectResult要好.因为你不需要把URL编码成xwork.xml中配置的ActionMapper提供的模式. 这就是说你可以在任意点上改变URL模式而不会影响你的应用程序. 因此强烈推荐使用这个Result而不是标准的redirect result来解决重定位到某个action的情况.


•ActionName (默认) - 重定位到的action名
•namespace - action的名称空间. 如果为null,则为当前名称空间

Redirect Result

调用{@link HttpServletResponse#sendRedirect(String) sendRedirect}方法来转到指定的位置. HTTP响应被告知使浏览器直接跳转到指定的位置(产生客户端的一个新请求). 这样做的结果会使刚刚执行的action(包括action实例,action中的错误消息等)丢失, 不再可用. 这是因为action是建立在单线程模型基础上的. 传递数据的唯一方式就是通过Session或者可以为Ognl表达式的web参数(url?name=value)


•location (默认) - action执行后跳转的地址.
•parse - 默认为true. 如果设置为false, location参数不会被当作Ognl表达式解析.

<result name="success" type="redirect">/displayCart.action?userId=${userId}</result>

 

Chain result type is used for Action Chaining which means that the source result invokes an entire other action, complete with it's own interceptor stack and result.


Redirect Action result type is used to redirect to another Action which means making your source Action, after it has successfully executed, result in a redirect.


As a rule, Action Chaining is not recommended. Redirect Result or the Redirect Action Result is preferred over Chain Result.

====================================

Struts2中的<result></result>标签的属性type="redirect"与type="redirect-action"的区别,

type="redirect" 的值可以转到其它命名空间下的action,而redirect-action只能转到同一命名空下的 action,

因此它可以省略.action的后缀直接写action的名称。

 

Struts2 redirect-action传递参数Struts2

<action name="enterpreinfo" class="preinfoBusinessAction" method="enterPreinfoSub"> 
<result name="success" type="redirect-action"> 
showpreinfo?preinfo.order_number=${preinfo.order_number}&preinfo.company_name=${preinfo.company_name} 
</result> 
<result name="error" type="redirect"> 
<param name="location">/error.jsp</param> 
</result> 
</action>



<<<<<<<<<<<<<<<<<
注意是redirectAction!!!!!!!大写A!!!!!!!!!!
>>>>>>>>>>>>>>>>>

struts2的action重定向到另外一个action如何传参数
当我们在某个action完成后,通常都要重定向到另外一个action,这时可以这样传参数:

<action name="tick_delImage" class="tickAction" method="delImage">
<result name="success" type="redirectAction">
<param name="actionName">tick_edtImage</param>
<param name="namespace">/vector/tick</param>
<param name="tickId">${tickId}</param>
</result>
</action>


 

"actionName"指明要从定向到的action名称;
"nameSpace"指明命名空间
"tickId"是要传递的参数,可以使用如下形式传递tickId:"tick_delImage.action?tickId='100'";


struts2中result的type中redirectAction和redirect的区别 收藏
刚开始学习struts2,在配置文件struts.xml中result标签中type中redirect和redirectAction是不同的效果:

redirect是让浏览器重新发送请求,例如要重新发送到另一个action,result的type需要使用redirect类型,可以是在同一个命名空间下的action,也可以是不同命名空间的另一个action;

redirectAction也是重新发送请求到action,但是是在当前执行的action的同一个namespace下进行查找需要的action。


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值