• notice
  • Congratulations on the launch of the Sought Tech site

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 Employeemodel to bind the entities, then we wrote one EmployeeControllerto handle the process and mapping, and finally we wrote a named employeeHomeview, which describes the form of the user typing in the input value.

The form has only one button Submit, which is mapped to addEmployeethe controller named RequestMappingto 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 RequestMappingadd another button to the same form with the same path in the controllerCancel, .


First, let's employeeHome.jspadd a new button to the form:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<h3>Welcome, Enter The Employee Details</h3>
<form:form method="POST" action="${pageContext.request.contextPath}/addEmployee"
<td><input type="submit" name="submit" value="Submit" /></td>
<td><input type="submit" name="cancel" value="Cancel" /></td>

As we can see, we Submitadded an attribute to the existing button nameand added another namesetting as a cancel Cancelbutton .

We have also added model attributes at the top of the page message, if you click " Cancel, it will be displayed message.


Next, let's modify the controller to RequestMappingadd new properties paramto 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 paramsexisting method.**submit**. It is worth noting that its value is the namesame as that specified in the form .

Then, we added another method with a similar signature cancel, the only difference is cancelthe parameter specified as params. As before, it Cancelis nameexactly the same as the " " button in the JSP form .


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 Canceland we will see:


Here we see cancelthe message encoded in the controller's method .

Click Submitand 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 .


Technical otaku

Sought technology together

Related Topic


Leave a Reply