解决方法:
第一:
<%@ 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=""+product.productName+"【查看疾病信息】【更新】"; $('.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")】要好得多!
第七:
整个项目,单页面跳转的情况下,出现乱码问题怎么解决?