消息加载正常,但外来字符未按预期呈现:
我的消息属性(messages_sv.properties)是
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 中我得到了
<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 中,字符 ä 和 ö 无法正确呈现:
更新 131218 15:53 CET
但是,使用此属性文件会给出正确的结果。真的有必要以这种方式对 åäö 进行编码吗?
login.title=Logga in
login.username=Anv\u00e4ndarnamn
login.password=L\u00f6senord
更新 131218 20:40 CET
答案说我应该设置我所做的编码,我什至创建了可以验证编码的新文件,如果我然后更改为本机文本(åäö),它将不会给出正确的结果。我能得到正确结果的唯一方法是使用转义语法,这是不可取的。