Skip to content

Commit 5b73831

Browse files
committed
Add Spring MVC form and binding example
1 parent b846bcc commit 5b73831

8 files changed

Lines changed: 339 additions & 0 deletions

File tree

‎spring-mvc-forms/pom.xml‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.baeldung</groupId>
7+
<version>0.1-SNAPSHOT</version>
8+
<artifactId>spring-mvc-forms</artifactId>
9+
10+
<name>spring-mvc-forms</name>
11+
<packaging>war</packaging>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.springframework</groupId>
16+
<artifactId>spring-webmvc</artifactId>
17+
<version>${springframework.version}</version>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>javax.servlet</groupId>
22+
<artifactId>javax.servlet-api</artifactId>
23+
<version>${javax.servlet-api.version}</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>javax.servlet.jsp</groupId>
28+
<artifactId>javax.servlet.jsp-api</artifactId>
29+
<version>${javax.servlet.jsp-api.version}</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>javax.servlet</groupId>
34+
<artifactId>jstl</artifactId>
35+
<version>${jstl.version}</version>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.hibernate</groupId>
40+
<artifactId>hibernate-validator</artifactId>
41+
<version>${hibernate-validator.version}</version>
42+
</dependency>
43+
44+
45+
</dependencies>
46+
<profiles>
47+
<!-- Local -->
48+
<profile>
49+
<id>spring-mvc-forms</id>
50+
<activation>
51+
<activeByDefault>true</activeByDefault>
52+
</activation>
53+
<build>
54+
<pluginManagement>
55+
<plugins>
56+
57+
<plugin>
58+
<groupId>org.apache.maven.plugins</groupId>
59+
<artifactId>maven-compiler-plugin</artifactId>
60+
<version>${maven-compiler-plugin.version}</version>
61+
<configuration>
62+
<source>${maven-compiler-plugin.source}</source>
63+
<target>${maven-compiler-plugin.source}</target>
64+
</configuration>
65+
</plugin>
66+
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-war-plugin</artifactId>
70+
<version>${maven-war-plugin.version}</version>
71+
<configuration>
72+
<warSourceDirectory>src/main/webapp</warSourceDirectory>
73+
<warName>spring-mvc-forms</warName>
74+
<failOnMissingWebXml>false</failOnMissingWebXml>
75+
<outputDirectory>${deploy-path}</outputDirectory>
76+
</configuration>
77+
</plugin>
78+
79+
</plugins>
80+
</pluginManagement>
81+
<finalName>spring-mvc-forms</finalName>
82+
</build>
83+
</profile>
84+
</profiles>
85+
86+
<properties>
87+
<springframework.version>4.0.6.RELEASE</springframework.version>
88+
<maven-war-plugin.version>2.4</maven-war-plugin.version>
89+
<jstl.version>1.2</jstl.version>
90+
<javax.servlet.jsp-api.version>2.3.1</javax.servlet.jsp-api.version>
91+
<javax.servlet-api.version>3.1.0</javax.servlet-api.version>
92+
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
93+
<maven-compiler-plugin.source>1.8</maven-compiler-plugin.source>
94+
<hibernate-validator.version>5.1.1.Final</hibernate-validator.version>
95+
<deploy-path>enter-location-of-server</deploy-path>
96+
</properties>
97+
98+
</project>
99+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.springmvcforms.configuration;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
7+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
9+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
10+
11+
@Configuration
12+
@EnableWebMvc
13+
@ComponentScan(basePackages = "com.baeldung.springmvcforms")
14+
class ApplicationConfiguration extends WebMvcConfigurerAdapter {
15+
16+
@Override
17+
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
18+
configurer.enable();
19+
}
20+
21+
@Bean
22+
public InternalResourceViewResolver jspViewResolver() {
23+
InternalResourceViewResolver bean = new InternalResourceViewResolver();
24+
bean.setPrefix("/WEB-INF/views/");
25+
bean.setSuffix(".jsp");
26+
return bean;
27+
}
28+
29+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.springmvcforms.configuration;
2+
3+
import org.springframework.web.WebApplicationInitializer;
4+
import org.springframework.web.context.ContextLoaderListener;
5+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
6+
import org.springframework.web.servlet.DispatcherServlet;
7+
8+
import javax.servlet.ServletContext;
9+
import javax.servlet.ServletException;
10+
import javax.servlet.ServletRegistration;
11+
12+
public class WebInitializer implements WebApplicationInitializer {
13+
14+
public void onStartup(ServletContext container) throws ServletException {
15+
16+
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
17+
ctx.register(ApplicationConfiguration.class);
18+
ctx.setServletContext(container);
19+
20+
// Manage the lifecycle of the root application context
21+
container.addListener(new ContextLoaderListener(ctx));
22+
23+
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
24+
25+
servlet.setLoadOnStartup(1);
26+
servlet.addMapping("/");
27+
}
28+
// @Override
29+
// public void onStartup(ServletContext container) {
30+
// // Create the 'root' Spring application context
31+
// AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
32+
// rootContext.register(ServiceConfig.class, JPAConfig.class, SecurityConfig.class);
33+
//
34+
// // Manage the lifecycle of the root application context
35+
// container.addListener(new ContextLoaderListener(rootContext));
36+
//
37+
// // Create the dispatcher servlet's Spring application context
38+
// AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
39+
// dispatcherServlet.register(MvcConfig.class);
40+
//
41+
// // Register and map the dispatcher servlet
42+
// ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
43+
// dispatcher.setLoadOnStartup(1);
44+
// dispatcher.addMapping("/");
45+
//
46+
// }
47+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.springmvcforms.controller;
2+
3+
import com.baeldung.springmvcforms.domain.Employee;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.ui.ModelMap;
6+
import org.springframework.validation.BindingResult;
7+
import org.springframework.web.bind.annotation.*;
8+
import org.springframework.web.servlet.ModelAndView;
9+
10+
import javax.validation.Valid;
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
@Controller
15+
public class EmployeeController {
16+
17+
Map<Long, Employee> employeeMap = new HashMap<>();
18+
19+
@RequestMapping(value = "/employee", method = RequestMethod.GET)
20+
public ModelAndView showForm() {
21+
return new ModelAndView("employeeHome", "employee", new Employee());
22+
}
23+
24+
@RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET)
25+
public @ResponseBody Employee getEmployeeById(@PathVariable final long Id) {
26+
return employeeMap.get(Id);
27+
}
28+
29+
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
30+
public String submit(@Valid @ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) {
31+
if (result.hasErrors()) {
32+
return "error";
33+
}
34+
model.addAttribute("name", employee.getName());
35+
model.addAttribute("contactNumber", employee.getContactNumber());
36+
model.addAttribute("id", employee.getId());
37+
employeeMap.put(employee.getId(), employee);
38+
return "employeeView";
39+
}
40+
41+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.springmvcforms.domain;
2+
3+
import javax.validation.constraints.NotNull;
4+
import javax.validation.constraints.Size;
5+
6+
public class Employee {
7+
8+
private long id;
9+
10+
@NotNull
11+
@Size(min = 5)
12+
private String name;
13+
14+
@NotNull
15+
@Size(min = 7)
16+
private String contactNumber;
17+
18+
public Employee() {
19+
super();
20+
}
21+
22+
public String getName() {
23+
return name;
24+
}
25+
26+
public void setName(final String name) {
27+
this.name = name;
28+
}
29+
30+
public long getId() {
31+
return id;
32+
}
33+
34+
public void setId(final long id) {
35+
this.id = id;
36+
}
37+
38+
public String getContactNumber() {
39+
return contactNumber;
40+
}
41+
42+
public void setContactNumber(final String contactNumber) {
43+
this.contactNumber = contactNumber;
44+
}
45+
46+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
2+
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
3+
<html>
4+
<head>
5+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6+
<title>Form Example - Register an Employee</title>
7+
</head>
8+
<body>
9+
<h3>Welcome, Enter The Employee Details</h3>
10+
11+
<form:form method="POST" action="${pageContext.request.contextPath}/addEmployee" modelAttribute="employee">
12+
<table>
13+
<tr>
14+
<td><form:label path="name">Name</form:label></td>
15+
<td><form:input path="name" /></td>
16+
</tr>
17+
<tr>
18+
<td><form:label path="id">Id</form:label></td>
19+
<td><form:input path="id" /></td>
20+
</tr>
21+
<tr>
22+
<td><form:label path="contactNumber">Contact Number</form:label></td>
23+
<td><form:input path="contactNumber" /></td>
24+
</tr>
25+
<tr>
26+
<td><input type="submit" value="Submit" /></td>
27+
</tr>
28+
</table>
29+
</form:form>
30+
31+
</body>
32+
33+
</html>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
2+
<html>
3+
<head>
4+
<title>Spring MVC Form Handling</title>
5+
</head>
6+
<body>
7+
8+
<h2>Submitted Employee Information</h2>
9+
<table>
10+
<tr>
11+
<td>Name :</td>
12+
<td>${name}</td>
13+
</tr>
14+
<tr>
15+
<td>ID :</td>
16+
<td>${id}</td>
17+
</tr>
18+
<tr>
19+
<td>Contact Number :</td>
20+
<td>${contactNumber}</td>
21+
</tr>
22+
</table>
23+
</body>
24+
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7+
<title>SpringMVCExample</title>
8+
</head>
9+
10+
<body>
11+
<h3>Pleas enter the correct details</h3>
12+
<table>
13+
<tr>
14+
<td><a href="employee">Retry</a></td>
15+
</tr>
16+
</table>
17+
18+
</body>
19+
20+
</html>

0 commit comments

Comments
 (0)