我正在製作一個Web應用程序。在此應用程序中,我使用的是maven
,Spring framework
,Hibernate framework
,JSP
, Apache tiles
。JSP屬性文件讀取
此應用程序仍然是英文的。所以現在我的要求是轉換應用程序是用葡萄牙語。所以我爲葡萄牙語創建一個屬性文件,並在需要時使用這個屬性文件。
在我的應用程序創建兩個屬性文件,
1英語(messages.properties
),2葡萄牙語(messages_pt.properties
)
在我的jsp頁面時,我在那個時候從messages.properties
文件讀取值正常工作,但因爲在messages_pt.properties
文件中包含一些特殊的字符
看到,當從messages_pt.propertie
S檔在那個時候取不正常我的兩個屬性文件
messages.properties
gas=Gas
messages_pt.properties
gas=Gás
現在來找我的問題...
在JSP頁面中我使用fmt tag library
讀值的鍵從屬性文件 我的jsp頁面是這樣的
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
..............
</head>
<body>
..............
<fmt:message key="gas" />
</body>
</html>
所以當取值gas
鍵messages_pt.properties
當時的文件獲得G�s
值。
我搜索在谷歌的解決方案,並得到了一些解決方案,但
我申請以下解決方案
1)在JSP頁面中沒有一個人能解決我的問題,我在第一線和meta標籤中添加的contentType。請參閱上面的我的jsp頁面。
2)我在web.xml
文件中添加了CharacterEncodingFilter
文件,並且此過濾器位於起始頁的第一個過濾器中。
<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>
3)I在messageSourceBean
在xxx-servlet.xml
文件
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:properties/messages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8"/>
<property name="useCodeAsDefaultMessage" value="true"/>
</bean>
4)我pom.xml
文件添加project.build.sourceEncoding
加入defaultEncoding
。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
.......
</properties>
5)我想下面的代碼
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<p><spring:message code="label.menu"/></p>
但現在可以解決我的問題。
如果有人知道任何其他解決方案,那麼請告訴我。
屬性文件應該在ISO-8859-1中進行編碼。我的猜測是你的編碼是UTF8。 –
謝謝JB的答覆..我嘗試了'ISO-8859-1'編碼的屬性文件,但我得到'Gï¿s'而不是'Gás'。那麼你有任何其他解決方案? –