Non-execute method call Struts2

Non-execute method calls

1) If your Action class inherits from ActionSupport words, precisely, is to rewrite the execute method, ActionSupport in the default implementation is to return "success" view. So you can not achieve the execute method, as long as your struts.xml there are "success" corresponding result can be. 

2) If your Action class does not inherit ActionSupport, and you do not correspond in struts.xml the <action> tag with the method attribute to specify your own way, then, to find a default execute method, this time is necessary to achieve execute method, otherwise Struts2 will not find a corresponding method and error.

However, most cases are inherited ActionSupport (such as input validation, file upload functions requires must inherit). Also, whether you write did not write execute methods, or other methods can be specified by the method attribute <action> tag.

Only execute method of learning about Action in front of our Action in the real business logic, if we have only each Action in such a method, then when the function of our program requires a lot of time, we would have to manually write a lot of the Action class, which is obviously unreasonable. As I have said, our Action class does not necessarily have to inherit a class or implements an interface, we can use our POJO as Action, Action and we also do not necessarily have to have the execute method, if we not using the execute method, then we need to configure the action is the time to use the method attribute on the action label to indicate the action method we want to use.

Then we can also write an Action in multiple methods for implementing business logic, they were to perform different functions, but doing the work is somewhat similar. For example, we can handle all operations related to the user are written in UserAction them, then so that we can better organize our code. Similarly, we only need to specify the method we want to use for our action in struts.xml label.

To implement an Action class called non-execute method, there are three ways:

(1) using the method attribute

 <action name="userSave" class="action.UserAction" method="save">

           <result name="success">/success.jsp</result>

       </action>

(2) use of wildcards

In addition to using the above method when it comes, we can not have a method for each action in the Action are configured in struts.xml, we can configure it a, to specify which method will be executed using wildcards in this actin.

  <action name="*User" class="action.UserAction" method="{1}">

           <result name="input">/input.jsp</result>

           <result name="success">success.jsp</result>

       </action>

(3) dynamic method invocation

In addition to the method described above, struts2 There is also a realization method - dynamic method invocation. Using the format of dynamic method calls for  action! Method that is using the "!" To connect the action and method to execute our configuration, this way we do not need to specify the method attributes acttion label.

<package name="default" namespace="/" extends="struts-default">

   

       <action name="user" class="action.UserAction">

           <result name="input">/input.jsp</result>

           <result name="success">/success.jsp</result>

       </action>
      

</package>

 

Then we have to make some changes to input.jsp, the form's action attribute changed to "user save!":

<form action="user!save" method="post">

     username : <input ype="text" name="userName"/><br/>

     password :<input type="password" name="password"><br/>

      <input type="submit" value="submit"/>

  </form>

 

Guess you like

Origin www.cnblogs.com/lukelook/p/11125003.html