Multiple submit buttons on the form
1 Overview
In this quick tutorial, we will use the form entry in Spring MVC as a basis and add another button to the JSP form to map to the same URI.
2.Brief review
Previously, we created a small web application to enter employee details and save it in memory.
First, we wrote a Employee
model to bind the entities, then we wrote one EmployeeController
to handle the process and mapping, and finally we wrote a named employeeHome
view, which describes the form of the user typing in the input value.
The form has only one button Submit
, which is mapped to addEmployee
the controller named RequestMapping
to use the model to add the detailed information entered by the user to the in-memory database.
In the next few sections, we will see how to RequestMapping
add another button to the same form with the same path in the controllerCancel,
.
3.Form
First, let's employeeHome.jsp
add a new button to the form:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
...
<body>
<h3>Welcome, Enter The Employee Details</h3>
<h4>${message}</h4>
<form:form method="POST" action="${pageContext.request.contextPath}/addEmployee"
modelAttribute="employee">
<table>
...
<tr>
<td><input type="submit" name="submit" value="Submit" /></td>
<td><input type="submit" name="cancel" value="Cancel" /></td>
</tr>
...
As we can see, we Submit
added an attribute to the existing button name
and added another name
setting as a cancel
Cancel
button .
We have also added model attributes at the top of the page message
, if you click " Cancel
, it will be displayed message
.
4.Controller
Next, let's modify the controller to RequestMapping
add new properties param
to distinguish the two clicks of the button:
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST, params = "submit")
public String submit(@Valid @ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) {
// same code as before
}
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST, params = "cancel")
public String cancel(@Valid @ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) {
model.addAttribute("message", "You clicked cancel, please re-enter employee details:");
return "employeeHome";
}
Here, we have added a new parameter to the params
existing method.**submit**.
It is worth noting that its value is the name
same as that specified in the form .
Then, we added another method with a similar signature cancel
, the only difference is cancel
the parameter specified as params
. As before, it Cancel
is name
exactly the same as the " " button in the JSP form .
5.Test
For testing, we deploy the project on a web container (such as Tomcat).
When visiting the URL [http://localhost:8080/spring-mvc-forms/employee](http://localhost:8080/spring-mvc-forms/employee) ,
we will see:
Click Cancel
and we will see:
Here we see cancel
the message encoded in the controller's method .
Click Submit
and we will see the typed employee information as before:
Six, conclusion
In this tutorial, we learned how to add another button to the same form in a Spring MVC application, which maps to the same on the controller RequestMapping
.
If needed, you can add more buttons using the same technique demonstrated in the code snippet .
As always, source code is available on GitHub .
0 Comments