Skip to content

Commit 3aa3ac2

Browse files
author
Eugen
committed
Merge pull request eugenp#23 from Dheeraj-Baluja/master
added a new project for Using Spring Forms
2 parents 97dca03 + 463f14a commit 3aa3ac2

File tree

9 files changed

+213
-0
lines changed

9 files changed

+213
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Class-Path:
3+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xmlns:mvc="http://www.springframework.org/schema/mvc"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context-3.0.xsd
10+
http://www.springframework.org/schema/mvc
11+
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
12+
13+
<!-- Enable annotation driven controllers, validation etc... -->
14+
<mvc:annotation-driven />
15+
<context:component-scan base-package="com.demo.controllers" />
16+
17+
<bean id="viewResolver"
18+
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
19+
<property name="prefix">
20+
<value>/WEB-INF/views/</value>
21+
</property>
22+
<property name="suffix">
23+
<value>.jsp</value>
24+
</property>
25+
</bean>
26+
27+
</beans>
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
4+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5+
6+
<html>
7+
<head>
8+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
9+
<title>SpringMVCExample</title>
10+
</head>
11+
12+
<body>
13+
<h3>Welcome, Enter The Employee Details</h3>
14+
15+
<form:form method="POST" action="/SpringMVCFormExample/addEmployee"
16+
commandName="employee">
17+
<table>
18+
<tr>
19+
<td><form:label path="name">Name</form:label></td>
20+
<td><form:input path="name" /></td>
21+
</tr>
22+
<tr>
23+
<td><form:label path="id">Id</form:label></td>
24+
<td><form:input path="id" /></td>
25+
</tr>
26+
<tr>
27+
<td><form:label path="contactNumber">Contact Number</form:label></td>
28+
<td><form:input path="contactNumber" /></td>
29+
</tr>
30+
<tr>
31+
<td><input type="submit" value="Submit" /></td>
32+
</tr>
33+
</table>
34+
</form:form>
35+
36+
</body>
37+
38+
</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>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5">
3+
<javaee:display-name>SpringMVCFormExample</javaee:display-name>
4+
<servlet>
5+
<servlet-name>dispatcher</servlet-name>
6+
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
7+
<load-on-startup>1</load-on-startup>
8+
</servlet>
9+
<servlet-mapping>
10+
<servlet-name>dispatcher</servlet-name>
11+
<url-pattern>/</url-pattern>
12+
</servlet-mapping>
13+
<welcome-file-list>
14+
<welcome-file>index.jsp</welcome-file>
15+
</welcome-file-list>
16+
</web-app>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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"
4+
"http://www.w3.org/TR/html4/loose.dtd">
5+
<html>
6+
<head>
7+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8+
<title>Spring MVC Examples</title>
9+
</head>
10+
11+
<body>
12+
<h1>Spring MVC Examples</h1>
13+
<ul>
14+
<li><a href="employee">Welcome Page</a></li>
15+
</ul>
16+
</body>
17+
18+
</html>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.demo.controllers;
2+
3+
import javax.validation.Valid;
4+
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.ModelMap;
7+
import org.springframework.validation.BindingResult;
8+
import org.springframework.web.bind.annotation.ModelAttribute;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestMethod;
11+
import org.springframework.web.servlet.ModelAndView;
12+
13+
import com.demo.form.Employee;
14+
15+
@Controller
16+
public class EmployeeController {
17+
18+
@RequestMapping(value = "/employee", method = RequestMethod.GET)
19+
public ModelAndView showForm() {
20+
return new ModelAndView("employeeHome", "employee", new Employee());
21+
}
22+
23+
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
24+
public String submit(@Valid @ModelAttribute("employee")Employee employee, BindingResult result,
25+
ModelMap model) {
26+
if (result.hasErrors()) {
27+
return "error";
28+
}
29+
model.addAttribute("name", employee.getName());
30+
model.addAttribute("contactNumber", employee.getContactNumber());
31+
model.addAttribute("id", employee.getId());
32+
return "employeeAdded";
33+
}
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.demo.form;
2+
3+
public class Employee {
4+
5+
private String name;
6+
private long id;
7+
private String contactNumber;
8+
9+
public String getName() {
10+
return name;
11+
}
12+
13+
public void setName(String name) {
14+
this.name = name;
15+
}
16+
17+
public long getId() {
18+
return id;
19+
}
20+
21+
public void setId(long id) {
22+
this.id = id;
23+
}
24+
25+
public String getContactNumber() {
26+
return contactNumber;
27+
}
28+
29+
public void setContactNumber(String contactNumber) {
30+
this.contactNumber = contactNumber;
31+
}
32+
33+
}

0 commit comments

Comments
 (0)