简体   繁体   中英

Spring and XSLT, character encoding

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.

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

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

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

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

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

There's no content-type header!

Should I change my Tiles template?

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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