Skip to content

Instantly share code, notes, and snippets.

@vy-nguyen
Created February 1, 2016 01:27
Show Gist options
  • Save vy-nguyen/386b668e0d6f0ef9cde8 to your computer and use it in GitHub Desktop.
Save vy-nguyen/386b668e0d6f0ef9cde8 to your computer and use it in GitHub Desktop.
package com.howtodoinjava.demo.validator;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.howtodoinjava.demo.model.EmployeeVO;
@Component
public class EmployeeValidator implements Validator
{
public boolean supports(Class clazz) {
return EmployeeVO.class.isAssignableFrom(clazz);
}
public void validate(Object target, Errors errors)
{
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "error.firstName", "First name is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "error.lastName", "Last name is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "error.email", "Email is required.");
}
}
@RequestMapping(method = RequestMethod.POST)
public String submitForm(@ModelAttribute("employee") EmployeeVO employeeVO,
BindingResult result, SessionStatus status)
{
//Validation code
validator.validate(employeeVO, result);
//Check validation errors
if (result.hasErrors()) {
return "addEmployee";
}
//Store the employee information in database
//manager.createNewRecord(employeeVO);
//Mark Session Complete
status.setComplete();
return "redirect:addNew/success";
}
package com.howtodoinjava.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import com.howtodoinjava.demo.model.EmployeeVO;
import com.howtodoinjava.demo.service.EmployeeManager;
import com.howtodoinjava.demo.validator.EmployeeValidator;
@Controller
@RequestMapping("/employee-module/addNew")
@SessionAttributes("employee")
public class EmployeeController
{
@Autowired
EmployeeManager manager;
@Autowired
EmployeeValidator validator;
@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)
{
validator.validate(employeeVO, result);
if (result.hasErrors()) {
return "addEmployee";
}
//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";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment