Is it necessary to implement the Execute method in Action in Struts2

  • 2020-05-05 11:19:43
  • OfStack

Today, a friend asked me if Action in Struts2 must implement execute method. The answer came out smoothly.

There are actually two cases:

1) if your Action class is inherited from ActionSupport or BaseAction, it actually overrides the execute method. The default implementation in ActionSupport is to return the "success" view. Therefore, you can not implement the execute method, as long as your struts.xml has "success" corresponding to 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> 

In this code, the jump page is controlled through the action configuration file. There is no execute () method override in action's classes.

2) if your Action class does not inherit ActionSupport or BaseAction, and you do not correspond to < in struts.xml action > If you specify your own method with the method attribute in the tag, you will find the execute method by default, and you must implement the execute method, otherwise Struts2 will not find the corresponding method and report an error.

Most of the time, however, ActionSupport is inherited (for example, input validation, file upload, and so on are required). Also, whether you write the execute method or not, you can still use < action > The method attribute of the tag specifies that of the other method.

Above is the site to introduce Struts2 in Action whether to implement the Execute method of the relevant introduction, I hope to help you!


Related articles: