开发者

sitemesh and UTF-8 encoding

开发者 https://www.devze.com 2023-01-31 15:10 出处:网络
I have an spring-mvc application that is using sitemesh. The problem that I have is that my pages need to be UTF-8 but sitemesh supports ISO-8859-1 charset. Is it possible to configure Sitemesh to wor

I have an spring-mvc application that is using sitemesh. The problem that I have is that my pages need to be UTF-8 but sitemesh supports ISO-8859-1 charset. Is it possible to configure Sitemesh to work with UTF-8 pages? I am using a simple example where I am trying to show the page correclty but instead I am getting invalid characters like etc

The files I am using are:

sitemesh.xml

<sitemesh>
    <property name="decorators-file" value="/WEB-INF/decorators.xml" />
    <excludes file="${decorators-file}" />

    <page-parsers>
        <parser content-type="text/html"
            class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
        <parser content-type="tex开发者_如何学Pythont/html;charset=ISO-8859-1"
            class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
    </page-parsers>

    <decorator-mappers>
        <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
            <param name="config" value="${decorators-file}" />
        </mapper>
    </decorator-mappers>
</sitemesh>

web.xml

...
<filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
...

decorators.xml

<decorators defaultdir="/decorators">
    <decorator name="main" page="main.jsp">
          <pattern>/*</pattern>
    </decorator>
</decorators>

main.jsp

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<html>
<body>
 some stuff here
...
<div class="main"><decorator:body /></div>
</body>
</html>

My sample page:

<html>
...
<body>
     mùpeeàçè
</body>
</html>

Anyone have any idea? Thanks


you can try that in web.xml (forceEncoding important, it works for me)

    <filter>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>


Have you tried adding a CharacterEncodingFilter to your web.xml? See: http://ibnaziz.wordpress.com/2008/06/10/spring-utf-8-conversion-using-characterencodingfilter/


The reply is kinda late but I hope someone else might benefit from what I wasted hours on. Spring's filter didn't work for me either. I wrote my own and set servletResponse's contentType manually. I have no problems right now.

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
        throws ServletException, IOException {
    resp.setContentType("text/html; charset=UTF-8");
    chain.doFilter(req, resp);
}


<filter>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>com.muratdozen.mvc.filters.EncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/ui/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/WEB-INF/views/*</url-pattern>
    <dispatcher>ERROR</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>
0

精彩评论

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

关注公众号