Add form tag example
This commit is contained in:
parent
0fdbd513bc
commit
c1f4c574a8
@ -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<String> favouriteLanguage = new ArrayList<String>();
|
||||
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<String, List<String>> referenceData(final
|
||||
// HttpServletRequest request) throws Exception {
|
||||
//
|
||||
// final Map<String, List<String>> referenceData = new HashMap<>();
|
||||
//
|
||||
// final List<String> favouriteLanguageList = new ArrayList<String>();
|
||||
// favouriteLanguageList.add("Java");
|
||||
// favouriteLanguageList.add("C++");
|
||||
// favouriteLanguageList.add("Perl");
|
||||
// referenceData.put("favouriteLanguageList", favouriteLanguageList);
|
||||
//
|
||||
// return referenceData;
|
||||
//
|
||||
// }
|
||||
}
|
||||
|
@ -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<String> favouriteLanguage;
|
||||
private List<String> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
2
spring-mvc-xml/src/main/resources/messages.properties
Normal file
2
spring-mvc-xml/src/main/resources/messages.properties
Normal file
@ -0,0 +1,2 @@
|
||||
NotEmpty.person.name = Name is required!
|
||||
NotEmpty.person.password = Password is required!
|
@ -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>
|
@ -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"%>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Form Example - Register a Person</title>
|
||||
</head>
|
||||
<body>
|
||||
<h3>Welcome, Enter The Person Details</h3>
|
||||
|
||||
<form:form method="POST" action="/spring-mvc-xml/addPerson" modelAttribute="person">
|
||||
<table>
|
||||
<tr>
|
||||
<td><form:label path="name">Name</form:label></td>
|
||||
<td><form:input path="name" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="password">Password</form:label></td>
|
||||
<td><form:password path="password" /></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="${job}" path="job" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="country">Country</form:label></td>
|
||||
<td>
|
||||
<form:select path="country" items="${country}" />
|
||||
</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="${favouriteLanguage}" 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:hidden path="id" value="12345"/></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><input type="submit" value="Submit" /></td>
|
||||
</tr>
|
||||
</table>
|
||||
</form:form>
|
||||
|
||||
</body>
|
||||
<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;
|
||||
}
|
||||
|
||||
</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="error"/>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td><form:label path="name">Name</form:label></td>
|
||||
<td><form:input path="name" /></td>
|
||||
<td><form:errors path="name" cssClass="error" /></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="${job}" path="job" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="country">Country</form:label></td>
|
||||
<td>
|
||||
<form:select path="country" items="${country}" />
|
||||
</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="${favouriteLanguage}" 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>
|
@ -56,6 +56,10 @@
|
||||
<td>Notes :</td>
|
||||
<td>${person.notes}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>File :</td>
|
||||
<td>${person.file.originalFilename}</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
@ -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>C:/Users/ivan/Desktop/tmp</location>
|
||||
</multipart-config>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>mvc</servlet-name>
|
||||
|
Loading…
x
Reference in New Issue
Block a user