如何使用 spring i18n 表单错误
How to i18n form errors with spring
所以我无法将这些默认消息更改为我想要的消息。在所有 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} 是错误的。
希望这对以后的任何人都有用。
所以我无法将这些默认消息更改为我想要的消息。在所有 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} 是错误的。
希望这对以后的任何人都有用。