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 cz.swi2.mendeluis.app.web.controllers; import cz.swi2.mendeluis.dto.UserRegistrationDTO; import cz.swi2.mendeluis.facade.IUserFacade; import cz.swi2.mendeluis.service.exception.UniqueViolationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.validation.ObjectError; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import org.springframework.web.util.UriComponentsBuilder; import javax.validation.Valid; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.validation.annotation.Validated; /** * @author ivo */ @Controller @RequestMapping("/registration") public class RegistrationController { final static Logger log = LoggerFactory.getLogger(RegistrationController.class); @ModelAttribute("user") public UserRegistrationDTO getUser() { return new UserRegistrationDTO(); } @Autowired private IUserFacade userFacade; /** * Initializes input fields validators. */ @InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); sdf.setLenient(false); binder.registerCustomEditor(Date.class, "dateOfBirth", new CustomDateEditor(sdf, true)); } /** * Shows a registration form. */ @RequestMapping(method = RequestMethod.GET) public String create(Model model) { log.info("registration visited"); model.addAttribute("createUser", new UserRegistrationDTO()); return "user/registration"; } /** * Handles registration form submission. */ @RequestMapping(method = RequestMethod.POST) public String create(@ModelAttribute("user") @Validated UserRegistrationDTO user, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) { log.info("submitted registration: {}", user); if (bindingResult.hasErrors()) { for (ObjectError ge : bindingResult.getGlobalErrors()) { log.info("ObjectError: {}", ge); } for (FieldError fe : bindingResult.getFieldErrors()) { model.addAttribute(fe.getField() + "_error", true); log.info("FieldError: {}", fe); } model.addAttribute("alert_failure", "There are some errors with submitted data."); return "user/registration"; } else { try { userFacade.createNewUser(user.getName(), user.getUsername(), user.getPassword()); redirectAttributes.addFlashAttribute("alert_success", "You sign up. You can log in now!"); return "redirect:/login"; } catch (UniqueViolationException e) { model.addAttribute("alert_failure", "The username already exists!"); return "user/registration"; } } } }