BAEL-112 | Spring MVC Validation - a Custom Validator (#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
This commit is contained in:
parent
fa2f0159a9
commit
2b2dc447bf
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.springmvcforms.controller;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.baeldung.springmvcforms.domain.Customer;
|
||||
import com.baeldung.springmvcforms.validator.CustomerValidator;
|
||||
|
||||
@Controller
|
||||
public class CustomerController {
|
||||
|
||||
@Autowired
|
||||
CustomerValidator validator;
|
||||
|
||||
@RequestMapping(value = "/customer", method = RequestMethod.GET)
|
||||
public ModelAndView showForm() {
|
||||
return new ModelAndView("customerHome", "customer", new Customer());
|
||||
}
|
||||
|
||||
@PostMapping("/addCustomer")
|
||||
public String submit(@Valid @ModelAttribute("customer") final Customer customer, final BindingResult result, final ModelMap model) {
|
||||
validator.validate(customer, result);
|
||||
if (result.hasErrors()) {
|
||||
return "customerHome";
|
||||
}
|
||||
model.addAttribute("customerId", customer.getCustomerId());
|
||||
model.addAttribute("customerName", customer.getCustomerName());
|
||||
model.addAttribute("customerContact", customer.getCustomerContact());
|
||||
model.addAttribute("customerEmail", customer.getCustomerEmail());
|
||||
return "customerView";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.springmvcforms.domain;
|
||||
|
||||
public class Customer {
|
||||
private String customerId;
|
||||
private String customerName;
|
||||
private String customerContact;
|
||||
private String customerEmail;
|
||||
|
||||
public Customer() {
|
||||
super();
|
||||
}
|
||||
|
||||
public String getCustomerId() {
|
||||
return customerId;
|
||||
}
|
||||
|
||||
public void setCustomerId(String customerId) {
|
||||
this.customerId = customerId;
|
||||
}
|
||||
|
||||
public String getCustomerName() {
|
||||
return customerName;
|
||||
}
|
||||
|
||||
public void setCustomerName(String customerName) {
|
||||
this.customerName = customerName;
|
||||
}
|
||||
|
||||
public String getCustomerContact() {
|
||||
return customerContact;
|
||||
}
|
||||
|
||||
public void setCustomerContact(String customerContact) {
|
||||
this.customerContact = customerContact;
|
||||
}
|
||||
|
||||
public String getCustomerEmail() {
|
||||
return customerEmail;
|
||||
}
|
||||
|
||||
public void setCustomerEmail(String customerEmail) {
|
||||
this.customerEmail = customerEmail;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.springmvcforms.validator;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.ValidationUtils;
|
||||
import org.springframework.validation.Validator;
|
||||
|
||||
import com.baeldung.springmvcforms.domain.Customer;
|
||||
|
||||
@Component
|
||||
public class CustomerValidator implements Validator {
|
||||
|
||||
@Override
|
||||
public boolean supports(Class<?> clazz) {
|
||||
return Customer.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(Object target, Errors errors) {
|
||||
|
||||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerId", "error.customerId", "Customer Id is required.");
|
||||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerName", "error.customerName", "Customer Name is required.");
|
||||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerContact", "error.customerNumber", "Customer Contact is required.");
|
||||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerEmail", "error.customerEmail", "Customer Email is required.");
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
|
||||
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Form Example - Add Customer</title>
|
||||
<style>
|
||||
.error {
|
||||
color: #ff0000;
|
||||
font-weight: bold;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h3>Welcome, Enter The Customer Details</h3>
|
||||
|
||||
<form:form method="POST" action="${pageContext.request.contextPath}/addCustomer" modelAttribute="customer">
|
||||
<table>
|
||||
<tr>
|
||||
<td><form:label path="customerId">Customer Id</form:label></td>
|
||||
<td><form:input path="customerId" /></td>
|
||||
<td><form:errors path="customerId" cssClass="error" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="customerName">Customer Name</form:label></td>
|
||||
<td><form:input path="customerName" /></td>
|
||||
<td><form:errors path="customerName" cssClass="error" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="customerContact">Customer Contact</form:label></td>
|
||||
<td><form:input path="customerContact"/></td>
|
||||
<td><form:errors path="customerContact" cssClass="error" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="customerEmail">Customer Email</form:label></td>
|
||||
<td><form:input path="customerEmail" /></td>
|
||||
<td><form:errors path="customerEmail" cssClass="error" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><input type="submit" value="Submit" /></td>
|
||||
</tr>
|
||||
</table>
|
||||
</form:form>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -0,0 +1,28 @@
|
|||
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
|
||||
<html>
|
||||
<head>
|
||||
<title>Spring MVC Form Handling</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h2>Submitted Customer Information</h2>
|
||||
<table>
|
||||
<tr>
|
||||
<td>Customer Id :</td>
|
||||
<td>${customerId}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Customer Name :</td>
|
||||
<td>${customerName}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Customer Contact :</td>
|
||||
<td>${customerContact}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Customer Email :</td>
|
||||
<td>${customerEmail}</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue