ReloadableResourceBundleMessageSource:在代码'Size.userForm.username'下找不到任何消息

时间:2018-03-03 09:31:59

标签: spring freemarker

请帮帮我。我在我的Web应用程序中使用Spring MVC和Freemarker 2.3.27,我无法在我的FTL页面上捕获bindingResult.errors。当我尝试使用空用户名创建错误的用户时,我遇到错误:在区域设置'ru'的代码'Required'下找不到任何消息。但是当我创建具有短名称(<6个字符)的用户时,该消息将显示在我的注册表单中。为什么会这样?这是我的代码:

@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,"username","Required");
        if(user.getUsername().length()<6||user.getUsername().length()>32){
            errors.rejectValue("username","Size.userForm.username");
        }
        if(userService.findByUsername(user.getUsername())!=null){
            errors.rejectValue("username","Duplicate.userForm.username");
        }
        ValidationUtils.rejectIfEmptyOrWhitespace(errors,"password","Required");
        if(user.getPassword().length()<6||user.getPassword().length()>32){
            errors.rejectValue("password","Size.userForm.password");
        }
        if(!user.getConfirmPassword().equals(user.getPassword())){
            errors.rejectValue("confirmPassword","Different.userForm.password");
        }

    }
}
@Controller
@RequestMapping("/")
public class UserController {
   ....

    @GetMapping("/registration")
    public String goToRegistrationForm(Model model){
        model.addAttribute("userForm", new User());
        return "registration";
    }
    @PostMapping("/registration")
    public String registration(@ModelAttribute("userForm")User userForm, BindingResult bindingResult, Model model){
        userValidator.validate(userForm, bindingResult);
        if(bindingResult.hasErrors()){
            return "registration";
        }
        userService.saveUser(userForm);
        securityService.autologIn(userForm.getUsername(),userForm.getPassword());
        return "redirect:/hello";
    }
}

我的WebConfig:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "aquaplant")
public class WebConfig extends WebMvcConfigurerAdapter {
     .....
    @Bean(name = "messageSource")
    public ReloadableResourceBundleMessageSource getMessageSource() {
        ReloadableResourceBundleMessageSource messageSource = new 

ReloadableResourceBundleMessageSource();
            messageSource.setBasenames("validation");
            messageSource.setDefaultEncoding("UTF-8");
            //messageSource.setUseCodeAsDefaultMessage(true);
            return messageSource;
        }
   }    

我的项目结构:

 {main
       resources
                Resource Bundle 'validation'
                           validation_en.properties
                           validation_ru.properties}

1 个答案:

答案 0 :(得分:0)

问题出在我的validation_ru.properties中。我在记事本中以UTF格式保存了这个文件,但Notepad在文件的开头添加了一个不可见的Unicode字符BOM .......