Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.onclave.testbench.simpleForm; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.Validator; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * * @author sajib */ @Controller public class SimpleFormController { @Autowired @Qualifier("simpleFormValidatorBean") private Validator validator = null; @InitBinder private void initBinder(WebDataBinder binder) { binder.setValidator(validator); } @RequestMapping(value = "/simpleForm", method = RequestMethod.GET) public String viewForm(Model model) { StudentPOJO student = new StudentPOJO(); model.addAttribute("studentForm", student); return "/form/showForm"; } @RequestMapping(value = "/submitForm", method = RequestMethod.POST) public String showFormContents(@ModelAttribute("studentForm") @Validated StudentPOJO student, BindingResult result, Model model) { String returnView = "/form/result"; if (!model.containsAttribute("studentForm")) { model.addAttribute("studentForm", student); } if (result.hasErrors()) { returnView = "/form/showForm"; } else { model.addAttribute("studentForm", student); } return returnView; } }