打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
struts2中Action配置中的各项默认值和result的各种转发类型

在struts2中有很多项,我们平时的配置文件如下,如果不写的默认值会是什么呢?

  1. <package name="chj" namespace="/test" extends="struts-default">  
  2.         <action name="helloworld" class="com.chj.action.HelloWorldAction" method="execute" >  
  3.         <result name="success">/WEB-INF/page/hello.jsp</result>  
  4.         </action>  
  5.   </package>   


1、如果没有为action指定class,默认是ActionSupport。

2、如果没有为action指定method,默认执行action中的execute() 方法。

3、如果没有指定result的name属性,默认值为success。

Action中result的各种转发类型:


  1. <span style="color:#000000;"><action name="helloworld" class="com.chj.action.HelloWorldAction">  
  2.     <result name="success">/WEB-INF/page/hello.jsp</result>  
  3. </action></span>  
result配置相当于forward,  struts2中提供了多种结果类型,常用的类型有: dispatcher(默认值)、 redirect 、 redirectAction 、 plainText。

在result中还可以使用${属性名}表达式访问action中的属性,表达式里的属性名对应action中的属性。如下:

  1. <span style="color:#000000;"><result type="redirect">/view.jsp?id=${id}</result></span>  

下面是redirectAction 结果类型的例子,如果重定向的action中同一个包下

  1. <span style="color:#000000;"><result type="redirectAction">helloworld</result>  
  2. </span>  

如果重定向的action在别的命名空间下:
  1. <span style="color:#000000;"><result type="redirectAction">  
  2.     <param name="actionName">helloworld</param>  
  3.     <param name="namespace">/test</param>  
  4. </result>  
  5. </span>  

plaintext:显示原始文件内容,例如:当我们需要原样显示jsp文件源代码 的时候,我们可以使用此类型。
  1. <span style="color:#000000;"><result name="source" type="plainText ">  
  2.     <param name="location">/xxx.jsp</param>  
  3.     <param name="charSet">UTF-8</param><!-- 指定读取文件的编码 -->  
  4. </result>  
  5. </span>  

当多个action中都使用到了相同视图,这时我们应该把result定义为全局视图。struts2中提供了全局global-results中也提供了相似功能:
  1. <span style="color:#000000;"><package ....>  
  2.     <global-results>  
  3.         <result name="message">/message.jsp</result>  
  4.     </global-results>  
  5. </package>  
  6. </span>  




本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Struts2框架学习之三:result返回结果
使用struts2.5版本遇到的问题
Struts2+Spring集成合并
struts2.x中的Action配置
Struts2 注解配置Action(十九)
Struts2读书笔记-----struts 的action配置
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服