开发者

Tiles encoding problem

开发者 https://www.devze.com 2022-12-21 15:55 出处:网络
I\'m trying to use UTF-8 encoding for the Spring application I\'m developing but I have problems in getting the correct encoding when inserting attributes from tiles.

I'm trying to use UTF-8 encoding for the Spring application I'm developing but I have problems in getting the correct encoding when inserting attributes from tiles.

I have this fragment in my JSP template:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <title><tiles:getAsString name="title" /></title>   
</head>
<body>
    <tiles:insertAttribute name="header" ignore="true" />
....

and in my tiles XML config file I have something like:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
   "-//Apache Softwa开发者_运维技巧re Foundation//DTD Tiles Configuration 2.1//EN"
   "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
   <definition name="tiles:base" template="/WEB-INF/views/templates/main.jsp">
     <put-attribute name="title" value="Título" />
...

I have check in eclipse that this files have UTF-8 encoding. The word passed in title attribute is not shown correctly (the accented characters are shown in a wrong way) in the page though the rest of the JSP is correct (for instance the JSP fragment which is inserted in header). If I change the encoding to ISO-8859-1 the title is OK, but the rest odf the page is wrong. It seems I cannot get to change the encoding to UTF-8 in my tiles file. I have also looked for "ISO-8859-1" in the files I've created and I haven't set up this configuration in any file.

Can anyone tell me how can I set up the correct encoding for tiles?

Thanks


Add the following to web.xml. This has the same effect as adding the header in each JSP file.

web.xml:

<web-app>
    ...
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
            <trim-directive-whitespaces>true</trim-directive-whitespaces>
        </jsp-property-group>
    </jsp-config>    
</web-app>


It was a problem with the charset, not with the encoding. I had to set

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

in every JSP and it worked. I don't know if there is an easier way of configure this in all the JSPs of a Spring Web application.


Another way could be a usage of ReloadableResourceBundleMessageSource (with property defaultEncoding="UTF-8") also for the content being inserted from tiles.

I meant you can pass a keyword from tiles, and use it to output needed content from resource bundle, like this:

<tiles:useAttribute id="title_key" name="title"/>
<spring:message code="${title_key}"/>


During my Struts 2.3 to 2.5 migration i encountered a similar problem : content-type (in response header) of all javascript .JS files referenced by JSP were now "application/javascript;charset=ISO-8859-1" (struts 2.5) instead of charset=UTF-8 (in struts 2.3). Charset attribute was set to utf-8 for JSP and script markup referencing the js file.

I added the code from Leonel and it finally worked: but encoding is now "text/html;charset=UTF-8". So i've lost the application/javascript. it did'nt work properly.

<web-app>
...
<jsp-config>
    <jsp-property-group>
        <url-pattern>*.js</url-pattern>
        <page-encoding>UTF-8</page-encoding>
        <trim-directive-whitespaces>true</trim-directive-whitespaces>
    </jsp-property-group>
</jsp-config>    

So i tried something else : https://www.baeldung.com/tomcat-utf-8 With this i get the correct charset and content-type.

Let’s define a class named CharacterSetFilter:

public class CharacterSetFilter implements Filter {

// ...

public void doFilter(
  ServletRequest request, 
  ServletResponse response, 
  FilterChain next) throws IOException, ServletException {
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html; charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    next.doFilter(request, response);
}

// ...
}

We need to add the filter to our application’s web.xml so that it’s applied to all requests and responses:

<filter>
 <filter-name>CharacterSetFilter</filter-name>
 <filter-class>com.baeldung.CharacterSetFilter</filter-class>
</filter>

<filter-mapping>
 <filter-name>CharacterSetFilter</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号