我正在开发一个spring mvc应用程序,它应该支持英语和阿拉伯。我已按照弹簧参考文档中的说明配置了应用程序,并且区域设置的切换工作正常。但是,资源包中的阿拉伯语消息显示为垃圾字符。编码设置为UTF-8,并且工作正常。我还尝试运行native2ascii工具将messages_ar.properties文件转换为unicode。
没有任何作用。任何帮助将不胜感激。
web.xml(部分)
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<web-app version="2.4"...>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>
<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>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Handles all requests into the application -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/mvc-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
mvc-config.xml(部分)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<context:component-scan base-package="net.omnsoq.classified.controller" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>
<!-- Configures support for @Controllers -->
<mvc:annotation-driven />
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource"
p:basenames="WEB-INF/i18n/messages,WEB-INF/i18n/application" p:fallbackToSystemLocale="false" />
<!-- store preferred language configuration in a cookie -->
<bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver" p:cookieName="locale" />
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang" />
</mvc:interceptors>
jsp代码
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
...
<%@page contentType="text/html;charset=UTF-8" %>
...
<spring:message code="nav.label.myaccount" />
答案 0 :(得分:5)
我找到了解决方案。所以我只想分享它,以便它可能对其他人有所帮助。
我为messageSource设置了fileEncodings和defaultEncoding属性为UTF-8。
<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource"
p:basenames="WEB-INF/i18n/messages,WEB-INF/i18n/application" p:fallbackToSystemLocale="false" p:fileEncodings="UTF-8"
p:defaultEncoding="UTF-8" />
答案 1 :(得分:0)
您是否检查过资源文件的内容?它不应包含任何UTF-8字符,只能包含ASCII。
用于此用途:
&lt; native2ascii encoding =“UTF-8”src =“$ {src.file}” dest =“$ {conf.deploy.dir}”包含=“** / 消息 .properties”/&gt;