From c1f4c574a86d33f216564990870fb6b14ebdcc82 Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 12 Feb 2016 14:38:01 +0100 Subject: [PATCH] Add form tag example --- .../spring/controller/PersonController.java | 53 +++-- .../java/org/baeldung/spring/form/Person.java | 16 ++ .../src/main/resources/messages.properties | 2 + .../src/main/resources/webMvcConfig.xml | 13 ++ .../main/webapp/WEB-INF/view/personForm.jsp | 187 ++++++++++-------- .../main/webapp/WEB-INF/view/personResume.jsp | 4 + .../src/main/webapp/WEB-INF/web.xml | 3 + 7 files changed, 164 insertions(+), 114 deletions(-) create mode 100644 spring-mvc-xml/src/main/resources/messages.properties 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 688e52e51b..d5b68c7516 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 @@ -21,7 +21,28 @@ import org.springframework.web.servlet.ModelAndView; public class PersonController { @RequestMapping(value = "/person", method = RequestMethod.GET) - public ModelAndView initForm(final Model model) { + public ModelAndView showForm(final Model model) { + + initData(model); + return new ModelAndView("personForm", "person", new Person()); + } + + @RequestMapping(value = "/addPerson", method = RequestMethod.POST) + public String submit(@Valid @ModelAttribute("person") final Person person, final BindingResult result, + final ModelMap modelMap, final Model model) { + + if (result.hasErrors()) { + + initData(model); + return "personForm"; + } + + modelMap.addAttribute("person", person); + + return "personResume"; + } + + private void initData(final Model model) { final List favouriteLanguage = new ArrayList(); favouriteLanguage.add("Java"); @@ -52,35 +73,5 @@ public class PersonController { books.add("Nineteen Eighty-Four"); books.add("The Lord of the Rings"); model.addAttribute("books", books); - - return new ModelAndView("personForm", "person", new Person()); } - - @RequestMapping(value = "/addPerson", method = RequestMethod.POST) - public String submit(@Valid @ModelAttribute("person") final Person person, final BindingResult result, - final ModelMap model) { - - if (result.hasErrors()) { - return "error"; - } - - model.addAttribute("person", person); - - return "personResume"; - } - // - // protected Map> referenceData(final - // HttpServletRequest request) throws Exception { - // - // final Map> referenceData = new HashMap<>(); - // - // final List favouriteLanguageList = new ArrayList(); - // favouriteLanguageList.add("Java"); - // favouriteLanguageList.add("C++"); - // favouriteLanguageList.add("Perl"); - // referenceData.put("favouriteLanguageList", favouriteLanguageList); - // - // return referenceData; - // - // } } 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 1b77ab5771..3db8cc4377 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 @@ -2,10 +2,17 @@ package org.baeldung.spring.form; import java.util.List; +import org.hibernate.validator.constraints.NotEmpty; +import org.springframework.web.multipart.MultipartFile; + public class Person { private long id; + + @NotEmpty private String name; + + @NotEmpty private String password; private String sex; private String country; @@ -16,6 +23,7 @@ public class Person { private List favouriteLanguage; private List fruit; private String notes; + private MultipartFile file; public Person() { super(); @@ -117,4 +125,12 @@ public class Person { this.book = book; } + public MultipartFile getFile() { + return file; + } + + public void setFile(final MultipartFile file) { + this.file = file; + } + } diff --git a/spring-mvc-xml/src/main/resources/messages.properties b/spring-mvc-xml/src/main/resources/messages.properties new file mode 100644 index 0000000000..2a187731ae --- /dev/null +++ b/spring-mvc-xml/src/main/resources/messages.properties @@ -0,0 +1,2 @@ +NotEmpty.person.name = Name is required! +NotEmpty.person.password = Password is required! \ No newline at end of file diff --git a/spring-mvc-xml/src/main/resources/webMvcConfig.xml b/spring-mvc-xml/src/main/resources/webMvcConfig.xml index a7aa252c08..f3297fdbf6 100644 --- a/spring-mvc-xml/src/main/resources/webMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/webMvcConfig.xml @@ -11,7 +11,9 @@ > + + @@ -19,4 +21,15 @@ + + + + + + + + + \ 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 f6cf0bf460..9ef02efbae 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 @@ -1,88 +1,109 @@ <%@ 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 - Register a Person - - -

Welcome, Enter The Person Details

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Name
Password
Sex - Male:
- Female: -
Job - -
Country - -
Book - - - - -
Fruit - -
Receive newsletter
Hobbies - Bird watching: - Astronomy: - Snowboarding: -
Favourite languages - -
Notes
-
- - + + + Form Example - Register a Person + + + + + + +

Welcome, Enter The Person Details

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Name
Password
Sex + Male:
+ Female: +
Job + +
Country + +
Book + + + + +
Fruit + +
Receive newsletter
Hobbies + Bird watching: + Astronomy: + Snowboarding: +
Favourite languages + +
Notes
Select a file to upload
+ +
+ + \ No newline at end of file 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 index 81f7fd3411..857738c545 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp @@ -56,6 +56,10 @@ Notes : ${person.notes} + + File : + ${person.file.originalFilename} + \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index 5275efdf24..4e38b2fae6 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -28,6 +28,9 @@ mvc org.springframework.web.servlet.DispatcherServlet 1 + + C:/Users/ivan/Desktop/tmp + mvc