Spring + SiteMesh 2.0汉字支持

时间:2012-06-04 09:28:14

标签: spring-mvc encoding sitemesh

我使用的是spring3.0和sitemesh2.0。问题是我的jsp页面无法显示中文字符。

我做了以下

的web.xml

<filter>     
        <filter-name>charsetFilter</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>charsetFilter</filter-name>     
        <url-pattern>*.do</url-pattern> 
    </filter-mapping> 
    <filter>

在我的jsp中:

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

在sitemesh模板中:

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

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="text/html;charset=UTF-8"
            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>

但是中文字符仍然显示如下: å®å¾½çä½³å®ç©A·(EA¢)æéå¬å¸

提前致谢。

2 个答案:

答案 0 :(得分:2)

你的过滤器和jsp看起来都很好。也许检查数据库设置。 还要记住,当您连接到数据库时,需要指定characterEncoding

jdbc:mysql://localhost:3306/yourdb?characterEncoding=UTF-8

答案 1 :(得分:1)

答复有点晚,但我希望其他人可能会从我浪费的时间中受益。 Spring的过滤器对我来说也不起作用。我自己编写并手动设置servletResponse的contentType。我现在没有问题。

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>