【问题标题】:problem displaying unicode using Spring MVC使用 Spring MVC 显示 unicode 的问题
【发布时间】:2009-05-27 18:37:19
【问题描述】:

我正在尝试使用 freemarker 在我的 spring MVC webapp 中显示一些尼泊尔语言字符,

我做了所有事情,就像我的 web.xml 中有字符编码过滤器一样

<filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
</filter>

我的freemarker配置是这样的

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="cache" value="true"/>
        <property name="prefix" value=""/>
        <property name="contentType" value="text/html; charset=UTF-8"/>
        <property name="suffix" value=".ftl"/>
        <property name="exposeSpringMacroHelpers" value="true"/>
</bean> 

在模板中我有

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

定义我只是在我的页面中看到一些奇怪的字符。认为响应头有 正确的内容类型“text/html; charset=UTF-8”

不知道问题出在什么地方。我什至尝试从 控制器 response.setContentType('text/html; charset=UTF-8");

帮助大家

【问题讨论】:

  • 您的内容来自哪里?您的数据源的格式或类型是否支持扩展字符集?
  • 实际上我正在尝试使用 标签从 messages.properties 文件中读取
  • 您确定messages.properties 文件本身是UTF-8 编码的吗?很有可能(如果您使用的是 Windows),它实际上是 ISO 8859-1。您能否举例说明您希望看到的角色以及您实际看到的角色?
  • 我的属性文件是 UTF-8 编码的。

标签: model-view-controller spring unicode


【解决方案1】:

好的,我解决了这个问题,我使用了 ReloadableResourceBundleMessageSource 而不是 ResourceBundleMessageSource,其属性 defaultEncoding 为 UTF-8 所以 还必须为 basename 属性值添加类路径。

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

它现在可以工作了..

【讨论】:

    【解决方案2】:

    您确定内容正确吗?也许它不是有效的 UTF-8。

    “奇怪”的字符只是在开头吗?然后它们可以是 Windows 格式的 UTF-8 BOM(字节顺序标记)。

    【讨论】:

    • 我在 mac 但看起来这与我的 eclipse 文件和内容类型关联有关。
    猜你喜欢
    • 2019-02-15
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    相关资源
    最近更新 更多