Spring+SiteMesh 2.0 Chinese character support
-
12-06-2021 - |
Question
I use spring3.0 and sitemesh2.0.The issue is am not able to display chinese characters in my jsp page.
I have done the following
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>
In my jsp:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
In sitemesh template:
<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>
But still the chinese characters are displayed like this: å®å¾½çä½³å®ç©å·(éå¢)æéå¬å¸
Thanks in advance.
Solution
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>
OTHER TIPS
Your filter and jsp both look fine. Maybe check database settings as well.
Also bear in mind when you connect to your db, you need to specify characterEncoding
:
jdbc:mysql://localhost:3306/yourdb?characterEncoding=UTF-8
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow