Skip to content

Commit 2b2dc44

Browse files
gatmeisterzhendrikse
authored andcommitted
BAEL-112 | Spring MVC Validation - a Custom Validator (eugenp#934)
* BAEL-112 | Spring MVC Validation - a Custom Validator ### Implementation of a custom validator in Employee class using Validator interface * BAEL-112 | Spring MVC Validation - a Custom Validator Implementation of a custom validator in Employee class using Validator interface * BAEL-112 | Spring MVC Validation - a Custom Validator Reverting changes made for a previous module * BAEL-112 | Spring MVC Validation - a Custom Validator Reverting changes to module ‘spring-mvc-java’ * BAEL-112 | Spring MVC Validation - a Custom Validator Reverting changes to module ‘spring-mvc-java’ * BAEL-112 | Spring MVC Validation - a Custom Validator Final reversion of changes to previous module used * BAEL-112 | Spring MVC Validation - a Custom Validator formats the EmployeeValidator class * BAEL-112 | Spring MVC Validation - a Custom Validator Modification from @RequestMapping POST to @PostMapping as per Final Review * BAEL-112 | Spring MVC Validation - a Custom Validator Reverts the changes made to Employee and Employee Controller. Created a new class, Customer as implementation for custom validation * BAEL-112 | Spring MVC Validation - a Custom Validator Reverting EmployeeController * BAEL-112 | Spring MVC Validation - a Custom Validator Reverting EmployeeHome
1 parent fa2f015 commit 2b2dc44

5 files changed

Lines changed: 190 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.springmvcforms.controller;
2+
3+
import javax.validation.Valid;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.ModelMap;
8+
import org.springframework.validation.BindingResult;
9+
import org.springframework.web.bind.annotation.ModelAttribute;
10+
import org.springframework.web.bind.annotation.PostMapping;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestMethod;
13+
import org.springframework.web.servlet.ModelAndView;
14+
15+
import com.baeldung.springmvcforms.domain.Customer;
16+
import com.baeldung.springmvcforms.validator.CustomerValidator;
17+
18+
@Controller
19+
public class CustomerController {
20+
21+
@Autowired
22+
CustomerValidator validator;
23+
24+
@RequestMapping(value = "/customer", method = RequestMethod.GET)
25+
public ModelAndView showForm() {
26+
return new ModelAndView("customerHome", "customer", new Customer());
27+
}
28+
29+
@PostMapping("/addCustomer")
30+
public String submit(@Valid @ModelAttribute("customer") final Customer customer, final BindingResult result, final ModelMap model) {
31+
validator.validate(customer, result);
32+
if (result.hasErrors()) {
33+
return "customerHome";
34+
}
35+
model.addAttribute("customerId", customer.getCustomerId());
36+
model.addAttribute("customerName", customer.getCustomerName());
37+
model.addAttribute("customerContact", customer.getCustomerContact());
38+
model.addAttribute("customerEmail", customer.getCustomerEmail());
39+
return "customerView";
40+
}
41+
42+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.springmvcforms.domain;
2+
3+
public class Customer {
4+
private String customerId;
5+
private String customerName;
6+
private String customerContact;
7+
private String customerEmail;
8+
9+
public Customer() {
10+
super();
11+
}
12+
13+
public String getCustomerId() {
14+
return customerId;
15+
}
16+
17+
public void setCustomerId(String customerId) {
18+
this.customerId = customerId;
19+
}
20+
21+
public String getCustomerName() {
22+
return customerName;
23+
}
24+
25+
public void setCustomerName(String customerName) {
26+
this.customerName = customerName;
27+
}
28+
29+
public String getCustomerContact() {
30+
return customerContact;
31+
}
32+
33+
public void setCustomerContact(String customerContact) {
34+
this.customerContact = customerContact;
35+
}
36+
37+
public String getCustomerEmail() {
38+
return customerEmail;
39+
}
40+
41+
public void setCustomerEmail(String customerEmail) {
42+
this.customerEmail = customerEmail;
43+
}
44+
45+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.springmvcforms.validator;
2+
3+
import org.springframework.stereotype.Component;
4+
import org.springframework.validation.Errors;
5+
import org.springframework.validation.ValidationUtils;
6+
import org.springframework.validation.Validator;
7+
8+
import com.baeldung.springmvcforms.domain.Customer;
9+
10+
@Component
11+
public class CustomerValidator implements Validator {
12+
13+
@Override
14+
public boolean supports(Class<?> clazz) {
15+
return Customer.class.isAssignableFrom(clazz);
16+
}
17+
18+
@Override
19+
public void validate(Object target, Errors errors) {
20+
21+
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerId", "error.customerId", "Customer Id is required.");
22+
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerName", "error.customerName", "Customer Name is required.");
23+
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerContact", "error.customerNumber", "Customer Contact is required.");
24+
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerEmail", "error.customerEmail", "Customer Email is required.");
25+
26+
}
27+
28+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
2+
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
3+
<html>
4+
<head>
5+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6+
<title>Form Example - Add Customer</title>
7+
<style>
8+
.error {
9+
color: #ff0000;
10+
font-weight: bold;
11+
}
12+
</style>
13+
</head>
14+
<body>
15+
<h3>Welcome, Enter The Customer Details</h3>
16+
17+
<form:form method="POST" action="${pageContext.request.contextPath}/addCustomer" modelAttribute="customer">
18+
<table>
19+
<tr>
20+
<td><form:label path="customerId">Customer Id</form:label></td>
21+
<td><form:input path="customerId" /></td>
22+
<td><form:errors path="customerId" cssClass="error" /></td>
23+
</tr>
24+
<tr>
25+
<td><form:label path="customerName">Customer Name</form:label></td>
26+
<td><form:input path="customerName" /></td>
27+
<td><form:errors path="customerName" cssClass="error" /></td>
28+
</tr>
29+
<tr>
30+
<td><form:label path="customerContact">Customer Contact</form:label></td>
31+
<td><form:input path="customerContact"/></td>
32+
<td><form:errors path="customerContact" cssClass="error" /></td>
33+
</tr>
34+
<tr>
35+
<td><form:label path="customerEmail">Customer Email</form:label></td>
36+
<td><form:input path="customerEmail" /></td>
37+
<td><form:errors path="customerEmail" cssClass="error" /></td>
38+
</tr>
39+
<tr>
40+
<td><input type="submit" value="Submit" /></td>
41+
</tr>
42+
</table>
43+
</form:form>
44+
45+
</body>
46+
47+
</html>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
2+
<html>
3+
<head>
4+
<title>Spring MVC Form Handling</title>
5+
</head>
6+
<body>
7+
8+
<h2>Submitted Customer Information</h2>
9+
<table>
10+
<tr>
11+
<td>Customer Id :</td>
12+
<td>${customerId}</td>
13+
</tr>
14+
<tr>
15+
<td>Customer Name :</td>
16+
<td>${customerName}</td>
17+
</tr>
18+
<tr>
19+
<td>Customer Contact :</td>
20+
<td>${customerContact}</td>
21+
</tr>
22+
<tr>
23+
<td>Customer Email :</td>
24+
<td>${customerEmail}</td>
25+
</tr>
26+
</table>
27+
</body>
28+
</html>

0 commit comments

Comments
 (0)