`
jiuyuehe
  • 浏览: 181182 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

jquery ajax struts2 数据库等中文乱码问题解决大全

阅读更多
ok 乱码了。

乱码第一件事,静下心来,思考一下这乱码出处的流程。

1、后台发页面的乱码。

首先检查下后台打印出来是否是乱码。

数据来源是1)从数据库出来的话,检查数据库里面是否已经乱码了。检查下数据库的字符集。
          2)是从文件中读出来的话,一定要注意看是用什么格式读取,文件本身是什么格式编码。
注意了这俩2点,来源就清楚了。
读取文件代码:
try {  
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));  
            while(true){  
                String line = br.readLine();  
                if(line!=null){  
                    sb.append(line+"\n");  
                }else{  
                    break;  
                }  
            }  
            br.close();  
            //response.setCharacterEncoding("UTF-8");  //注意这里要在getWriter()之前。
            PrintWriter  out = response.getWriter();  
            
            //response.setContentType("text/html");  
            response.setContentType("text/html;charset=utf-8");  //写这里是没有任何作用的!!!!!!
              
            System.out.println("sb:"+sb.toString());  
             //这么转化一下是否有必要? 
            String des = new String(sb.toString().getBytes("UTF-8"),"UTF-8");  
              
            System.out.println("des:"+des);  
              
            out.print(des);  
            out.flush();  
            out.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            System.out.println("文件读取错误");  
            e.printStackTrace();  
        }  
        return null;  


注意这样的代码。粗心一看觉得没问题,其实问题多得是。

好了以上问题没有。是页面显示乱码了。
  那就看看:1.页面是什么编码。跟response set 的是否一样?
            2.tomcat 里面是否加了 Tomcat中的server.xml中的URIEncoding
            3.用的struts2 的mvc ,那设置了 <conston name="struts.i18n.encoding" value="utf-8"/>;(此处一定要跟页面编码一致


如果以上都对了,那就不会有问题了。

2 页面后台发送数据乱码

页面提交到后台,要么是form 表单,要么是ajax .

一般出现乱码的情况都是页面编码跟后台编码不一致造成,页面是utf-8 ,后台是utf-8.那么后台设置
request.setCharacterEncoding("uft-8")
以后就不会乱码。

如果页面是gbk或其他形式,那么光靠后台request的设置是没有左右的。

用ajax,或者jquery 的ajax() 提交话,它默认是给你的数据做了encodeURIComponent(data);
他是用的utf-8的编码方式,在页面上,自己提交中文数据的时候还需要手动写一次,也就是2次encodeURIComponent(data);

然后在后台
URLDecoder.decode(content, "utf-8");
解码。这样来解决乱码的问题。

附上点代码
function submitContent(){
			var content = $("#txt").val();
			var newcon = encodeURIComponent(content);
                        //post 提交方式
			$.post("<%=basePath%>pkg_updatePage",
				{ rootpath: allPath, content: newcon },
   				function(data){
					var ll = confirm("are you sure?");
					if(ll==true){
	    			
					}else{
						return;
					}
    			 
   		});
		}


数据比较大的时候一定要用post .而不能用get get传输数据最大的长度约2k。超过就会直接返回请求错误。
  还见过一些这样的错误 (错误写法):
	var allPath ;
		function geteditData(name){
			var path =rootPath+name;
			allPath = path;
			$.ajax({  
                      async: false,  
                      cache: false,  
                      type:"POST",   //post!
                      dataType: "text",
                      contentType:"application/x-www-form-urlencoded;charset=gbk",
                      url: "<%=basePath%>pkg_readPage?rootpath="+path,  //?xxx=xxx
                      success : function(data){
                    	  //alert(typeof(data));
						  $("#txt").val(data);
                      },
					  error: function(){
                    	  alert("trsData = error")
					  	}	
                       });
				}


url 后面?xxx=xx 其实还是用的是get 方式,post 的争取写法应该给个data参数!

java 接受参数
  //更新文件
	    public String updatePage() throws UnsupportedEncodingException{
	    	request.setCharacterEncoding("gbk");
	    	String fiePath = request.getParameter("rootpath");
	    	String content = request.getParameter("content");
	    	String newcontent=null;
	    	try {
//编码	    		
newcontent = URLDecoder.decode(content, "utf-8");
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			} 
	    	
	    	FileWriter fw;
			try {
				fw = new FileWriter(new File(fiePath));
				fw.write(newcontent);
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println("更新成功");
			return null;
	    }


如此检查 防止乱码!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics