Spring 和 XSLT,字符编码 [英] Spring and XSLT, character encoding

查看:26
本文介绍了Spring 和 XSLT,字符编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 HTML 视图的正确字符集编码部分有疑问.JSP 文件中的 XSL 文件生成 .html.来自数据库的值编码正确,但表的静态标题包含错误的字符.

I have problem with proper charset encoding part of HTML view. XSL file in JSP files generates .html. Values from database are encoded correct, but static headers of table contain wrong characters.

例如,有标题命名为:Imię、Nazwisko、Hasło、Płeć、但它会产生:ImiÄ™、Nazwisko、HasÅ‚o、PÅ‚eć

For example, there are headers named: Imię, Nazwisko, Hasło, Płeć, but it generates: ImiÄ™, Nazwisko, HasÅ‚o, PÅ‚eć

我的 forHomeHtml.xml 模板:

My forHomeHtml.xml template:

<?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 站点:

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

我使用 Tiles,所以编码在主模板中声明:

I use Tiles, so the encoding is declared in main template:

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

我会补充说我在 web.xml 中有编码过滤器

I will add that I have encoding filter in 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>

文件(JSP、XSL、XML 等)的字符编码设置为 UTF-8.浏览器的字符编码设置为UTF-8.

Character encoding of files (JSP, XSL, XML, etc.) is setted to UTF-8. Character encoding of the browser is setted to 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>

没有内容类型标题!

我应该更改我的瓷砖模板吗?

Should I change my Tiles template?

顺便说一句,@Alejandro、@Jim Garrison - 感谢您的提示.

Btw, @Alejandro, @Jim Garrison - thanks for tips.

推荐答案

JSTL 似乎没有正确读取或输出 UTF-8 数据.我发现了一些关于 UTF-8 数据和 JSTL 转换问题的报告,但没有很多解决方案.

It seems as if the JSTL is either not reading or outputting UTF-8 data correctly. I found several reports of issues with UTF-8 data and JSTL transforms, but not a lot of solutions.

我确实找到了 此页面描述了 JSTL 和 UTF-8 支持的类似问题.解决方案是切换变压器并使用 Saxon.

I did find this page describing a similar problem with JSTL and UTF-8 support. The solution was to switch transformers and use Saxon.

这篇关于Spring 和 XSLT,字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆