3

我有一个 Spring MVC J2EE 应用程序,它利用 SpringlocaleChangeInterceptorCookieLocaleResolver提供语言环境驱动的支持。这是可行的,但是当我尝试使用重音符号对字母进行编码时,前端无法按预期呈现它们。

这是我的 webmvc-config.xml 文件中的一个片段:

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

    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>

    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en"/>
    </bean>

    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <ref bean="localeChangeInterceptor" />
        </property>
    </bean>

除此之外,我还有一些 message_xx.properties 文件,这些文件包含我的标签来呈现内容。包括这样的标签,其中嵌入了重音符号:district.manager.approval=Aprobaci&#243;n del Gerente de Distrito。我的不满是它在前端显示完全一样,而不是显示给我Aprobación del Gerente de Distrito

知道我哪里可能出错了吗?

4

3 回答 3

2

.properties 文件的编码通常(除了少数例外)预期为 Latin-1。因此,为了呈现 Unicode 内容,您需要对 Latin-1 曲目之外的字符进行转义:

district.manager.approval=Aprobaci\u00F3n del Gerente de Distrito

或者使用可以编码为 UTF8 的 XML 属性文件。

于 2013-08-15T03:29:57.430 回答
1

经过一番探索,我似乎遗漏了一个关键细节:这似乎只发生在我使用 JSTL<c:set>和我的 Spring 标记时编码无法正常工作的情况。事实证明,在使用 时<c:out>,您需要将其与escapeXml="false"属性一起使用。这是我所做的,它现在似乎工作正常:

这是在一页中设置的

<c:set var="headerScreenTitle">
    <spring:message code='district.manager.review.and.approval' />
</c:set>   

这在导入的页面中使用

<c:out value="${headerScreenTitle}" escapeXml="false" />

它给了我这个:

REVISIÓN Y APROBACIÓN DEL GERENTE DE DISTRITO

感谢大家的回应!

于 2013-08-15T22:19:10.297 回答
0

如果您使用 html 实体(例如 &)并使用<spring:message code="property.name" />标签打印值,请将“htmlEscape”属性设置为“false”:

<spring:message code="property.name" htmlEscape="false" />
于 2013-08-15T12:37:36.890 回答