0

我有一个使用 spring mvc 的 portlet,当门户是西班牙语时,在控制器中我尝试使用 messageSource.getMessage 它返回拉丁字符作为奇怪的字符。

应用程序上下文中的 MessageSource def:

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

控制器中的定义:

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

当我尝试以下操作时,它会返回奇怪的字符:

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

似乎它忽略了 UTF-8 编码。有任何想法吗?

4

3 回答 3

2

阅读此内容后找到了我的问题的解决方案 --> http://forum.springsource.org/showthread.php?64002-UTF-8-encoding-in-Spring-MVC-Portlet并进行了进一步的故障排除。

这是在提供资源和使用 ajax 时发生的。我通过在 ResourceResponse 上将字符编码设置为 UTF-8 解决了这个问题,似乎默认值是 ISO-8859-1。

您可以使用以下任一方法:

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

或者

response.setContentType("text/html;charset=UTF-8");
于 2011-07-05T14:50:34.983 回答
1

检查您的messages_es.properties文件的编码。它也必须是 UTF-8。

于 2011-06-30T13:39:24.710 回答
0

对不起我的英语水平不好。我有过类似的问题。当您尝试从消息源读取文件时,重要的是 JVM 环境也具有 UTF8 编码。换句话说,定义 VM 选项:-Dfile.encoding=UTF8

这可以解决任何尝试从文件系统的文件中读取的编码问题。也许它会帮助某人:)

于 2015-06-13T10:56:57.003 回答