简体   繁体   English

Spring和XSLT,字符编码

[英]Spring and XSLT, character encoding

I have problem with proper charset encoding part of HTML view. 我对HTML视图的正确字符集编码部分有疑问。 XSL file in JSP files generates .html. JSP文件中的XSL文件生成.html。 Values from database are encoded correct, but static headers of table contain wrong characters. 来自数据库的值编码正确,但是表的静态头包含错误的字符。

For example, there are headers named: Imię, Nazwisko, Hasło, Płeć, but it generates: ImiÄ™, Nazwisko, HasÅ‚o, PÅ‚eć 例如,存在标题为:Imię,Nazwisko,Hasło,Płeć的标头,但是它生成:ImiÄ™,Nazwisko,Hasão,PÅ‚eć

My forHomeHtml.xml template: 我的forHomeHtml.xml模板:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"
version="1.0">

<xsl:output method="xhtml" encoding="UTF-8" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />

<xsl:template match="/">
    <xsl:apply-templates />
</xsl:template>

<xsl:template match="/employees">
    <table>
        <tr>
            <th></th>
            <th>Imię</th>
            <th>Nazwisko</th>
            <th>Hasło</th>
            <th>Płeć</th>
        </tr>
        <xsl:for-each select="./employee">
            <tr>
                <td></td>
                <td>
                    <xsl:value-of select="name/text()" />
                </td>
                <td>
                    <xsl:value-of select="surname/text()" />
                </td>
                <td>
                    <xsl:value-of select="password/text()" />
                </td>
                <td>
                    <xsl:value-of select="gender/text()" />
                </td>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

JSP site: JSP网站:

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:import var="inputDocument"
url="http://localhost:8080/xyz/home.xml" />
<c:import var="stylesheet" url="/WEB-INF/xsl/forHomeHtml.xsl" />

<x:transform xml="${inputDocument}" xslt="${stylesheet}">
</x:transform>

I use Tiles, so the encoding is declared in main template: 我使用Tiles,因此在主模板中声明了编码:

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title><tiles:getAsString name="title" /></title>
...

I will add that I have encoding filter in web.xml 我要补充一点,我在web.xml中有编码过滤器

<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>

Character encoding of files (JSP, XSL, XML, etc.) is setted to UTF-8. 文件(JSP,XSL,XML等)的字符编码设置为UTF-8。 Character encoding of the browser is setted to UTF-8. 浏览器的字符编码设置为UTF-8。

Does anybody know the reason of that problem? 有人知道那个问题的原因吗?


Update : It is strange, but the source of site contains following code: 更新 :这很奇怪,但是网站的源代码包含以下代码:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-    transitional.dtd">
<html>
<head>
<title>Strona główna</title>
<style type="text/css">
.table-list {
border: 1px solid black;
border-collapse: collapse;
}
...
</style>
</head>
<body>
<table width="100%" border="0">
<tr style="background-color: #EEEEEE;">
<td><?xml version="1.0" encoding="UTF-8" ?>
<ul class="navigation_menu">
<li><a href="./home.htm">Strona główna</a></li>
<li><a href="./rejestracja.htm">Rejestracja</a></li>
<li><a href="./historia-wypozyczen-samochodu.htm">Historia samochodu</a></li>
<li><a href="./dodawanie-zamowienia.htm">Dodawanie zamówienia</a></li>
</ul>

<div style="text-align: center;">
Liczba obsłużonych dzisiaj zamówień:
0
</div></td></tr>
<tr>
<td valign="top" align="left">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE table PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-    transitional.dtd">
<table xmlns="http://www.w3.org/1999/xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<tr>
<th />
<th>ImiÄ&#153;</th>
<th>Nazwisko</th>
<th>HasÅ&#130;o</th>
<th>PÅ&#130;eÄ&#135;</th>
</tr>
<tr>
<td />
<td>Zenon</td>
<td>Kowalski</td>
<td>zHasło</td>
<td>Mężczyzna</td>
</tr>
<tr>
<td /></tr>
....
<tr style="background-color: #EEEEEE;">
<td><?xml version="1.0" encoding="UTF-8" ?>
<div style="text-align: center;"></div></td></tr>
</table>
</body>
</html>

There's no content-type header! 没有内容类型标题!

Should I change my Tiles template? 我应该更改我的Tiles模板吗?

Btw, @Alejandro, @Jim Garrison - thanks for tips. 顺便说一句,@ Alejandro,@ Jim Garrison-感谢您的提示。

It seems as if the JSTL is either not reading or outputting UTF-8 data correctly. 似乎JSTL没有正确读取或输出UTF-8数据。 I found several reports of issues with UTF-8 data and JSTL transforms, but not a lot of solutions. 我发现了一些有关UTF-8数据和JSTL转换问题的报告,但没有很多解决方案。

I did find this page describing a similar problem with JSTL and UTF-8 support . 我确实发现此页面描述了JSTL和UTF-8支持的类似问题 The solution was to switch transformers and use Saxon. 解决方案是切换变压器并使用Saxon。

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

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