我使用 spring3.0 和 sitemesh2.0.问题是无法在我的 jsp 页面中显示中文字符。
我已经做了以下工作
网络.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>
但汉字仍然显示如下:®Å å3/41/2çä1/2³å ç©å®·(éå¢)æéå¬å ̧
提前谢谢。
你的过滤器和 jsp 看起来都很好。也许还要检查数据库设置。另请记住,当您连接到数据库时,您需要指定characterEncoding
:
jdbc:mysql://localhost:3306/yourdb?characterEncoding=UTF-8
回复有点晚,但我希望其他人可以从我浪费的时间中受益。春天的过滤器对我也不起作用。我编写了自己的代码并手动设置了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>