如何使用弹簧 i18n 形成错误

问题描述

所以我无法将这些认消息更改为我想要的消息。在所有 JSP 文件中,它们都运行良好,但我不能让它们在表单中运行。

这是我的用户控制器:

 @RequestMapping(path = "/user/profile",method = RequestMethod.POST)
    public ModelAndView userProfilePost(@modelattribute("userForm") @Valid UserForm userForm,BindingResult result) {
        if(result.hasErrors()){
            return userProfileGet(userForm);
        }
        User user = us.findByEmail(SecurityContextHolder.getContext().getAuthentication().getName());
        user = userHelper.update(userForm,us,user);
        userForm = new UserForm();
        userForm.setName(user.getName());
        userForm.setEmail(user.getEmail());
       return userProfileGet(userForm);
    }

然后我们有了我的 WebConfig

    @Bean
    public LocalValidatorfactorybean localValidatorfactorybean() {
        LocalValidatorfactorybean bean = new LocalValidatorfactorybean();
        bean.setValidationMessageSource(messageSource());
        return bean;
    }

    @Bean
    public MessageSource messageSource()
    {
        final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames("classpath:languages_i18n/messages","classpath:languages_i18n/validation");
        messageSource.setdefaultencoding(StandardCharsets.UTF_8.displayName());
        messageSource.setCacheSeconds(5);
        return messageSource;
    }

我的用户表单:

public class UserForm {
    @Email()
    private String email;
    @Size(max=100,min=3)
    private String password;
    @Size(max=100,min =3)
    private String name;

    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }

    public String getpassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

这里我也尝试过(当然没用哈哈):

    @Email(message = "{Email.UserForm.email}")
    private String email;
    @Size(max=100,min=3,message = "{Size.UserForm.password}")
    private String password;
    @Size(max=100,min =3,message = "{Size.UserForm.name}")
    private String name;

这是我的validation_en.properties:

Email.UserForm.email = Your e-mail format is not valid
Size.UserForm.password = Your password must have between {min} and {max} characters
Size.UserForm.name = Your name must have between {min} and {max} characters

我知道如果我做对了,.properties 文件中的行应该是彩色的,但它们仍然是灰色的,所以很明显我没有正确到达它们。 任何评论都会受到欢迎,在此先感谢您。

解决方法

参数是一个对象数组。

{0} 是字段名称,其他字段按字母顺序排列,最大然后最小。

所以应该是:

Size.UserForm.password = Your password must have between {2} and {1} characters
Size.UserForm.name = Your name must have between {2} and {1} characters
,

显然解决方案是表单的第一个字母应该是小写的,就像这样(我不知道为什么):

Size.userForm.password = Your password must have between {2} and {1} characters
Size.userForm.name = Your name must have between {2} and {1} characters

之后我们还要补充一下Tung Phan所说的也是正确的,因为参数是对象数组,所以{min}和{max}是错误的。

希望这对未来的任何人都有用。