Handling Forms in Spring MVC
Handling forms in Spring MVC involves creating form backing objects, binding form data to these objects, and performing validation. This guide covers the key concepts and steps for handling forms, including creating form backing objects, creating and submitting forms, and validating form data.
Key Concepts of Handling Forms
- Form Backing Object: A Java object that holds form data.
- @ModelAttribute: Binds a method parameter or method return value to a named model attribute.
- @RequestParam: Binds a request parameter to a method parameter.
- Validation: Ensuring that form data meets certain criteria before processing.
Step 1: Create a Form Backing Object
Create a Java class that will hold the form data:
User.java
// User.java
package com.example.springmvc.models;
public class User {
private String name;
private String email;
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Step 2: Create the Form
Create an HTML form that binds to the form backing object:
userForm.jsp
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head>
<title>User Form</title>
</head>
<body>
<h2>User Form</h2>
<form:form method="post" action="submitUserForm" modelAttribute="user">
<form:label path="name">Name:</form:label>
<form:input path="name" />
<br/>
<form:label path="email">Email:</form:label>
<form:input path="email" />
<br/>
<input type="submit" value="Submit" />
</form:form>
</body>
</html>
Step 3: Create the Controller
Create a controller to handle form submission and binding:
UserController.java
// UserController.java
package com.example.springmvc.controllers;
import com.example.springmvc.models.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class UserController {
@GetMapping("/userForm")
public String showUserForm(Model model) {
model.addAttribute("user", new User());
return "userForm";
}
@PostMapping("/submitUserForm")
public String submitUserForm(@ModelAttribute User user, Model model) {
model.addAttribute("user", user);
return "userDetails";
}
}
Step 4: Create the View for Form Submission Result
Create a JSP file to display the form submission result:
userDetails.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>User Details</title>
</head>
<body>
<h2>User Details</h2>
<p>Name: ${user.name}</p>
<p>Email: ${user.email}</p>
</body>
</html>
Step 5: Validation
To validate the form data, use JSR-303/JSR-380 annotations and a @Valid
annotation in the controller:
User.java (with Validation)
// User.java
package com.example.springmvc.models;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
public class User {
@NotEmpty(message = "Name is required")
private String name;
@NotEmpty(message = "Email is required")
@Email(message = "Email should be valid")
private String email;
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
UserController.java (with Validation)
// UserController.java
package com.example.springmvc.controllers;
import com.example.springmvc.models.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import javax.validation.Valid;
@Controller
public class UserController {
@GetMapping("/userForm")
public String showUserForm(Model model) {
model.addAttribute("user", new User());
return "userForm";
}
@PostMapping("/submitUserForm")
public String submitUserForm(@Valid @ModelAttribute User user, BindingResult result, Model model) {
if (result.hasErrors()) {
return "userForm";
}
model.addAttribute("user", user);
return "userDetails";
}
}
Key Points
- Form Backing Object: A Java object that holds form data.
- @ModelAttribute: Binds a method parameter or method return value to a named model attribute.
- @RequestParam: Binds a request parameter to a method parameter.
- Validation: Ensuring that form data meets certain criteria before processing.
- Create a form backing object to hold form data.
- Create an HTML form to bind to the form backing object.
- Create a controller to handle form submission and binding.
- Create a view to display the form submission result.
- Use validation annotations to ensure form data is valid.
Conclusion
Handling forms in Spring MVC involves creating form backing objects, binding form data, and performing validation. By following these steps and understanding the key concepts, developers can efficiently handle forms and user input in their Spring MVC applications. Happy coding!