日期:2014-05-17  浏览次数:20701 次

【求助】 mysql 中文乱码
初学ssh,向mysql数据库里存入的中文全部变为乱码。如图:



mysql中的字符编码格式都是utf8,如图:





jsp的编码格式也是utf8,

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

按照网上查过的一些资料,还在Web.xml中配置了encodingFilter过滤器,代码:

<filter>  
           <filter-name>encodeFilter</filter-name>  
           <filter-class>com.test.common.EncodeFilter</filter-class>  
           <init-param>  
               <param-name>encoding</param-name>  
               <param-value>UTF-8</param-value>  
           </init-param>  
</filter>
        
        <filter-mapping>  
           <filter-name>encodeFilter</filter-name>  
           <url-pattern>/*</url-pattern>  
</filter-mapping>


过滤器代码:



package com.test.common;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;

public class EncodeFilter extends StrutsPrepareAndExecuteFilter implements
Filter
{
private FilterConfig config = null;
private String encoding = null;

@Override
public void init(FilterConfig config) throws ServletException
{
this.config = config;
}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
if (encoding == null)
{
encoding = config.getInitParameter("encoding");
}
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}

@Override
public void destroy()
{
config = null;
encoding = null;
}
}



在spring里的applicationContext.xml里的数据库连接也修改过:

<property