Add form tag example

This commit is contained in:
Ivan 2016-02-12 14:38:01 +01:00
parent 0fdbd513bc
commit c1f4c574a8
7 changed files with 164 additions and 114 deletions

View File

@ -21,7 +21,28 @@ import org.springframework.web.servlet.ModelAndView;
public class PersonController { public class PersonController {
@RequestMapping(value = "/person", method = RequestMethod.GET) @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>(); final List<String> favouriteLanguage = new ArrayList<String>();
favouriteLanguage.add("Java"); favouriteLanguage.add("Java");
@ -52,35 +73,5 @@ public class PersonController {
books.add("Nineteen Eighty-Four"); books.add("Nineteen Eighty-Four");
books.add("The Lord of the Rings"); books.add("The Lord of the Rings");
model.addAttribute("books", books); 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;
//
// }
} }

View File

@ -2,10 +2,17 @@ package org.baeldung.spring.form;
import java.util.List; import java.util.List;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.multipart.MultipartFile;
public class Person { public class Person {
private long id; private long id;
@NotEmpty
private String name; private String name;
@NotEmpty
private String password; private String password;
private String sex; private String sex;
private String country; private String country;
@ -16,6 +23,7 @@ public class Person {
private List<String> favouriteLanguage; private List<String> favouriteLanguage;
private List<String> fruit; private List<String> fruit;
private String notes; private String notes;
private MultipartFile file;
public Person() { public Person() {
super(); super();
@ -117,4 +125,12 @@ public class Person {
this.book = book; this.book = book;
} }
public MultipartFile getFile() {
return file;
}
public void setFile(final MultipartFile file) {
this.file = file;
}
} }

View File

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

View File

@ -11,7 +11,9 @@
> >
<mvc:annotation-driven/> <mvc:annotation-driven/>
<context:component-scan base-package="org.baeldung.spring.controller"/> <context:component-scan base-package="org.baeldung.spring.controller"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/> <property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/> <property name="suffix" value=".jsp"/>
@ -19,4 +21,15 @@
<mvc:view-controller path="/sample.html" view-name="sample"/> <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> </beans>

View File

@ -5,18 +5,33 @@
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Form Example - Register a Person</title> <title>Form Example - Register a Person</title>
</head> </head>
<style>
.error {
color: #ff0000;
}
</style>
<body> <body>
<h3>Welcome, Enter The Person Details</h3> <h3>Welcome, Enter The Person Details</h3>
<form:form method="POST" action="/spring-mvc-xml/addPerson" modelAttribute="person"> <form:form method="POST" action="/spring-mvc-xml/addPerson" modelAttribute="person" enctype="multipart/form-data">
<form:errors path="*" cssClass="error"/>
<table> <table>
<tr> <tr>
<td><form:label path="name">Name</form:label></td> <td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td> <td><form:input path="name" /></td>
<td><form:errors path="name" cssClass="error" /></td>
</tr> </tr>
<tr> <tr>
<td><form:label path="password">Password</form:label></td> <td><form:label path="password">Password</form:label></td>
<td><form:password path="password" /></td> <td><form:password path="password" /></td>
<td><form:errors path="password" cssClass="error" /></td>
</tr> </tr>
<tr> <tr>
<td><form:label path="sex">Sex</form:label></td> <td><form:label path="sex">Sex</form:label></td>
@ -74,13 +89,19 @@
<td><form:label path="notes">Notes</form:label></td> <td><form:label path="notes">Notes</form:label></td>
<td><form:textarea path="notes" rows="3" cols="20"/></td> <td><form:textarea path="notes" rows="3" cols="20"/></td>
</tr> </tr>
<tr>
<td><form:label path="file">Select a file to upload</form:label></td>
<td><input type="file" name="file" /></td>
</tr>
<tr> <tr>
<td><form:hidden path="id" value="12345"/></td> <td><form:hidden path="id" value="12345"/></td>
</tr> </tr>
<tr> <tr>
<td><input type="submit" value="Submit" /></td> <td><input type="submit" value="Submit" /></td>
</tr> </tr>
</table> </table>
</form:form> </form:form>
</body> </body>

View File

@ -56,6 +56,10 @@
<td>Notes :</td> <td>Notes :</td>
<td>${person.notes}</td> <td>${person.notes}</td>
</tr> </tr>
<tr>
<td>File :</td>
<td>${person.file.originalFilename}</td>
</tr>
</table> </table>
</body> </body>
</html> </html>

View File

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