总结彻底解决Spring MVC+Mybatis中文乱码问题

Java对于新手最容易出现的问题就是中文乱码的问题。今天我就来总结一下彻底解决Spring mvc+Mybatis中文乱码的方案。

首先要看打一断点看一下Controller接收到参数值是否正常。如果不正常多半是因为Spring或者页面编码的设置问题。

 

遇到的问题:


    public @ResponseBody String cancleask(...) throws UnsupportedEncodingException {
        RSResult result = new RSResult();
        
        try {
            Ask cond = new Ask();
            String newcancelReason = new String(cancelReason.getBytes("iso-8859-1"),"utf-8");            
            cond.setCancelReason(newcancelReason);
            cond.setAskid(Integer.parseInt(askid));
            cond.setStatus(Integer.parseInt(userType)==3?6:5);
            if (askid != null && !askid.equals("")) {
                int retval = askBiz.cancleAsk(cond);
                result.setResult(retval);
                result.setMessage("取消咨询ok!");
            } else {
                result.setResult(-1);
                result.setMessage("取消咨询error!");
            }
        } catch (BizException ex) {
            log.error("取消咨询 :" + ex.getMessage());
            result.setResult(-1);
            result.setMessage("取消咨询error!");
        }
        return JSONObject.fromObject(result).toString();
    }

 

加上这一行导致 添加数据到数据库的是乱码。

 

一、Spring或页面编码问题

在JSP页面第一行加上下面代码:


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

因为Springmvc采用默认的编码(ISO-8859-1)进行解析参数, 这时就会出现乱码问题

在Web.xml加上Spring编码转换过滤器filter。

  1. <filter>
  2. <filter-name>encodingFilter</filter-name>
  3. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  4. <init-param>
  5. <param-name>encoding</param-name>
  6. <param-value>utf-8</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>forceEncoding</param-name>
  10. <param-value>true</param-value>
  11. </init-param>
  12. </filter>
  13. <filter-mapping>
  14. <filter-name>encodingFilter</filter-name>
  15. <url-pattern>/*</url-pattern>
  16. </filter-mapping>


二、Web容器的问题

如果上面方案一还没有解决乱码的问题,看一下你的Web容器的问题的编码设置,比如我使用的是Tomcat,找到server.xml。

可以看到Connector没有设置编码。加上编码属性URIEncoding,如下:


  1. <Connector port="8081" protocol="HTTP/1.1"
  2. connectionTimeout="20000"
  3. redirectPort="8443" URIEncoding="UTF-8"/>

端口我使用是8081。上面主要是解决GET请求中文乱码的问题。


三、数据库或者链接数据库问题

如果通过打断点看到Spring Controller接收到值中方是正常的,但是插入数据库之后就是乱码了。一般这种情况无非就是两种问题。

1、数据库编码、表编码、列编码依次检查是否是UTF-8编码

2、mysql的链接字符串加上编码参数,如下:

  1. <environments default="development">
  2. <environment id="development">
  3. <transactionManager type="JDBC"/>
  4. <dataSource type="POOLED">
  5. <property name="driver" value="com.mysql.jdbc.Driver"/>
  6. <property name="url" value="jdbc:mysql://127.0.0.1:3306/lanhuprivi?useUnicode=true&amp;characterEncoding=UTF-8"/>
  7. <property name="username" value="root"/>
  8. <property name="password" value="root"/>
  9. </dataSource>
  10. </environment>
  11. </environments>

四、Response或者Servlet乱码问题

第一种方法:


  1. //getWriter()方法将 输出编码设置成iso-8859-1,这样输出utf8编码字符串必然乱码
  2. PrintWriter pw = response.getWriter();
  3. //1、
  4. //response.setCharacterEncoding("UTF-8");
  5. //2、
  6. response.setContentType("text/html; charset=utf-8");
  7. pw.write(resStr);
  8. pw.flush();
  9. pw.close();

setContentType 和 setCharacterEncoding两方法中设定characterEncoding的方法对服务器效果一致,不需要反复调用。


在输出文本内容时,采用response.setContentType("text/html; charset=utf-8");似乎更为方便。


第二种方法:


    1. PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(), "UTF-8"));
原文地址:https://www.cnblogs.com/QAZLIU/p/6800814.html