Merge pull request #373 from ivanp81/master

Spring MVC Form Tag Library example
This commit is contained in:
David Morley 2016-03-03 06:18:38 -06:00
commit 0385e78ff5
8 changed files with 480 additions and 0 deletions

View File

@ -0,0 +1,84 @@
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.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;
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 {
@Autowired
PersonValidator validator;
@RequestMapping(value = "/person", method = RequestMethod.GET)
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) {
validator.validate(person, result);
if (result.hasErrors()) {
initData(model);
return "personForm";
}
modelMap.addAttribute("person", person);
return "personView";
}
private void initData(final Model model) {
final List<String> favouriteLanguageItem = new ArrayList<String>();
favouriteLanguageItem.add("Java");
favouriteLanguageItem.add("C++");
favouriteLanguageItem.add("Perl");
model.addAttribute("favouriteLanguageItem", favouriteLanguageItem);
final List<String> jobItem = new ArrayList<String>();
jobItem.add("Full time");
jobItem.add("Part time");
model.addAttribute("jobItem", jobItem);
final Map<String, String> countryItems = new LinkedHashMap<String, String>();
countryItems.put("US", "United Stated");
countryItems.put("IT", "Italy");
countryItems.put("UK", "United Kingdom");
countryItems.put("FR", "Grance");
model.addAttribute("countryItems", countryItems);
final List<String> fruit = new ArrayList<String>();
fruit.add("Banana");
fruit.add("Mango");
fruit.add("Apple");
model.addAttribute("fruit", fruit);
final List<String> books = new ArrayList<String>();
books.add("The Great Gatsby");
books.add("Nineteen Eighty-Four");
books.add("The Lord of the Rings");
model.addAttribute("books", books);
}
}

View File

@ -0,0 +1,152 @@
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;
private String name;
private String email;
private String dateOfBirth;
@NotEmpty
private String password;
private String sex;
private String country;
private String book;
private String job;
private boolean receiveNewsletter;
private String[] hobbies;
private List<String> favouriteLanguage;
private List<String> fruit;
private String notes;
private MultipartFile file;
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 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;
}
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<String> getFavouriteLanguage() {
return favouriteLanguage;
}
public void setFavouriteLanguage(final List<String> favouriteLanguage) {
this.favouriteLanguage = favouriteLanguage;
}
public String getNotes() {
return notes;
}
public void setNotes(final String notes) {
this.notes = notes;
}
public List<String> getFruit() {
return fruit;
}
public void setFruit(final List<String> fruit) {
this.fruit = fruit;
}
public String getBook() {
return book;
}
public void setBook(final String book) {
this.book = book;
}
public MultipartFile getFile() {
return file;
}
public void setFile(final MultipartFile file) {
this.file = file;
}
}

View File

@ -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");
}
}

View File

@ -0,0 +1,2 @@
required.name = Name is required!
NotEmpty.person.password = Password is required!

View File

@ -11,7 +11,9 @@
>
<mvc:annotation-driven/>
<context:component-scan base-package="org.baeldung.spring.controller"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
@ -19,4 +21,15 @@
<mvc:view-controller path="/sample.html" view-name="sample"/>
<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
<property name="basename" value="messages" />
</bean>
<!-- <bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>
</beans>

View File

@ -0,0 +1,124 @@
<%@ 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 - Register a Person</title>
</head>
<style>
.error {
color: #ff0000;
}
.errorbox {
background-color: #ffEEEE;
border: 2px solid #ff0000;
}
</style>
<body>
<h3>Welcome, Enter The Person Details</h3>
<form:form method="POST" action="/spring-mvc-xml/addPerson" modelAttribute="person" enctype="multipart/form-data">
<form:errors path="*" cssClass="errorbox" element="div" />
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
<td><form:errors path="name" cssClass="error" element="div"/></td>
</tr>
<tr>
<td><form:label path="email">E-mail</form:label></td>
<td><form:input type="email" path="email" /></td>
<td><form:errors path="email" cssClass="error" /></td>
</tr>
<tr>
<td><form:label path="dateOfBirth">Date of birth</form:label></td>
<td><form:input type="date" path="dateOfBirth" /></td>
</tr>
<tr>
<td><form:label path="password">Password</form:label></td>
<td><form:password path="password" /></td>
<td><form:errors path="password" cssClass="error" /></td>
</tr>
<tr>
<td><form:label path="sex">Sex</form:label></td>
<td>
Male: <form:radiobutton path="sex" value="M"/> <br/>
Female: <form:radiobutton path="sex" value="F"/>
</td>
</tr>
<tr>
<td><form:label path="job">Job</form:label></td>
<td>
<form:radiobuttons items="${jobItem}" path="job" />
</td>
</tr>
<tr>
<td><form:label path="country">Country</form:label></td>
<td>
<form:select path="country" items="${countryItems}" />
</td>
</tr>
<tr>
<td><form:label path="book">Book</form:label></td>
<td>
<form:select path="book">
<form:option value="-" label="--Please Select"/>
<form:options items="${books}" />
</form:select>
</td>
</tr>
<tr>
<td><form:label path="fruit">Fruit</form:label></td>
<td>
<form:select path="fruit" items="${fruit}" multiple="true"/>
</td>
</tr>
<tr>
<td><form:label path="receiveNewsletter">Receive newsletter</form:label></td>
<td><form:checkbox path="receiveNewsletter" rows="3" cols="20"/></td>
</tr>
<tr>
<td><form:label path="hobbies">Hobbies</form:label></td>
<td>
Bird watching: <form:checkbox path="hobbies" value="Bird watching"/>
Astronomy: <form:checkbox path="hobbies" value="Astronomy"/>
Snowboarding: <form:checkbox path="hobbies" value="Snowboarding"/>
</td>
</tr>
<tr>
<td><form:label path="favouriteLanguage">Favourite languages</form:label></td>
<td>
<form:checkboxes items="${favouriteLanguageItem}" path="favouriteLanguage" />
</td>
</tr>
<tr>
<td><form:label path="notes">Notes</form:label></td>
<td><form:textarea path="notes" rows="3" cols="20"/></td>
</tr>
<tr>
<td><form:label path="file">Select a file to upload</form:label></td>
<td><input type="file" name="file" /></td>
</tr>
<tr>
<td><form:hidden path="id" value="12345"/></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>

View File

@ -0,0 +1,80 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Spring MVC Form Handling</title>
</head>
<body>
<h2>Submitted Person Information</h2>
<table>
<tr>
<td>Id :</td>
<td>${person.id}</td>
</tr>
<tr>
<td>Name :</td>
<td>${person.name}</td>
</tr>
<tr>
<td>Date of birth :</td>
<td>${person.dateOfBirth}</td>
</tr>
<tr>
<td>Password :</td>
<td>${person.password}</td>
</tr>
<tr>
<td>Sex :</td>
<td>${person.sex}</td>
</tr>
<tr>
<td>Job :</td>
<td>${person.job}</td>
</tr>
<tr>
<td>Country :</td>
<td>${person.country}</td>
</tr>
<tr>
<td>Fruit :</td>
<td><c:forEach items="${person.fruit}" var="current">[<c:out value="${current}" />]</c:forEach></td>
</tr>
<tr>
<td>Book :</td>
<td>${person.book}</td>
</tr>
<tr>
<td>Receive Newsletter :</td>
<td>${person.receiveNewsletter}</td>
</tr>
<tr>
<td>Hobbies :</td>
<td><c:forEach items="${person.hobbies}" var="current">[<c:out value="${current}" />]</c:forEach></td>
</tr>
<tr>
<td>Favourite Languages :</td>
<td><c:forEach items="${person.favouriteLanguage}" var="current">[<c:out value="${current}" />]</c:forEach></td>
</tr>
<tr>
<td>Notes :</td>
<td>${person.notes}</td>
</tr>
</table>
<h2>Submitted File</h2>
<table>
<tr>
<td>OriginalFileName :</td>
<td>${person.file.originalFilename}</td>
</tr>
<tr>
<td>Type :</td>
<td>${person.file.contentType}</td>
</tr>
</table>
</body>
</html>

View File

@ -28,6 +28,9 @@
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<multipart-config>
<location>/tmp</location>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>