0

我使用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"%>

在站点网格模板中:

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

站点网格.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>

但是汉字仍然是这样显示的:å®å¾½çä½³å®ç©å·(éå¢)æéå¬å¸

提前致谢。

4

2 回答 2

2

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

jdbc:mysql://localhost:3306/yourdb?characterEncoding=UTF-8
于 2012-10-12T01:25:25.360 回答
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>
于 2013-11-17T18:51:35.730 回答