Guest User

Untitled

a guest
Nov 22nd, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. @Configuration
  2. @ComponentScan("com.test.project")
  3. @EnableWebMvc
  4. public class WebConfig implements WebMvcConfigurer {
  5.  
  6. ...
  7.  
  8. @Bean
  9. public MessageSource messageSource() {
  10. ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
  11. messageSource.setBasenames("resources");
  12. messageSource.setUseCodeAsDefaultMessage(true);
  13. messageSource.setDefaultEncoding("UTF-8");
  14. messageSource.setFallbackToSystemLocale(false);
  15. return messageSource;
  16. }
  17. }
  18.  
  19. user.loginIsEmpty = Login is required.
  20. user.login = Login must be between 8 and 16 characters.
  21. user.duplicateLogin = Such login already exists.
  22. user.passwordIsEmpty = Password is required.
  23. user.password = Password must be over 8 characters.
  24. user.confirmPasswordIsEmpty = Confirm password is required.
  25. user.differentPassword = Password don't match.
  26.  
  27. @Component
  28. public class UserValidator implements Validator {
  29.  
  30. @Autowired
  31. private UserService userService;
  32.  
  33. @Override
  34. public boolean supports(Class<?> aClass) {
  35. return User.class.equals(aClass);
  36. }
  37.  
  38. @Override
  39. public void validate(Object o, Errors errors) {
  40. User user = (User) o;
  41.  
  42. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "login", "user.loginIsEmpty");
  43. String login = user.getLogin();
  44. String password = user.getPassword();
  45. if (login.length() > 16 || login.length() < 8)
  46. errors.rejectValue("login", "user.login");
  47.  
  48. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "user.passwordIsEmpty");
  49. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPassword", "user.confirmPasswordIsEmpty");
  50.  
  51. if (password.length() < 8)
  52. errors.rejectValue("password", "user.password");
  53.  
  54. if (!password.equals(user.getConfirmPassword()))
  55. errors.rejectValue("confirmPassword", "user.differentPassword");
  56.  
  57. if (userService.getByLogin(login) != null)
  58. errors.rejectValue("login", "user.duplicateLogin");
  59. }
  60. }
  61.  
  62. @Controller
  63. public class RegController {
  64.  
  65. @Autowired
  66. UserValidator userValidator ;
  67.  
  68. @Autowired
  69. private SecurityService securityService;
  70.  
  71. @Autowired
  72. private UserService userService;
  73.  
  74.  
  75. @RequestMapping(value = "/reg", method = RequestMethod.GET)
  76. public String signUp(Model model) {
  77. model.addAttribute("user", new User());
  78. return "reg";
  79. }
  80.  
  81. @RequestMapping(value = "/reg", method = RequestMethod.POST)
  82. public String signUp(@ModelAttribute("user") User user, BindingResult bindingResult, Model model) {
  83. userValidator.validate(user, bindingResult);
  84. if (!bindingResult.hasErrors()) {
  85. userService.create(passenger);
  86. return "/index";
  87. }
  88. else
  89. return "/reg";
  90. }
  91. }
  92.  
  93. <form:form method="POST" modelAttribute="passenger">
  94. <spring:bind path="login">
  95. <div class="form-group ${status.error ? 'has-error' : ''}">
  96. <form:input type="text" path="login" class="form-control" placeholder="Login"></form:input>
  97. <form:errors path="login"></form:errors>
  98. </div>
  99. </spring:bind>
  100.  
  101. <spring:bind path="password">
  102. <div class="form-group ${status.error ? 'has-error' : ''}">
  103. <form:input type="password" path="password" class="form-control" placeholder="Password"></form:input>
  104. <form:errors path="password"></form:errors>
  105. </div>
  106. </spring:bind>
  107.  
  108. <spring:bind path="confirmPassword">
  109. <div class="form-group ${status.error ? 'has-error' : ''}">
  110. <form:input type="password" path="confirmPassword" class="form-control" placeholder="Confirm your password"></form:input>
  111. <form:errors path="confirmPassword"></form:errors>
  112. </div>
  113. </spring:bind>
  114.  
  115. <button type="submit">Submit</button>
  116. </form:form>
  117.  
  118. @Configuration
  119. @PropertySource(value = {"file:resources/message.properties"})
  120.  
  121. @Configuration
  122. @PropertySource(value = {"file:/opt/common_resources/message.properties"})
  123.  
  124. @Bean
  125. public void properties(
  126. @Value("${user.loginIsEmpty}") String loginIsEmpty,
  127. @Value("${user.login}") String login,
  128. @Value("${user.duplicateLogin}") String duplicateLogin,
  129. @Value("${user.passwordIsEmpty}") String password,
  130. @Value("${user.confirmPasswordIsEmpty}") String confirmPasswordIsEmpty,
  131. @Value("${user.differentPassword}") String differentPassword,
  132. ) {
  133. // Работа с данными
  134. }
Add Comment
Please, Sign In to add comment