Struts2中Action中是否需要实现Execute方法


今天有朋友问我Struts2中Action必须实现execute方法吗?顺利的回答出来了。

其实分两种情况:

1)如果你的Action类是继承自ActionSupport或是BaseAction的话,确切的说是重写了execute方法,ActionSupport里的默认实现就是返回"success"视图。因此,你可以不实现execute方法,只要你的struts.xml里有"success"对应的result即可。

<action name="doRevenuesMaintenance"> 
<interceptor-ref name="novatar-webStack-baseparam"> 
<param name="security.actionType">PRIVATE</param> 
</interceptor-ref> 
<result name="success">incomeMaintenance.jsp</result> 
< /action> 

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

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

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

以上所述是小编给大家介绍的Struts2中Action中是否需要实现Execute方法的相关介绍,希望对大家有所帮助!


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3