简体   繁体   English

Eclipse / Spring / Tomcat 6的奇怪字符编码问题

[英]Strange character encoding issue with Eclipse / Spring / Tomcat 6

I have been trying things all day but can't get a proper solution. 我一整天都在尝试,但无法找到合适的解决方案。 My problem is: I am developing a Spring MVC based app in my local Tomcat. 我的问题是:我正在我的本地Tomcat中开发一个基于Spring MVC的应用程序。 My MySQl database has UTF-8 encoding set, all content in there displays properly when using phpMyAdmin. 我的MySQl数据库具有UTF-8编码集,当使用phpMyAdmin时,其中的所有内容都正确显示。 Also the output in LOG files using log4j in catalina.out works fine. 使用catalina.out中的log4j LOG文件中的输出也可正常工作。

My JSP pages are configured by 我的JSP页面由配置

<!-- encoding -->
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page pageEncoding="UTF-8" %>

Also showing data on my JSP works fine. 还显示我的JSP数据工作正常。 I can also send data from my Controller without any DB intereference using special chars, eg 我也可以使用特殊字符从我的控制器发送数据,而不会有任何数据库干扰,例如

String str = "UTF-8 Test: Ä Ö Ü ß è é â";
logger.debug(str);
mav.addObject("utftest", str);

That displays correctly in log and on jsp page in browser . 在浏览器的日志和jsp页面上正确显示。

BUT: When having special chars directly in my JSP file, eg for text in headers, this does not work. 但是:当我的JSP文件中直接使用特殊字符时,例如标题中的文本,这不起作用。 FF and Google Chrome display strange chars but report the page to be UTF-8. FF和谷歌浏览器显示奇怪的字符,但报告页面为UTF-8。 When switching to Latin, the chars just get more and more strange. 切换到拉丁语时,字符变得越来越奇怪。

Same problem when showing text tokens from my messages.properties file, although Eclipse says when right-clicking that UTF-8 will be used. 从我的messages.properties文件中显示文本标记时会出现同样的问题,尽管Eclipse在右键单击时会说明将使用UTF-8。

I am a little at lost and don't know where to check now. 我有点迷路了,现在不知道在哪里检查。

Summary: 摘要:

  • DB storage is fine 数据库存储很好
  • DB output on JSP is fine JSP上的DB输出很好
  • Output on JSP directly form controller is fine JSP上直接输出控制器的输出很好
  • even reading in form forms is fine 甚至以表格形式阅读也没关系
  • .properties files and JSP text is not fine !!! .properties文件和JSP文本不好 !!!

Any ideas? 有任何想法吗? I really appreciate and tips. 我非常感谢和提示。

The quest 任务

I got exactly the same problem than yours with a very similar configuration (Tomcat, Spring, Spring Web Flow, JSF2). 我遇到了与你的问题完全相同的问题,配置非常相似(Tomcat,Spring,Spring Web Flow,JSF2)。

Little facts about my own investigations: 关于我自己调查的一些事实:

  • WAR under Tomcat Window: encoding problem, Tomcat Window下的WAR:编码问题,
  • same WAR under Tomcat Linux: no problem → suspect OS default encoding as Linux is in UTF-8, Tomcat Linux下的相同WAR:没问题→可疑操作系统默认编码,因为Linux是UTF-8,
  • same WAR under Tomcat run by Eclipse WTP on Windows: no problem → WTF?! 在Windows上由Eclipse WTP运行的Tomcat下的相同WAR:没问题→WTF ?!
  • passing properties files in UTF-8 with natural latin characters instead of unicode placeholders: solve the problem for externalized labels, 使用自然拉丁字符而不是unicode占位符传递UTF-8中的属性文件:解决外部化标签的问题,
  • same in Facelets (JSF2 pages): always get the problem, only thing working is <f:verbatim>&amp;eacute;</f:verbatim> . 在Facelets(JSF2页面)中也是如此:总是遇到问题,只有工作的是<f:verbatim>&amp;eacute;</f:verbatim>

Still getting the problem, after having checked all my code for classic prerequisites and recommandations found on forums: 在检查了我在论坛上找到的经典先决条件和建议的所有代码之后,仍然遇到问题:

  • <?xml version="1.0" encoding="UTF-8" ?> at top of XML files, <?xml version="1.0" encoding="UTF-8" ?>在XML文件的顶部,
  • <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> inside HTML header of same files, <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />在同一文件的HTML标题内,
  • encoding="UTF-8" in <f:view> . <f:view> encoding="UTF-8"

The configuration of Tomcat in the following ways did nothing: Tomcat的配置在以下方面没有做任何事情:

  • URIEncoding="UTF-8" on connector in server.xml (normal because it concerns URI encoding not page encoding) URIEncoding = server.xml中连接器上的“UTF-8”(正常,因为它涉及URI编码而非页面编码)
  • org.springframework.web.filter.CharacterEncodingFilter on and off, 打开或关闭org.springframework.web.filter.CharacterEncodingFilter
  • also that (I presumably miss the point here): 还有(我可能错过了这里的观点):

     <locale-encoding-mapping-list> <locale-encoding-mapping> <locale>fr</locale> <encoding>UTF-8</encoding> </locale-encoding-mapping> </locale-encoding-mapping-list> 

The key 钥匙

I found the solution comparing the Tomcat command line between WTP and classic command-line MS-DOS Tomcat launch. 我找到了比较WTP和经典命令行MS-DOS Tomcat启动之间的Tomcat命令行的解决方案。 The only difference is the parameter -Dfile.encoding=UTF-8 . 唯一的区别是参数-Dfile.encoding=UTF-8 It was the key for me to solve the problem. 这是解决问题的关键。

Set JAVA_OPTS=-Dfile.encoding="UTF-8" and it works fine. 设置JAVA_OPTS = -Dfile.encoding =“UTF-8”,它工作正常。

The (attempted) explanation (尝试)解释

The only explanation I found, Tomcat use JVM encoding which is by default the system encoding (UTF-8 on Linux, CP1252 on Windows). 我发现的唯一解释是,Tomcat使用JVM编码,默认情况下是系统编码(Linux上为UTF-8,Windows上为CP1252)。 Eclipse WTP force the JVM encoding according to its workspace encoding settings. Eclipse WTP根据其工作空间编码设置强制JVM编码。 Passing JVM in UTF-8 gives the solution. 以UTF-8传递JVM提供了解决方案。

I suspect it's not really the right one and that there is a configuration problem either on my stack or on resources filtering made either by maven-resources-plugin or maven-war-plugin , but I haven't found it yet. 我怀疑它不是真的是正确的,并且我的堆栈或由maven-resources-pluginmaven-war-plugin进行的资源过滤都存在配置问题,但我还没有找到它。

You need to configure Eclipse to save the files as UTF-8. 您需要配置Eclipse以将文件保存为UTF-8。

Go to Window > Preferences , enter filter text encoding in top, explore all sections to set everything to UTF-8. 转到窗口>首选项 ,在顶部输入过滤器文本encoding ,浏览所有部分以将所有内容设置为UTF-8。 Specifically for JSP files this is in Web > JSP Files > Encoding . 特别是对于JSP文件,这是在Web> JSP Files> Encoding中 Choose the topmost UTF-8 option (called "ISO 10646/Unicode(UTF-8)"). 选择最顶层的UTF-8选项(称为“ISO 10646 / Unicode(UTF-8)”)。

For properties files this is a story apart. 对于属性文件,这是一个独立的故事。 As per the specification, they will by default be read as ISO-8859-1. 根据规范,它们默认为ISO-8859-1。 You need either native2ascii tool for this or supply a custom properfies file loader which uses UTF-8. 您需要native2ascii工具或提供使用UTF-8的自定义本地文件加载器。 For more detail, see this article . 有关更多详细信息,请参阅此文章

As BalusC said, you must save the files in format utf-8. 正如BalusC所说,你必须以utf-8格式保存文件。

To address your additional problem of included files, simply include the header 要解决包含文件的其他问题,只需包含标题即可

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

at the top of each included file. 在每个包含文件的顶部。 This tells the servlet to treat the file as UTF-8 encoded, instead of using the default ISO-8859-1. 这告诉servlet将文件视为UTF-8编码,而不是使用默认的ISO-8859-1。

I'm using Tomcat 7 with Spring frameworks and using <jsp:include page="anyFile.html"/> in JSP fail and give me a java.lang.IllegalStateException . 我在Spring框架中使用Tomcat 7并在JSP中使用<jsp:include page="anyFile.html"/>失败并给我一个java.lang.IllegalStateException The <jsp:include> works fine if i want to include another JSP file instead of a static HTML file though but when I'm trying to inject static HTML file it keep giving me this exception in relation with the Character Encoding. 如果我想包含另一个JSP文件而不是静态HTML文件, <jsp:include>可以正常工作但是当我尝试注入静态HTML文件时,它会继续给我这个与字符编码有关的异常。

Using <jsp:directive.include file="anyFile.html" /> or <%@include file="anyFile.html"%> works but all the special character ("é", "è", "ç" etc.) appear coded into ISO-8891 instead of UTF-8 even if the JSP file have the <%@page contentType="text/html" pageEncoding="UTF-8"%> and the <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> in it. 使用<jsp:directive.include file="anyFile.html" /><%@include file="anyFile.html"%>可以使用所有特殊字符(“é”,“è”,“ç”等。 )即使JSP文件具有<%@page contentType="text/html" pageEncoding="UTF-8"%><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />也会显示编码为ISO-8891而不是UTF-8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

I found the solution by using the JSLT tag library with the import tag: 我通过使用带有import标记的JSLT标记库找到了解决方案:

  1. put this into the JSP: <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 把它放到JSP中: <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

  2. Then get the HTML file I want to include using this: <c:import url="anyFile.html" charEncoding="UTF-8"/> 然后使用以下内容获取我想要包含的HTML文件: <c:import url="anyFile.html" charEncoding="UTF-8"/>

Has you can see the import tag from the JSLT library have a charEncoding attribute that can set the html file to the appropriate Character encoding and display it's content correctly. 您是否可以看到来自JSLT库的import标记具有charEncoding属性,该属性可以将html文件设置为适当的Character编码并正确显示其内容。

For JSP, see @BalusC. 对于JSP,请参阅@BalusC。

For properties files see: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Properties.html 有关属性文件,请参阅: http//download.oracle.com/javase/1.4.2/docs/api/java/util/Properties.html

When saving properties to a stream or loading them from a stream, the ISO 8859-1 character encoding is used. 将属性保存到流或从流中加载它们时,将使用ISO 8859-1字符编码。 For characters that cannot be directly represented in this encoding, Unicode escapes are used; 对于无法在此编码中直接表示的字符,使用Unicode转义; however, only a single 'u' character is allowed in an escape sequence. 但是,在转义序列中只允许使用一个“u”字符。 The native2ascii tool can be used to convert property files to and from other character encodings. native2ascii工具可用于将属性文件转换为其他字符编码或从其他字符编码转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM