Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Configuration
- @ComponentScan("com.test.project")
- @EnableWebMvc
- public class WebConfig implements WebMvcConfigurer {
- ...
- @Bean
- public MessageSource messageSource() {
- ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
- messageSource.setBasenames("resources");
- messageSource.setUseCodeAsDefaultMessage(true);
- messageSource.setDefaultEncoding("UTF-8");
- messageSource.setFallbackToSystemLocale(false);
- return messageSource;
- }
- }
- user.loginIsEmpty = Login is required.
- user.login = Login must be between 8 and 16 characters.
- user.duplicateLogin = Such login already exists.
- user.passwordIsEmpty = Password is required.
- user.password = Password must be over 8 characters.
- user.confirmPasswordIsEmpty = Confirm password is required.
- user.differentPassword = Password don't match.
- @Component
- public class UserValidator implements Validator {
- @Autowired
- private UserService userService;
- @Override
- public boolean supports(Class<?> aClass) {
- return User.class.equals(aClass);
- }
- @Override
- public void validate(Object o, Errors errors) {
- User user = (User) o;
- ValidationUtils.rejectIfEmptyOrWhitespace(errors, "login", "user.loginIsEmpty");
- String login = user.getLogin();
- String password = user.getPassword();
- if (login.length() > 16 || login.length() < 8)
- errors.rejectValue("login", "user.login");
- ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "user.passwordIsEmpty");
- ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPassword", "user.confirmPasswordIsEmpty");
- if (password.length() < 8)
- errors.rejectValue("password", "user.password");
- if (!password.equals(user.getConfirmPassword()))
- errors.rejectValue("confirmPassword", "user.differentPassword");
- if (userService.getByLogin(login) != null)
- errors.rejectValue("login", "user.duplicateLogin");
- }
- }
- @Controller
- public class RegController {
- @Autowired
- UserValidator userValidator ;
- @Autowired
- private SecurityService securityService;
- @Autowired
- private UserService userService;
- @RequestMapping(value = "/reg", method = RequestMethod.GET)
- public String signUp(Model model) {
- model.addAttribute("user", new User());
- return "reg";
- }
- @RequestMapping(value = "/reg", method = RequestMethod.POST)
- public String signUp(@ModelAttribute("user") User user, BindingResult bindingResult, Model model) {
- userValidator.validate(user, bindingResult);
- if (!bindingResult.hasErrors()) {
- userService.create(passenger);
- return "/index";
- }
- else
- return "/reg";
- }
- }
- <form:form method="POST" modelAttribute="passenger">
- <spring:bind path="login">
- <div class="form-group ${status.error ? 'has-error' : ''}">
- <form:input type="text" path="login" class="form-control" placeholder="Login"></form:input>
- <form:errors path="login"></form:errors>
- </div>
- </spring:bind>
- <spring:bind path="password">
- <div class="form-group ${status.error ? 'has-error' : ''}">
- <form:input type="password" path="password" class="form-control" placeholder="Password"></form:input>
- <form:errors path="password"></form:errors>
- </div>
- </spring:bind>
- <spring:bind path="confirmPassword">
- <div class="form-group ${status.error ? 'has-error' : ''}">
- <form:input type="password" path="confirmPassword" class="form-control" placeholder="Confirm your password"></form:input>
- <form:errors path="confirmPassword"></form:errors>
- </div>
- </spring:bind>
- <button type="submit">Submit</button>
- </form:form>
- @Configuration
- @PropertySource(value = {"file:resources/message.properties"})
- @Configuration
- @PropertySource(value = {"file:/opt/common_resources/message.properties"})
- @Bean
- public void properties(
- @Value("${user.loginIsEmpty}") String loginIsEmpty,
- @Value("${user.login}") String login,
- @Value("${user.duplicateLogin}") String duplicateLogin,
- @Value("${user.passwordIsEmpty}") String password,
- @Value("${user.confirmPasswordIsEmpty}") String confirmPasswordIsEmpty,
- @Value("${user.differentPassword}") String differentPassword,
- ) {
- // Работа с данными
- }
Add Comment
Please, Sign In to add comment