從js向Action傳中文參數(shù)出現(xiàn)亂碼問題的解決方法
更新時間:2013年12月29日 16:38:19 作者:
Action獲取jsp表單中的中文參數(shù),只要整個項目都采用UTF-8編碼格式都不會出現(xiàn)亂碼問題;但JSP中用到JS,并從JS向Action傳中文參數(shù),就會出現(xiàn)中文亂的現(xiàn)象
做項目的時候,發(fā)現(xiàn)Action獲取jsp表單中的中文參數(shù),只要整個項目都采用UTF-8編碼格式都不會出現(xiàn)亂碼問題;但JSP中用到JS,并從JS向Action傳中文參數(shù),就會出現(xiàn)中文亂的現(xiàn)象。幾經詢問百度,上面說法很多。
經過實踐發(fā)現(xiàn)下面的方法可以解決中文亂碼問題:
JSP的JS中:中文參數(shù)用encodeURI(encodeURI(中文參數(shù))),經過兩次轉碼。例如:
function show(next,id,realName){
document.forms['f2'].action="usersearchNextPage?next="+next+"&id="+id+"&realName="+encodeURI(encodeURI(realName));
document.forms['f2'].submit();
}
其中 realName是中文參數(shù)。故在提交的URL中將realName轉碼兩次。encodeURI(encodeURI(realName))
Action中:接收中文參數(shù)時解碼。用:java.net.URLDecoder.decode(realName,"UTF-8");
如:
String realName = ServletActionContext.getRequest().getParameter("realName");
try {
realName = java.net.URLDecoder.decode(realName,"UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
經過上述處理,問題解決。
經過實踐發(fā)現(xiàn)下面的方法可以解決中文亂碼問題:
JSP的JS中:中文參數(shù)用encodeURI(encodeURI(中文參數(shù))),經過兩次轉碼。例如:
復制代碼 代碼如下:
function show(next,id,realName){
document.forms['f2'].action="usersearchNextPage?next="+next+"&id="+id+"&realName="+encodeURI(encodeURI(realName));
document.forms['f2'].submit();
}
其中 realName是中文參數(shù)。故在提交的URL中將realName轉碼兩次。encodeURI(encodeURI(realName))
Action中:接收中文參數(shù)時解碼。用:java.net.URLDecoder.decode(realName,"UTF-8");
如:
復制代碼 代碼如下:
String realName = ServletActionContext.getRequest().getParameter("realName");
try {
realName = java.net.URLDecoder.decode(realName,"UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
經過上述處理,問題解決。
相關文章
js模擬jquery的slide和fadeIn和fadeOut功能
以前用過jquery的slideUp,slideDown,等許多很不錯的方法,感覺很容易就能實現(xiàn)頁面元素的動畫效果!2010-07-07