Add form tag example
This commit is contained in:
parent
c1f4c574a8
commit
90853626d2
|
@ -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<String> favouriteLanguage = new ArrayList<String>();
|
||||
favouriteLanguage.add("Java");
|
||||
favouriteLanguage.add("C++");
|
||||
favouriteLanguage.add("Perl");
|
||||
model.addAttribute("favouriteLanguage", favouriteLanguage);
|
||||
final List<String> favouriteLanguageItem = new ArrayList<String>();
|
||||
favouriteLanguageItem.add("Java");
|
||||
favouriteLanguageItem.add("C++");
|
||||
favouriteLanguageItem.add("Perl");
|
||||
model.addAttribute("favouriteLanguageItem", favouriteLanguageItem);
|
||||
|
||||
final List<String> job = new ArrayList<String>();
|
||||
job.add("Full time");
|
||||
job.add("Part time");
|
||||
model.addAttribute("job", job);
|
||||
final List<String> jobItem = new ArrayList<String>();
|
||||
jobItem.add("Full time");
|
||||
jobItem.add("Part time");
|
||||
model.addAttribute("jobItem", jobItem);
|
||||
|
||||
final Map<String, String> country = new LinkedHashMap<String, String>();
|
||||
country.put("US", "United Stated");
|
||||
country.put("IT", "Italy");
|
||||
country.put("UK", "United Kingdom");
|
||||
country.put("FR", "Grance");
|
||||
model.addAttribute("country", country);
|
||||
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");
|
||||
|
|
|
@ -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<String> favouriteLanguage;
|
||||
private List<String> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -28,6 +28,15 @@
|
|||
<td><form:input path="name" /></td>
|
||||
<td><form:errors path="name" cssClass="error" /></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>
|
||||
|
@ -43,13 +52,13 @@
|
|||
<tr>
|
||||
<td><form:label path="job">Job</form:label></td>
|
||||
<td>
|
||||
<form:radiobuttons items="${job}" path="job" />
|
||||
<form:radiobuttons items="${jobItem}" path="job" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="country">Country</form:label></td>
|
||||
<td>
|
||||
<form:select path="country" items="${country}" />
|
||||
<form:select path="country" items="${countryItems}" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
|
@ -82,7 +91,7 @@
|
|||
<tr>
|
||||
<td><form:label path="favouriteLanguage">Favourite languages</form:label></td>
|
||||
<td>
|
||||
<form:checkboxes items="${favouriteLanguage}" path="favouriteLanguage" />
|
||||
<form:checkboxes items="${favouriteLanguageItem}" path="favouriteLanguage" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
|
|
|
@ -1,65 +0,0 @@
|
|||
<%@ 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>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>
|
||||
<tr>
|
||||
<td>File :</td>
|
||||
<td>${person.file.originalFilename}</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
|
@ -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>
|
Loading…
Reference in New Issue