Struts2中Action必须实现execute方法吗

来源:互联网 发布:java读取txt文件第二行 编辑:程序博客网 时间:2024/06/05 18:45

好长时间没有写博客了,今天遇到了一个问题,顺利的解决了,分享出来。微笑

struts2中action中是否需要实现execute方法呢?

其实分两种情况:


1)如果你的Action类是继承自ActionSupport或是BaseAction的话,确切的说是重写了execute方法,ActionSupport里的默认实现就是返回"success"视图。因此,你可以不实现execute方法,只要你的struts.xml里有"success"对应的result即可。
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <action name="doRevenuesMaintenance">  
  2.             <interceptor-ref name="novatar-webStack-baseparam">  
  3.                 <param name="security.actionType">PRIVATE</param>  
  4.             </interceptor-ref>  
  5.             <result name="success">incomeMaintenance.jsp</result>  
  6. < /action>  

这段代码中就是通过action的配置文件来控制跳转页面。在action的类中没有execute()方法重写。


2)如果你的Action类没有继承ActionSupport或是BaseAction的话,而你又没有在struts.xml中对应<action>标签中用method属性指定你自己的方法的话,默认就要找execute方法,这时是必须要实现execute方法的,否则Struts2会找不到对应的方法而报错。

不过,大部分情况下都是继承ActionSupport的(比如输入验证、文件上传等功能就要求必须继承)。还有,不管你写没写execute方法,还是可以用<action>标签的method属性指定其他方法的。原文
0 0
原创粉丝点击