Carlos
Carlos

Reputation: 1806

Spring MVC messageSource spanish characters

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?

Upvotes: 1

Views: 3260

Answers (3)

SerdukovAA
SerdukovAA

Reputation: 131

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 :)

Upvotes: 0

Carlos
Carlos

Reputation: 1806

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");

Upvotes: 2

phlogratos
phlogratos

Reputation: 13924

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

Upvotes: 2

Related Questions