Skip to content

Commit f94d6cc

Browse files
author
eugenp
committed
Merge branch 'master' of https://github.com/eugenp/tutorials
2 parents bda2923 + c5a22d1 commit f94d6cc

File tree

8 files changed

+181
-8
lines changed

8 files changed

+181
-8
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.baeldung.spring.controller;
2+
3+
import javax.validation.Valid;
4+
5+
import org.baeldung.spring.form.Employee;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.ModelMap;
8+
import org.springframework.validation.BindingResult;
9+
import org.springframework.web.bind.annotation.ModelAttribute;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.servlet.ModelAndView;
13+
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") final Employee employee, final BindingResult result, final ModelMap model) {
25+
if (result.hasErrors()) {
26+
return "error";
27+
}
28+
model.addAttribute("name", employee.getName());
29+
model.addAttribute("contactNumber", employee.getContactNumber());
30+
model.addAttribute("id", employee.getId());
31+
return "employeeAdded";
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.baeldung.spring.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+
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
3-
xsi:schemaLocation="
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4+
xmlns:mvc="http://www.springframework.org/schema/mvc"
5+
xsi:schemaLocation="
46
http://www.springframework.org/schema/beans
57
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context-3.2.xsd
610
http://www.springframework.org/schema/mvc
711
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
812

9-
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
10-
<property name="prefix" value="/WEB-INF/view/" />
11-
<property name="suffix" value=".jsp" />
12-
</bean>
13+
<mvc:annotation-driven />
14+
<context:component-scan base-package="org.baeldung.spring.controller" />
15+
<bean id="viewResolver"
16+
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17+
<property name="prefix" value="/WEB-INF/view/" />
18+
<property name="suffix" value=".jsp" />
19+
</bean>
1320

14-
<mvc:view-controller path="/sample.html" view-name="sample" />
21+
<mvc:view-controller path="/sample.html" view-name="sample" />
1522

1623
</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="/spring-mvc-xml/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>

spring-mvc-xml/src/main/webapp/WEB-INF/web.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<session-timeout>10</session-timeout>
4040
</session-config>
4141
<welcome-file-list>
42-
<welcome-file>index.html</welcome-file>
42+
<welcome-file>index.jsp</welcome-file>
4343
</welcome-file-list>
4444

4545
</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>

0 commit comments

Comments
 (0)