1、先看瀏覽器編碼是否ok;
2、修改請求為post;(get提交,中文容易亂碼)
3、接口指定編碼utf-8;(指定content-type)
我的問題是將get改為 post 解決的;
也可參考其他resource,such as:
=================================================================
轉自:https://www.cnblogs.com/sxdcgaq8080/p/5741461.html;
【前台 亂碼】 前台單獨亂碼+后台往前台傳輸的數據亂碼
解決方法:
第一:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> 這句話是加在每個頁面的最上面,在<!DOCTYPE html>之前加的 並且 request.setCharacterEncoding("gb2312");
第二:
配置Tomcat的 server.xml。 <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>
第三:
配置 web.xml <filter> <filter-name>setEncoding</filter-name> <filter-class>com.dinner.filter.CharsetFilters</filter-class> <init-param> <param-name>coding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>setEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
第四:
工程編碼 開發中發現Windows版Eclipse默認Java和Web工程的默認編碼方式不那么遂人願,修改方法如下: 1. Web工程文件編碼修改方式 Window -> Preference -> Content types 推薦將Web相關文件的編碼都設置為UTF-8 2. Java源文件編碼修改方式 Window -> Preference -> Workspace -> Text file encoding Windows平台默認為GBK,Linux平台默認為UTF-8
第五:
如果使用ajax前后台數據交互,可以改變傳輸方式,將type設置為post
$.ajax({url:"productAdd.htmls", type:"post", data:{ "productName" : productName, "productCre" : productCre }, success:function(data){ if(data != null){ var product = eval("("+data+")"); temp="<tr class='text-c'><td><input type='checkbox' value='"+product.productId+ "'></td><td>"+product.productName+"</td><td><a href='checkdisease.htmls'>【查看疾病信息】</a><a href='updateproduct.htmls'>【更新】</a></td></tr>"; $('.table-bordered tbody tr:first' , parent.document).before(temp); $('.table-bordered tbody tr:last' , parent.document).remove(); } // var index = parent.layer.getFrameIndex(window.name); 可以用這個獲取當前要關閉的layer ,也可以使用parent.indexProductAdd 獲取在父層定義的那個layer。open()的彈窗 parent.layer.close(parent.indexProductAdd); //獲取到layer的彈出窗 關閉它 }});
這樣即可以解決!!!
第六:
在type不能設置為post的情況下,可以在后台接收到數據之后,自行進行解碼
【 URLDecoder.decode(condition, "utf-8");】參數1:字符串 參數2:編碼方式
@RequestMapping(value= "/statistics" ,produces = "text/html;charset=UTF-8") @ResponseBody public String statistics(HttpServletRequest request,String condition,String questOptions) throws UnsupportedEncodingException{ questOptions = questOptions.replaceAll("category=", ""); String [] questArr = questOptions.split("&"); condition = URLDecoder.decode(condition, "utf-8"); System.out.println(condition); System.out.println(questOptions); return null; }
在這里 獲取到轉碼后的字符串的 效果 比【new String(str.getBytes("ISO-8859-1"),"utf-8")】要好得多!
第七:
整個項目,單頁面跳轉的情況下,出現亂碼問題怎么解決?