About the charset of pages.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

About the charset of pages.

smallfish
Hi,all guys:
I have met a problem about the charset of the page.
Here is a characterEncodingFilter in web.xml
        <filter>
                <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
                <url-pattern>/*</url-pattern>
        </filter-mapping>
and I set the Content-Type to "text/html; charset=UTF-8" in my template file.
Here is code:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

But,I noticed that the charset of the rendered page is always ISO-8859-1.
Here is the response header:

Content-Language:zh-CN
Content-Length:5096
Content-Type:text/html;charset=ISO-8859-1
Date:Thu, 02 Jun 2011 13:50:19 GMT
Server:Apache-Coyote/1.1

So ,I got some chars likes "??" finally in my Chrome brower.
Could any one tell me how to change the charset to UTF-8?
Reply | Threaded
Open this post in threaded view
|

Re: About the charset of pages.

danielfernandez
Administrator
Hello,

You've made me discover a bug in charset specification, thanks. But first, the workaround for what you need:

With the current 1.0.0-beta1 version, you will need to define a ThymeleafView bean with a "name" attribute equal to the template name and set its "contentType" property to "text/html; charset=UTF-8". For example, for a template named "main":

<bean name="main" class="org.thymeleaf.spring3.view.ThymeleafView">
  <property name="contentType" value="text/html; charset=UTF-8"/>
</bean>

The problem is that, if you have many templates, you will have to do this for every one of them. The ThymeleafViewResolver class has a "contentType" property which should keep you from doing this and should set that contentType to all its "views"/templates (without the need to declare them as beans), but that is precisely where the bug is, and this doesn't work :-(

I've fixed it in the SVN trunk, but for using it you will either have to wait for 1.0.0-beta2 (to be published next June 12th) or use the latest 1.0.0-beta2-SNAPSHOT version from the Sonatype snapshots repository in https://oss.sonatype.org/content/repositories/snapshots

With this fix, you will not need to declare beans for ThymeleafView, and instead only configure your ThymeleafViewResolver:

<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
  <property name="templateEngine" ref="templateEngine" />
  <property name="order" value="1" />
  <property name="viewNames" value="*.html,*.xhtml" />
  <property name="contentType" value="text/html; charset=UTF-8"/>
</bean>

About your code: the filter has no influence, it is only used for setting the charset of incoming form data. And the <meta> tag should be there for document validness and documentation purposes, but again will not be honored by the browser when determining the charset used by the page (the really important bit is the HTTP header, which you adequately examined).


Regards,
Daniel.

Reply | Threaded
Open this post in threaded view
|

Re: About the charset of pages.

smallfish
Hi,
It works well with the latest 1.0.0-beta2-SNAPSHOT version.Thx.
Reply | Threaded
Open this post in threaded view
|

Re: About the charset of pages.

Florian L
In reply to this post by danielfernandez
Wow ! I was searching for a solution and this extra line saved me from a white night. Cheers.