From 2b2dc447bf4f72b933f770db1e6fcf85e23170b7 Mon Sep 17 00:00:00 2001 From: gatmeister Date: Sun, 15 Jan 2017 06:49:14 +0800 Subject: [PATCH] BAEL-112 | Spring MVC Validation - a Custom Validator (#934) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- .../controller/CustomerController.java | 42 +++++++++++++++++ .../springmvcforms/domain/Customer.java | 45 ++++++++++++++++++ .../validator/CustomerValidator.java | 28 +++++++++++ .../webapp/WEB-INF/views/customerHome.jsp | 47 +++++++++++++++++++ .../webapp/WEB-INF/views/customerView.jsp | 28 +++++++++++ 5 files changed, 190 insertions(+) create mode 100644 spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/CustomerController.java create mode 100644 spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/domain/Customer.java create mode 100644 spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/validator/CustomerValidator.java create mode 100644 spring-mvc-forms/src/main/webapp/WEB-INF/views/customerHome.jsp create mode 100644 spring-mvc-forms/src/main/webapp/WEB-INF/views/customerView.jsp diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/CustomerController.java b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/CustomerController.java new file mode 100644 index 0000000000..586c2467fe --- /dev/null +++ b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/CustomerController.java @@ -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"; + } + +} diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/domain/Customer.java b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/domain/Customer.java new file mode 100644 index 0000000000..8ecb2aa503 --- /dev/null +++ b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/domain/Customer.java @@ -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; + } + +} diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/validator/CustomerValidator.java b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/validator/CustomerValidator.java new file mode 100644 index 0000000000..94a7bd0ebd --- /dev/null +++ b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/validator/CustomerValidator.java @@ -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."); + + } + +} diff --git a/spring-mvc-forms/src/main/webapp/WEB-INF/views/customerHome.jsp b/spring-mvc-forms/src/main/webapp/WEB-INF/views/customerHome.jsp new file mode 100644 index 0000000000..df34a47cc0 --- /dev/null +++ b/spring-mvc-forms/src/main/webapp/WEB-INF/views/customerHome.jsp @@ -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"%> + + + +Form Example - Add Customer + + + +

Welcome, Enter The Customer Details

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Customer Id
Customer Name
Customer Contact
Customer Email
+
+ + + + \ No newline at end of file diff --git a/spring-mvc-forms/src/main/webapp/WEB-INF/views/customerView.jsp b/spring-mvc-forms/src/main/webapp/WEB-INF/views/customerView.jsp new file mode 100644 index 0000000000..ab2631bd02 --- /dev/null +++ b/spring-mvc-forms/src/main/webapp/WEB-INF/views/customerView.jsp @@ -0,0 +1,28 @@ +<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> + + +Spring MVC Form Handling + + + +

Submitted Customer Information

+ + + + + + + + + + + + + + + + + +
Customer Id :${customerId}
Customer Name :${customerName}
Customer Contact :${customerContact}
Customer Email :${customerEmail}
+ + \ No newline at end of file