消息加载正常但外部字符不按预期呈现:
我的消息属性(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中,字符ä和ö无法正确呈现:
但是,使用此属性文件会得到正确的结果。是否真的有必要以这种方式对åäö进行编码?
login.title=Logga in
login.username=Anv\u00e4ndarnamn
login.password=L\u00f6senord
答案说我应该设置我所做的编码,我甚至创建了我可以验证的新文件是否在编码中,如果我然后更改为原生文本(åäö),它将无法给出正确的结果。我可以获得正确结果的方法是使用转义语法,这是不可取的。
答案 0 :(得分:2)
属性文件必须以ISO-8859-1编码。您不能简单地使用UTF-8对属性文件进行编码,并希望工作正常。你检查了你的编码吗?
对于属性文件有一种替代的基于XML的格式,这种格式更详细,但允许您使用UTF-8而不会出现丑陋的转义语法。