【发布时间】:2016-10-20 04:44:39
【问题描述】:
我正在制作一个 Web 应用程序。在这个应用程序中,我使用maven、Spring framework、Hibernate framework、JSP、Apache tiles。
这个应用程序仍然是英文的。所以现在我的要求是转换应用程序是葡萄牙语。所以我为葡萄牙语创建了一个属性文件,并在需要时使用这个属性文件。
在我的应用程序中创建两个属性文件,
1 表示英语(messages.properties),2 表示葡萄牙语(messages_pt.properties)
在我的 jsp 页面中,当我从 messages.properties 文件中获取值时工作正常,但是当从 messages_pt.properties 文件中获取时无法正常工作,因为在 messages_pt.properties 文件中包含一些特殊字符
查看我的两个属性文件
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>
因此,当从messages_pt.properties 文件中获取gas 键的值时,当时得到G�s 值。
我在谷歌搜索解决方案并得到一些解决方案,但没有人能解决我的问题
我正在申请以下解决方案
1) 在 JSP 页面中,我在第一行和元标记中添加 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) 我在messageSourceBean 中添加了defaultEncoding 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。
<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”。那么您还有其他解决方案吗?
标签: spring hibernate maven jsp apache-tiles