• 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, .

3.Form

First, let's employeeHome.jspadd 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 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.

4.Controller

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 .

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:

multiple-submit-buttons-on-a-form.png

Click Canceland we will see:

multiple-submit-buttons-on-a-form-1.png

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

Click Submitand we will see the typed employee information as before:

multiple-submit-buttons-on-a-form-2.png

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 .


Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+