In many Web applications, one HTML form tag may have two or more submit buttons in order to accomplish different tasks, as shown in the following code:
Because multiple submit buttons in <form> are submitted to an action, the Execute method using the STRUTS2 action cannot determine which submit button the user clicked on. If you have used struts1.x, you will know that the version before Struts1.2.9 needs to use a lookupdispatchaction action to handle a form with multiple submit. However, the use of Lookupdispatchaction action requires access to the property file, but also need mapping, more cumbersome. Starting from Struts1.2.9, a eventdispatchaction action was added. This class can invoke the action specified by the request parameter through Java reflection (in fact, it simply determines that a request parameter is not present and, if it exists, invokes a method with the same name as the action Class). Using Eventdispatchaction you must specify a different value for the name attribute of the submit to differentiate each submit. This functionality will be easier to implement in Struts2.
Of course, we can also simulate the Eventdispatchaction method to obtain and process parameter information through request. But this is more troublesome. Another approach is provided in Struts2, so that there is no need to configure a different method to be executed in the same action class (by default, the Execute method is executed). Using this method also requires the request parameter to specify the action to be performed. The request parameter name is in the format
Action!method.action
Note: Since Struts2 only requires a parameter name, the parameter value is anything.
Here's an example program that shows how to handle a form with multiple submit:
"Step 1th" Implementation homepage (MORE_SUBMIT.JSP)
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%> <%@ taglib prefix=
"s" uri= "/ Struts-tags "%>
There are two submit in more_submit.jsp: Save and print. The method property is used to specify the methods to invoke: Save and print. Therefore, you must have the Save and print methods in the action class.