如何为Spring消息本地化启用外来字符? [英] How to enable foreign characters for a Spring message localization?
问题描述
消息已正确加载,但外部字符未按预期呈现:
The messages are loaded alright but the foreign chars do not render as expected:
我的邮件属性(messages_sv.properties)是
My message properties (messages_sv.properties) are
login.title=Logga in
login.username=Användarnamn
login.password=Lösenord
JSP是
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ page import="com.ses.admin.controller.ObjectName" %>
<!DOCTYPE html>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="pu" tagdir="/WEB-INF/tags/node" %>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title><fmt:message key="login.title"/></title>
<fmt:message var="jqueryUrl" key="jquery.js.url"/>
<script src="<c:url value='${jqueryUrl}'/>" type="text/javascript"></script>
<fmt:message var="applicationJsUrl" key="application.js.url"/>
<script src="<c:url value='${applicationJsUrl}'/>" type="text/javascript"></script>
<link href="/css/style.css" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Raleway:400,200" rel="stylesheet" type="text/css">
</head>
<body>
<div id="header">
<div id="header-title"><img src="/images/logga.png">Account Administration</div>
</div>
<div class="login">
<hr />
<spring:url var="action" value="/admin/execute"/>
<form name='f' class="marg-left" id="inputForm" method="post" action="<c:url value='j_spring_security_check'/>" >
<h4 class="title"><spring:message code="login.title" /></h4>
<label>
<span><spring:message code="login.username" /></span>
<input type="text" name="j_username" />
</label>
<label>
<span><spring:message code="login.password" /></span>
<input type="password" name="j_password" />
</label>
<div class="buttons">
<button type="submit" ><spring:message code="login.title" /></button>
<button type="reset" ><spring:message code="login.reset" /></button>
</div>
</form>
</div>
</body>
</html>
我的XML配置是
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:annotation-driven/>
<context:annotation-config/>
<!--
<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
<property name="basenames">
<list>
<value>messages</value>
<value>errors</value>
<value>urls</value>
</list>
</property>
</bean>
-->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean id="jsonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
<bean id="methodHandlerExceptionResolver"
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
<property name="messageConverters">
<list>
<ref bean="stringHttpMessageConverter"/>
<ref bean="jsonHttpMessageConverter"/>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="assetFactory" class="com.ses.service.asset.PdfAssetFactoryImpl">
<constructor-arg ref="partyRepository"/>
<constructor-arg ref="customerAccountRepository"/>
<constructor-arg name="registrationTemplatePath" value="${SES_SERVICE_ASSET_FACTORY_REGISTRATION_TEMPLATE}"/>
</bean>
</beans>
在我的web.xml中,
And in my web.xml I got
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我知道我应该在所有地方都使用UTF-8,所以我想念什么?在呈现的HTML中,字符ä和ö无法正确呈现:
I know I should use UTF-8 everywhere so what did I miss? In the HTML that is rendered the chars ä and ö do not render correctly:
但是,使用此属性文件可获得正确的结果.真的有必要用这种方式对åäö进行编码吗?
However, using this properties file gives the right result. Is it really necessary to encode åäö this way?
login.title=Logga in
login.username=Anv\u00e4ndarnamn
login.password=L\u00f6senord
更新131218 20:40 CET
答案说,我应该设置编码方式,甚至创建了可以验证的新文件,如果我更改为本地文本(åäö),它将无法给出正确的结果.我可以获得正确结果的唯一方法是使用转义语法,这是不可取的.
Update 131218 20:40 CET
The answer says that I should set the encoding which I did and I even created new files that I can verify is in the encoding and if I then change to native text (åäö) it won't give the correct result. The oinly way I can get the correct result is with escape syntax which is not preferrable.
推荐答案
属性文件必须使用ISO-8859-1进行编码.您不能简单地以UTF-8编码属性文件并期望一切正常.您检查过编码了吗?
Properties files have to be encoded in ISO-8859-1. You can't simply encode a properties file in UTF-8 and expect things to work. Did you check your encoding?
属性文件还有另一种基于XML的格式,它比较冗长,但是允许您使用UTF-8,而无需使用丑陋的转义语法.
There is an alternative XML-based format for properties files which is more verbose, but allows you to use UTF-8 without ugly escape syntax.
这篇关于如何为Spring消息本地化启用外来字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!