diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java index d5b68c7516..0d89a29b9d 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java @@ -8,6 +8,8 @@ import java.util.Map; import javax.validation.Valid; import org.baeldung.spring.form.Person; +import org.baeldung.spring.validator.PersonValidator; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; @@ -20,6 +22,9 @@ import org.springframework.web.servlet.ModelAndView; @Controller public class PersonController { + @Autowired + PersonValidator validator; + @RequestMapping(value = "/person", method = RequestMethod.GET) public ModelAndView showForm(final Model model) { @@ -31,6 +36,8 @@ public class PersonController { public String submit(@Valid @ModelAttribute("person") final Person person, final BindingResult result, final ModelMap modelMap, final Model model) { + validator.validate(person, result); + if (result.hasErrors()) { initData(model); @@ -39,28 +46,28 @@ public class PersonController { modelMap.addAttribute("person", person); - return "personResume"; + return "personView"; } private void initData(final Model model) { - final List favouriteLanguage = new ArrayList(); - favouriteLanguage.add("Java"); - favouriteLanguage.add("C++"); - favouriteLanguage.add("Perl"); - model.addAttribute("favouriteLanguage", favouriteLanguage); + final List favouriteLanguageItem = new ArrayList(); + favouriteLanguageItem.add("Java"); + favouriteLanguageItem.add("C++"); + favouriteLanguageItem.add("Perl"); + model.addAttribute("favouriteLanguageItem", favouriteLanguageItem); - final List job = new ArrayList(); - job.add("Full time"); - job.add("Part time"); - model.addAttribute("job", job); + final List jobItem = new ArrayList(); + jobItem.add("Full time"); + jobItem.add("Part time"); + model.addAttribute("jobItem", jobItem); - final Map country = new LinkedHashMap(); - country.put("US", "United Stated"); - country.put("IT", "Italy"); - country.put("UK", "United Kingdom"); - country.put("FR", "Grance"); - model.addAttribute("country", country); + final Map countryItems = new LinkedHashMap(); + countryItems.put("US", "United Stated"); + countryItems.put("IT", "Italy"); + countryItems.put("UK", "United Kingdom"); + countryItems.put("FR", "Grance"); + model.addAttribute("countryItems", countryItems); final List fruit = new ArrayList(); fruit.add("Banana"); diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java index 3db8cc4377..bf313b4d7d 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java @@ -9,8 +9,9 @@ public class Person { private long id; - @NotEmpty private String name; + private String email; + private String dateOfBirth; @NotEmpty private String password; @@ -18,7 +19,7 @@ public class Person { private String country; private String book; private String job; - boolean receiveNewsletter; + private boolean receiveNewsletter; private String[] hobbies; private List favouriteLanguage; private List fruit; @@ -45,6 +46,22 @@ public class Person { this.name = name; } + public String getEmail() { + return email; + } + + public void setEmail(final String email) { + this.email = email; + } + + public String getDateOfBirth() { + return dateOfBirth; + } + + public void setDateOfBirth(final String dateOfBirth) { + this.dateOfBirth = dateOfBirth; + } + public String getPassword() { return password; } @@ -132,5 +149,4 @@ public class Person { public void setFile(final MultipartFile file) { this.file = file; } - } diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/validator/PersonValidator.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/validator/PersonValidator.java new file mode 100644 index 0000000000..068a9fee7f --- /dev/null +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/validator/PersonValidator.java @@ -0,0 +1,22 @@ +package org.baeldung.spring.validator; + +import org.baeldung.spring.form.Person; +import org.springframework.stereotype.Component; +import org.springframework.validation.Errors; +import org.springframework.validation.ValidationUtils; +import org.springframework.validation.Validator; + +@Component +public class PersonValidator implements Validator { + + @Override + public boolean supports(final Class calzz) { + return Person.class.isAssignableFrom(calzz); + } + + @Override + public void validate(final Object obj, final Errors errors) { + + ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "required.name"); + } +} \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp index 9ef02efbae..9b0a78899c 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp @@ -28,6 +28,15 @@ + + E-mail + + + + + Date of birth + + Password @@ -43,13 +52,13 @@ Job - + Country - + @@ -82,7 +91,7 @@ Favourite languages - + diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp deleted file mode 100644 index 857738c545..0000000000 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp +++ /dev/null @@ -1,65 +0,0 @@ -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> - - - -Spring MVC Form Handling - - - -

Submitted Person Information

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Id :${person.id}
Name :${person.name}
Password :${person.password}
Sex :${person.sex}
Job :${person.job}
Country :${person.country}
Fruit :[]
Book :${person.book}
Receive Newsletter :${person.receiveNewsletter}
Hobbies :[]
Favourite Languages :[]
Notes :${person.notes}
File :${person.file.originalFilename}
- - \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp new file mode 100644 index 0000000000..8893314d20 --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp @@ -0,0 +1,80 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + Spring MVC Form Handling + + + +

Submitted Person Information

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Id :${person.id}
Name :${person.name}
Date of birth :${person.dateOfBirth}
Password :${person.password}
Sex :${person.sex}
Job :${person.job}
Country :${person.country}
Fruit :[]
Book :${person.book}
Receive Newsletter :${person.receiveNewsletter}
Hobbies :[]
Favourite Languages :[]
Notes :${person.notes}
+ +

Submitted File

+ + + + + + + + + + + + +
OriginalFileName :${person.file.originalFilename}
Type :${person.file.contentType}
+ + \ No newline at end of file