4

I have a Spring form wih some validations and all the personalized messeges from javax.validation.constraints appears to use the wrong encoding.

Lets take this as an example:

@NumberFormat(style=Style.NUMBER) 
@NotNull 
private BigDecimal maintenanceCosts;

With applicationContext.xml file containing

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="defaultEncoding" value="UTF-8" />
    <property name="basenames">
        <list>
            <value>classpath:messages</value>
            <value>classpath:ValidationMessages</value>
        </list>
    </property>
</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="en" />
</bean>

And a ValidationMessage_en.properties encoded in UTF-8 and marked so (in Eclipse with right click, properties) with the text:

javax.validation.constraints.NotNull.message=This field can't be empty

I want to show thouse messages in localized strings with the right codification so I added an UTF-8 file ValidationMessages_ru.properties with:

javax.validation.constraints.NotNull.message=Это поле не может быть пустым

But the message shows this message: Это поле не может быть пуÑтым

On the other hand I has able to customize the spring managed error messages with the right encoding. But JSR303 texts seems to behave differently.

borjab
  • 11,149
  • 6
  • 71
  • 98
  • Are you absolutely sure that the properties file is in UTF-8 and Eclipse is not autosilly encoding non-ASCII characters? Did you check the file in a normal text editor? – Jakub Jirutka Apr 07 '14 at 22:29
  • I used notepad++ to do it – borjab Apr 08 '14 at 07:50
  • Well, please try [this](http://stackoverflow.com/a/20912549/2217862) configuration. It may not be related with your issue, but just to be sure that it’s not a configuration problem. What version of Spring and Hibernate Validator are you using? – Jakub Jirutka Apr 08 '14 at 09:20
  • Spring version is 3.2.3.RELEASE Hibernate validator is 4.2.0.Final – borjab Apr 08 '14 at 09:33
  • Oki, did you try the linked configuration? If it did not work, then try to upgrade Hibernate Validator to 4.3.1.Final. – Jakub Jirutka Apr 08 '14 at 11:12
  • I have tried it and after making the change I only see the default mesage. I will try the upgrade – borjab Apr 08 '14 at 14:22
  • by the way see: http://stackoverflow.com/questions/6421790/hibernate-validator-jsf-2-0-validationmessages-properties-in-utf-8 May be the problem is that I am not familiar at all with Java Server Faces. – borjab Apr 08 '14 at 14:35

2 Answers2

5

I found that it was much more elegant to get rid of the ValidationMessages_XX.properties files. Just all the localized configuration in one file that supports UTF-8!

The answer is to set the ValidationMessageSource of your validator with your resourceBundle. For example:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" 
        p:defaultEncoding="UTF-8"
        p:basenames      ="classpath:messages"/>

<!-- JSR-303 validation-->
<bean id ="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"
        p:validationMessageSource-ref="messageSource"/>

All merit must be attributed to this blog post. Not being able to read thouse characters was annoying. No more: message=\u042D\u0442\u043E \u043F\u043E\u043B\u0435 \u043D\u0435 \u043C

borjab
  • 11,149
  • 6
  • 71
  • 98
0

Properties file in Java are per default ISO 8859-1. Characters which you cannot display this way need to use unicode escapes. To quote the javadocs of Properties:

...except the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes ; only a single 'u' character is allowed in an escape sequence.

You will need to use unicode escapes. You can di it manually or use native2ascii.

Hardy
  • 18,659
  • 3
  • 49
  • 65
  • This is true, beside that he’s not using legacy Java [ResourceBundle](http://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html), but Spring’s [MessageSource](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/MessageSource.html) that does not have this silly limitation. – Jakub Jirutka Apr 08 '14 at 11:09
  • Since Java 5 there seems to be some function to read properties in UTF-8. http://bordet.blogspot.com.es/2007/01/utf-8-handling-for-resourcebundle-and.html Anyway this silly limitation is really annoying. P.S. I find the -1 a bit hard. I really appreciate his insight even if it does not really solve the problem it helps to explain why it happend. – borjab Apr 08 '14 at 14:05