Created
February 1, 2016 01:25
-
-
Save vy-nguyen/250f7e64d43ffc3be27b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.howtodoinjava.demo.model; | |
import java.io.Serializable; | |
public class EmployeeVO implements Serializable | |
{ | |
private static final long serialVersionUID = 1L; | |
private Integer id; | |
private String firstName; | |
private String lastName; | |
private String email; | |
//Getters and Setters | |
@Override | |
public String toString() { | |
return "EmployeeVO [id=" + id + ", firstName=" + firstName | |
+ ", lastName=" + lastName + ", email=" + email + "]"; | |
} | |
} | |
<%@ page contentType="text/html;charset=UTF-8"%> | |
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> | |
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> | |
<html> | |
<head> | |
<title>Add Employee Form</title> | |
</head> | |
<body> | |
<h2><spring:message code="lbl.page" text="Add New Employee" /></h2> | |
<br/> | |
<form:form method="post" modelAttribute="employee"> | |
<table> | |
<tr> | |
<td><spring:message code="lbl.firstName" text="First Name" /></td> | |
<td><form:input path="firstName" /></td> | |
</tr> | |
<tr> | |
<td><spring:message code="lbl.lastName" text="Last Name" /></td> | |
<td><form:input path="lastName" /></td> | |
</tr> | |
<tr> | |
<td><spring:message code="lbl.email" text="Email Id" /></td> | |
<td><form:input path="email" /></td> | |
</tr> | |
<tr> | |
<td colspan="2"><input type="submit" value="Add Employee"/></td> | |
</tr> | |
</table> | |
</form:form> | |
</body> | |
</html> | |
@Controller | |
@RequestMapping("/employee-module/addNew") | |
@SessionAttributes("employee") | |
public class EmployeeController | |
{ | |
@Autowired | |
EmployeeManager manager; | |
@RequestMapping(method = RequestMethod.GET) | |
public String setupForm(Model model) | |
{ | |
EmployeeVO employeeVO = new EmployeeVO(); | |
model.addAttribute("employee", employeeVO); | |
return "addEmployee"; | |
} | |
@RequestMapping(method = RequestMethod.POST) | |
public String submitForm(@ModelAttribute("employee") EmployeeVO employeeVO, | |
BindingResult result, SessionStatus status) | |
{ | |
//Store the employee information in database | |
//manager.createNewRecord(employeeVO); | |
//Mark Session Complete | |
status.setComplete(); | |
return "redirect:addNew/success"; | |
} | |
@RequestMapping(value = "/success", method = RequestMethod.GET) | |
public String success(Model model) | |
{ | |
return "addSuccess"; | |
} | |
} | |
<%@ page contentType="text/html;charset=UTF-8"%> | |
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> | |
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> | |
<html> | |
<head> | |
<title>Add Employee Form</title> | |
<style> | |
.error | |
{ | |
color: #ff0000; | |
font-weight: bold; | |
} | |
</style> | |
</head> | |
<body> | |
<h2><spring:message code="lbl.page" text="Add New Employee" /></h2> | |
<br/> | |
<form:form method="post" modelAttribute="employee"> | |
<%-- <form:errors path="*" cssClass="error" /> --%> | |
<table> | |
<tr> | |
<td><spring:message code="lbl.firstName" text="First Name" /></td> | |
<td><form:input path="firstName" /></td> | |
<td><form:errors path="firstName" cssClass="error" /></td> | |
</tr> | |
<tr> | |
<td><spring:message code="lbl.lastName" text="Last Name" /></td> | |
<td><form:input path="lastName" /></td> | |
<td><form:errors path="lastName" cssClass="error" /></td> | |
</tr> | |
<tr> | |
<td><spring:message code="lbl.email" text="Email Id" /></td> | |
<td><form:input path="email" /></td> | |
<td><form:errors path="email" cssClass="error" /></td> | |
</tr> | |
<tr> | |
<td colspan="3"><input type="submit" value="Add Employee"/></td> | |
</tr> | |
</table> | |
</form:form> | |
</body> | |
</html> | |
@RequestMapping(method = RequestMethod.POST) | |
public String submitForm(@ModelAttribute("employee") EmployeeVO employeeVO, | |
BindingResult result, SessionStatus status) | |
{ | |
//Validation code start | |
boolean error = false; | |
System.out.println(employeeVO); //Verifying if information is same as input by user | |
if(employeeVO.getFirstName().isEmpty()){ | |
result.rejectValue("firstName", "error.firstName"); | |
error = true; | |
} | |
if(employeeVO.getLastName().isEmpty()){ | |
result.rejectValue("lastName", "error.lastName"); | |
error = true; | |
} | |
if(employeeVO.getEmail().isEmpty()){ | |
result.rejectValue("email", "error.email"); | |
error = true; | |
} | |
if(error) { | |
return "addEmployee"; | |
} | |
//validation code ends | |
//Store the employee information in database | |
//manager.createNewRecord(employeeVO); | |
//Mark Session Complete | |
status.setComplete(); | |
return "redirect:addNew/success"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment