1. 程式人生 > >url傳遞中文亂碼解決

url傳遞中文亂碼解決

js合成url時,如果引數是中文,傳到struts2中會亂碼,解決辦法如下:
1.js檔案中使用encodeURI()方法(必須套兩層)。
login_name = encodeURI(encodeURI(login_name));  

2.action中URLDecoder解碼
loginName = Java.net.URLDecoder.decode(loginName,"UTF-8"); 
-------------------------------------------------------------------------------------

實際應用如下queryPrice()方法:

Java程式碼  收藏程式碼
  1. 1)js程式碼:  
  2. /*模糊查詢價格策略*/  
  3. function queryPrice()  
  4. {  
  5.     var checkMoney = true;  
  6.     var textMoney = $("#textMoney");  
  7.     var textArea = $("#textArea");//地區錯誤提示位置  
  8.     /** 
  9.      * 點選查詢,判斷至少選擇了一個面值,否則不能查詢 
  10.      */  
  11.     var obj = document.getElementsByName("money");  
  12.     for( var i=0; i<obj.length; i++)  
  13.     {  
  14.         if(obj[i].checked)  
  15.         {  
  16.             checkMoney = true;  
  17.             break;  
  18.         }  
  19.         else  
  20.         {  
  21.             checkMoney = false;  
  22.         }  
  23.     }  
  24.     if( ($("#Area_a").val() != 'no') && ($("#Area_b").val() != 'no') && checkMoney == true
     )  
  25.     {  
  26.         var checkText=$("#Area_b").find("option:selected").text();//###這裡得到select被選中option的text  
  27.         var Area_b_text = encodeURI(encodeURI(checkText));  
  28.         $("#form1").attr("action","priceStrategy_querAllPriceStrategy2?Area_b_text="+Area_b_text);  
  29.         $("#form1").submit();  
  30.     }  
  31.     else  
  32.     {  
  33.         textArea.html("<font color='red'>選擇地區!</font>");  
  34.         textMoney.html("<br><font color='red'>至少選擇一種面值!</font>");  
  35.     }  
  36. }  
Java程式碼  收藏程式碼
  1. 2)jsp頁面  
  2.     <table border=1 width="100%">  
  3.         <tr>  
  4.             <td colspan="3">查詢操作</td>  
  5.         </tr>  
  6.         <tr>  
  7.             <td>  
  8.                 地區:  
  9.             </td>  
  10.             <td>  
  11.                 <select id="Area_a" name="Area_a" size="1" onchange="getAllCityOrProvince()">  
  12.                     <c:choose>  
  13.                         <c:when test="${ Area_a == 'nei' }">  
  14.                             <option value="no">==請選擇==</option>  
  15.                             <option value="nei" selected="selected">省內</option>  
  16.                             <option value="wai">省外</option>  
  17.                         </c:when>  
  18.                         <c:when test="${ Area_a == 'wai' }">  
  19.                             <option value="no">==請選擇==</option>  
  20.                             <option value="nei">省內</option>  
  21.                             <option value="wai" selected="selected">省外</option>  
  22.                         </c:when>  
  23.                         <c:otherwise>  
  24.                             <option value="no" selected="selected">==請選擇==</option>  
  25.                             <option value="nei">省內</option>  
  26.                             <option value="wai">省外</option>  
  27.                         </c:otherwise>  
  28.                     </c:choose>  
  29.                 </select>  
  30.                 <select id="Area_b" name="Area_b" size="1">  
  31.                     <c:choose>  
  32.                         <c:when test="${ Area_b_text != null }">  
  33.                             <option value="${ Area_b }">${ Area_b_text }</option>  
  34.                         </c:when>  
  35.                         <c:otherwise>  
  36.                             <option value="no">&nbsp;&nbsp;&nbsp;&nbsp;</option>  
  37.                         </c:otherwise>  
  38.                     </c:choose>  
  39.                 </select>  
  40.             </td>  
  41.             <td><span id="textArea"></span></td>  
  42.         </tr>  
  43.         <tr>  
  44.             <td>  
  45.             </td>  
  46.             <td>  
  47.                 <input type="button" value=" 查詢 "  onclick="queryPrice()"/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
  48.                 <input type="button" value=" 重置 " onclick="resetQueryPrice()"/>  
  49.             </td>  
  50.             <td>&nbsp;</td>  
  51.         </tr>  
  52.     </table>  
Java程式碼  收藏程式碼
  1. 3)struts2的Action中getter方法這樣設定:  
  2. private String Area_b_text;//第二個select中的text  
  3. public void setArea_b(String areaB) {  
  4.     Area_b = areaB;  
  5. }  
  6. public String getArea_b_text() throws UnsupportedEncodingException {  
  7.     return java.net.URLDecoder.decode(Area_b_text,"UTF-8");//前臺獲得時自動轉為UTF-8編碼格式  
  8. }  

=====================================================================================

###其他資料

url傳遞中文
如果jsp頁面,myeclipse、web.xml中org.springframework.web.filter.CharacterEncodingFilter,都是UTF-8編碼,
直接傳中文一般是不會亂碼的,如果再有亂碼,可以用以下的方式試試。
目前收集到4中方法,中文傳參一documentPath為例:
1.改為form方式提交,不用超連結方式提交,用form方式傳參指定不亂碼。

2.通過encodeURI(encodeURI(checkText))提交,java程式碼中用URLDecoder.decode解碼:
<script>
function download(documentPath){
  var url = "<c:url value='/product/download.action?documentPath='/>"+documentPath;
  url = encodeURI(encodeURI(url));
  window.location.href=url;
}
</script>
java程式碼中取中文:
String documentPath = (String) request.getParameter('documentPath');
documentPath = URLDecoder.decode(documentPath,"utf-8");

3.修改tomcat的server.xml中的connector,新增URLEncoding="UTF-8"

4.中文從java中傳到jsp再通過url傳到java:
java中編碼:URLEncoder.encode(URLEncoder.encode("傳遞的中文","utf-8"));
java中解碼碼:URLDecoder.decode(request.getParameter('documentPath'),"utf-8");