JSF+MYSQL 中文问题

解决中文问题需要3个,

1. JSF 需要一个Filter
public class CharacterEncodingFilter implements Filter {

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
chain.doFilter(req, resp);
}

public void init(FilterConfig filterConfig) throws ServletException {

}

public void destroy() {

}
}

web.xml里配
<filter>
<filter-name>CharacterEncodingFilte</filter-name>
<filter-class>com.hengtiansoft.portal.util.CharacterEncodingFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>CharacterEncodingFilte</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2. MySQL
需要1. 把table 的charset设置成UTF8, COLLECTION 设置成 utf8-general-ci
2. 把字符字段的charset 同样设置成UTF8, COLLECTION 设置成 utf8-general-ci,这个在建表语句中可以体现,例如
`title` varchar(100) CHARACTER SET utf8 NOT NULL,

3. connection的语句要使用
jdbc:mysql://ip:port/dbname?UseUnicode=true&characterEncoding=UTF-8

就可以了。

你可能感兴趣的:(mysql)