From 0fdbd513bcfdc936228c4485caf0b7e5ff15d294 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 12 Feb 2016 00:09:58 +0100 Subject: [PATCH] Add form tag example --- .../spring/controller/PersonController.java | 86 +++++++++++++ .../java/org/baeldung/spring/form/Person.java | 120 ++++++++++++++++++ .../main/webapp/WEB-INF/view/personForm.jsp | 88 +++++++++++++ .../main/webapp/WEB-INF/view/personResume.jsp | 61 +++++++++ 4 files changed, 355 insertions(+) create mode 100644 spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java create mode 100644 spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp 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 new file mode 100644 index 0000000000..688e52e51b --- /dev/null +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java @@ -0,0 +1,86 @@ +package org.baeldung.spring.controller; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import javax.validation.Valid; + +import org.baeldung.spring.form.Person; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +@Controller +public class PersonController { + + @RequestMapping(value = "/person", method = RequestMethod.GET) + public ModelAndView initForm(final Model model) { + + final List favouriteLanguage = new ArrayList(); + favouriteLanguage.add("Java"); + favouriteLanguage.add("C++"); + favouriteLanguage.add("Perl"); + model.addAttribute("favouriteLanguage", favouriteLanguage); + + final List job = new ArrayList(); + job.add("Full time"); + job.add("Part time"); + model.addAttribute("job", job); + + 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 List fruit = new ArrayList(); + fruit.add("Banana"); + fruit.add("Mango"); + fruit.add("Apple"); + model.addAttribute("fruit", fruit); + + final List books = new ArrayList(); + books.add("The Great Gatsby"); + 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 new file mode 100644 index 0000000000..1b77ab5771 --- /dev/null +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java @@ -0,0 +1,120 @@ +package org.baeldung.spring.form; + +import java.util.List; + +public class Person { + + private long id; + private String name; + private String password; + private String sex; + private String country; + private String book; + private String job; + boolean receiveNewsletter; + private String[] hobbies; + private List favouriteLanguage; + private List fruit; + private String notes; + + public Person() { + super(); + } + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(final String password) { + this.password = password; + } + + public String getSex() { + return sex; + } + + public void setSex(final String sex) { + this.sex = sex; + } + + public String getCountry() { + return country; + } + + public void setCountry(final String country) { + this.country = country; + } + + public String getJob() { + return job; + } + + public void setJob(final String job) { + this.job = job; + } + + public boolean isReceiveNewsletter() { + return receiveNewsletter; + } + + public void setReceiveNewsletter(final boolean receiveNewsletter) { + this.receiveNewsletter = receiveNewsletter; + } + + public String[] getHobbies() { + return hobbies; + } + + public void setHobbies(final String[] hobbies) { + this.hobbies = hobbies; + } + + public List getFavouriteLanguage() { + return favouriteLanguage; + } + + public void setFavouriteLanguage(final List favouriteLanguage) { + this.favouriteLanguage = favouriteLanguage; + } + + public String getNotes() { + return notes; + } + + public void setNotes(final String notes) { + this.notes = notes; + } + + public List getFruit() { + return fruit; + } + + public void setFruit(final List fruit) { + this.fruit = fruit; + } + + public String getBook() { + return book; + } + + public void setBook(final String book) { + this.book = book; + } + +} 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 new file mode 100644 index 0000000000..f6cf0bf460 --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp @@ -0,0 +1,88 @@ +<%@ 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
+
+ + + + \ 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 new file mode 100644 index 0000000000..81f7fd3411 --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp @@ -0,0 +1,61 @@ +<%@ 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}
+ + \ No newline at end of file