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

ok 乱码了。

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

[size=medium][color=green]1、后台发页面的乱码。[/color][/size]

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

数据来源是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;


[color=red]注意这样的代码。粗心一看觉得没问题,其实问题多得是。[/color]

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


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

[size=medium][color=green]2 页面后台发送数据乱码[/color][/size]

页面提交到后台,要么是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。超过就会直接返回请求错误。
还见过一些这样的错误 ([color=red]错误写法[/color]):
	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;
}


如此检查 防止乱码!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值