开发者

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 vo开发者_如何学编程id 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?


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜