0

I have a portlet that uses spring mvc and when the portal is in spanish and in the controller I try to use the messageSource.getMessage it returns latin characters as weird chars.

MessageSource def in application context:

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

Def in the controller:

@Autowired
    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

When I try the following it returns the weird chars:

messageSource.getMessage("messagekey", null, request.getLocale());

It seems like it's ignoring the UTF-8 encoding. Any ideas?

Carlos
  • 1,696
  • 1
  • 14
  • 21

3 Answers3

2

Found the solution to my problem after reading this --> http://forum.springsource.org/showthread.php?64002-UTF-8-encoding-in-Spring-MVC-Portlet and doing further troubleshooting.

This was happening while serving a resource and using ajax. I solved the issue by setting the character encoding to UTF-8 on the ResourceResponse, it seems the default is ISO-8859-1.

You can use either of the following:

response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");

or

response.setContentType("text/html;charset=UTF-8");
Carlos
  • 1,696
  • 1
  • 14
  • 21
  • I'm using Spring security. If a user is logged-in (but not fully authenticated because their login wasn't recent) and tries to visit an Account page that I've set to require full auth, my `access-denied-page="/login?reauthorize=true"` setting bounces them to the Login controller. But the login view was showing ??? instead of Chinese characters. UTF-8 works throughout my site in every case except for this one funky 403 circumstance. I don't know why it was necessary for me to specify `response.setCharacterEncoding("UTF-8");` in my LoginController (and not other controllers), but that solved it. – Ryan Oct 29 '13 at 21:00
1

Check the encoding of your messages_es.properties file. It has to be UTF-8 as well.

phlogratos
  • 13,234
  • 1
  • 32
  • 37
  • Yes the encoding of the property files is UTF-8. – Carlos Jun 30 '11 at 13:41
  • Properties files in Java should be always in ISO 8859-1 character encoding. "Characters that cannot be directly represented in this encoding can be written using Unicode escapes [skip] The native2ascii tool can be used to convert property files to and from other character encodings." See http://download.oracle.com/javase/6/docs/api/java/util/Properties.html – Slava Semushin Jul 06 '11 at 07:39
0

Sory my bad level English. I've had similar problems. When you try read file from message source it's important that JVM environment also had UTF8 encoding. In other words, define VM options: -Dfile.encoding=UTF8

This may solve the problem of coding for any attempt to read from the files of the file system. Maybe it will help someone :)

SerdukovAA
  • 131
  • 2
  • 2