我正在使用Spring(4.0.5)和Hibernate Validator(5.1.2.Final)
我已经通过.properties文件处理了错误消息,它也适用于i18n 。
我有以下内容:
ValidationMessages_es_PE.properties 文件(西班牙文)
person.salary.digits=Dato invalido '${validatedValue}', máximo tamaño no decimal permitido es {integer}, máximo tamaño decimal permitido es {fraction}
关于基础设施
@Configuration
public class ValidatorConfiguration {
@Bean
public ResourceBundleMessageSource resourceBundleMessageSource(){
ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
resourceBundleMessageSource.setDefaultEncoding("UTF-8");
resourceBundleMessageSource.setBasenames("com.manuel.jordan.validation.messages.ValidationMessages");
return resourceBundleMessageSource;
}
@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean(){
LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
localValidatorFactoryBean.setValidationMessageSource(resourceBundleMessageSource());
MessageSourceResourceBundleLocator messageSourceResourceBundleLocator = new MessageSourceResourceBundleLocator(resourceBundleMessageSource());
ResourceBundleMessageInterpolator resourceBundleMessageInterpolator = new ResourceBundleMessageInterpolator(messageSourceResourceBundleLocator);
localValidatorFactoryBean.setMessageInterpolator(resourceBundleMessageInterpolator);
return localValidatorFactoryBean;
}
}
观察我有setDefaultEncoding("UTF-8")
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=ValidatorConfiguration.class)
public class InvalidPersonValidation03Test {
private static final Logger logger = LoggerFactory.getLogger(InvalidPersonValidation03Test.class);
@Autowired
private LocalValidatorFactoryBean validator;
public InvalidPersonValidation03Test(){
LocaleContextHolder.setLocale(new Locale("es","PE"));
}
@Test
public void testInvalidNullId(){
...
部分输出如下
- >>>testInvalidFractionSalary
- ConstraintViolationImpl{interpolatedMessage='Dato invalido '1578.100', m�ximo tama�o no decimal permitido es 4, m�ximo tama�o decimal permitido es 2', propertyPath=salary, rootBeanClass=class com.manuel.jordan.domain.PersonValidation03, messageTemplate='{person.salary.digits}'}
- ConstraintViolationImpl{interpolatedMessage='Dato invalido '1578.100', m�ximo tama�o no decimal permitido es 4, m�ximo tama�o decimal permitido es 2', propertyPath=salary, rootBeanClass=class com.manuel.jordan.domain.PersonValidation03, messageTemplate='{person.salary.digits}'}
字符ñ
和á
未显示预期效果
我的IDE已经
Preferences -> Workspace -> Text file encoding
UTF-8
我需要哪些额外缺少的配置?
答案 0 :(得分:1)
您不能在属性文件中使用这样的unicode字符。见http://docs.oracle.com/javase/8/docs/api/java/util/PropertyResourceBundle.html
在这种情况下,无法在ISO-8859-1中表示的字符 编码必须由Unicode Escapes表示,如部分中所定义 3.3 Java™语言规范......
á将是\ u00E1和ñ\ u00F1
答案 1 :(得分:0)
社区
我看过这篇文章 Problem with Java properties utf8 encoding in Eclipse
因此,请考虑使用这个简单而实用的工具 ResourceBundle Editor
适用于Eclipse和Spring Tool Suite
问题解决了