STRUTS2 Tutorial 2: Handle a form multiple submit

Source: Internet
Author: User
Tags html form requires

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.