In many web applications, in order to do different jobs, one HTML form tag may have two or more submit buttons, as shown in the following code:
<HTML action = "" method = "Post">
<Input type = "Submit" value = "save"/>
<Input type = "Submit" value = "print"/>
</Html>
Since Multiple submission buttons in <form> are submitted to one action, the execute method of struts2 action cannot determine which submission button the user has clicked. If you have used struts1.x, you will know that in versions earlier than struts1.2.9, you need to use a lookupdispatchaction to process forms with multiple submit entries. However, when you use the lookupdispatchaction, you need to access the attribute file and map the file, which is troublesome. An eventdispatchaction is added from struts1.2.9. This class can call the action specified by the request parameter through Java reflection (in fact, it only determines that a request parameter does not exist. If so, it calls a method with the same name as this parameter in the action class ). When using eventdispatchaction, you must specify different values for the name attribute of the submit to distinguish each submit. This function is easier to implement in struts2.
Of course, we can also simulate the eventdispatchaction method to obtain and process parameter information through the request. However, this is troublesome. Another method is provided in struts2, so that different methods can be executed in the same action class without any configuration (the execute method is executed by default ). To use this method, you also need to specify the action to be executed through the request parameter. The format of the request parameter name is
Action! Method. Action
Note: Because struts2 only requires the parameter name, the parameter value can be anything.
The following is an instance program to demonstrate how to handle multiple submit forms:
Step 2: implement the home page (more_submit.jsp)
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "GBK" %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<HTML>
<Head>
<Title> my JSP 'hello. jsp 'starting page </title>
</Head>
<Body>
<S: Form Action = "Submit. Action">
<S: textfield name = "MSG" label = "input content"/>
<S: Submit name = "save" value = "save" align = "Left" method = "save"/>
<S: Submit name = "print" value = "print" align = "Left" method = "print"/>
</S: Form>
</Body>
</Html>
There are two submit in more_submit.jsp: Save and print. The method attribute specifies the methods to be called: Save and print. Therefore, the SAVE and print methods must be included in the action class.
Step 2: implement the action class (moresubmitaction)
Package action;
Import javax. servlet. http .*;
Import com. opensymphony. xwork2.actionsupport;
Import org. Apache. struts2.interceptor .*;
Public class moresubmitaction extends actionsupport implements servletrequestaware
{
Private string MSG;
Private javax. servlet. http. httpservletrequest request;
// Obtain the httpservletrequest object
Public void setservletrequest (httpservletrequest request)
{
This. Request = request;
}
// Process the Save submit button action
Public String save () throws exception
{
Request. setattribute ("result", "successfully saved [" + MSG + "]");
Return "save ";
}
// Process the print submit button action
Public String print () throws exception
{
Request. setattribute ("result", "successfully printed [" + MSG + "]");
Return "print ";
}
Public String getmsg ()
{
Return MSG;
}
Public void setmsg (string MSG)
{
This. MSG = MSG;
}
}
Note the following two points for the above Code:
The SAVE and print methods must exist. Otherwise, a java. Lang. nosuchmethodexception exception is thrown.
The methods in struts2 action are different from those in struts1.x action. Only the execute method of the struts2 action cannot access the request object. Therefore, the struts2 action class needs to implement a built-in interceptor of struts2 to obtain the request object. The Interceptor is as follows:
Org. Apache. struts2.interceptor. servletrequestaware
Step 2: Configure struts2 action
The Struts. XML Code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype struts public
"-// Apache Software Foundation // DTD struts configuration 2.0 // en"
Http://struts.apache.org/dtds/struts-2.0.dtd>
<Struts>
<Package name = "Demo" extends = "struts-Default">
<Action name = "Submit" class = "Action. moresubmitaction">
<Result name = "save">
/Result. jsp
</Result>
<Result name = "print">
/Result. jsp
</Result>
</Action>
</Package>
</Struts>
[Step 2] compile the result page (result. jsp)
<% @ Page pageencoding = "GBK" %>
<HTML>
<Head>
<Title> submission result </title>
</Head>
<Body>
<H1 >$ {result} </Body>
</Html>
In result. jsp, the execution result information written to the request attribute in the SAVE and print methods is obtained and output to the client.
After Tomcat is started, run the following URL in IE to test the program:
HTTP: /localhost: 8080/moresubmit/more_submit.jsp
You can also directly use the following URL to call the SAVE and print methods:
Call the Save method: http: // localhost: 8080/moresubmit/submit! Save. Action
Call the print method: http: // localhost: 8080/moresubmit/submit! Print. Action