Java tutorial
/* * This file is part of EasyExchange. * * (c) 2014 - Machiel Molenaar <machiel@machiel.me> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ package com.mtech.easyexchange.mvc; import com.mtech.easyexchange.manager.exception.UserAlreadyExistsException; import com.mtech.easyexchange.model.User; import com.mtech.easyexchange.manager.user.IUserManager; import com.mtech.easyexchange.validator.RegistrationValidator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Controller; 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 javax.validation.Valid; @Controller public class RegisterController { @Autowired @Qualifier("userManager") protected IUserManager userManager; @Autowired protected RegistrationValidator registrationValidator; @RequestMapping(value = "/register", method = RequestMethod.GET) public String register() { return "user/register"; } @RequestMapping(value = "/register", method = RequestMethod.POST) public String processRegistration(@Valid @ModelAttribute("user") User user, BindingResult bindingResult) { registrationValidator.validate(user, bindingResult); BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(10); user.setPassword(encoder.encode(user.getPassword())); if (bindingResult.hasErrors()) { return "user/register"; } try { userManager.createUser(user); } catch (UserAlreadyExistsException e) { return "user/register"; } return "redirect:/register/thank-you"; } @RequestMapping(value = "/register/thank-you", method = RequestMethod.GET) public String registrationCompleted() { return "user/registration-complete"; } }