我正在制作一个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'。那么你有任何其他解决方案? –