spring-mvcportletjsr286

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?


Solution

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