url传递中文乱码解决

6 篇文章 0 订阅

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.       
  8.     var textArea = $("#textArea");//地区错误提示位置  
  9.       
  10.     /** 
  11.      * 点击查询,判断至少选择了一个面值,否则不能查询 
  12.      */  
  13.     var obj = document.getElementsByName("money");  
  14.     for( var i=0; i<obj.length; i++)  
  15.     {  
  16.         if(obj[i].checked)  
  17.         {  
  18.             checkMoney = true;  
  19.             break;  
  20.         }  
  21.         else  
  22.         {  
  23.             checkMoney = false;  
  24.         }  
  25.     }  
  26.       
  27.     if( ($("#Area_a").val() != 'no') && ($("#Area_b").val() != 'no') && checkMoney == true )  
  28.     {  
  29.         var checkText=$("#Area_b").find("option:selected").text();//###这里得到select被选中option的text  
  30.         var Area_b_text = encodeURI(encodeURI(checkText));  
  31.         $("#form1").attr("action","priceStrategy_querAllPriceStrategy2?Area_b_text="+Area_b_text);  
  32.         $("#form1").submit();  
  33.     }  
  34.     else  
  35.     {  
  36.         textArea.html("<font color='red'>选择地区!</font>");  
  37.         textMoney.html("<br><font color='red'>至少选择一种面值!</font>");  
  38.     }  
  39. }  

 

 

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" οnchange="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.                   
  46.             </td>  
  47.             <td>  
  48.                 <input type="button" value=" 查询 "  οnclick="queryPrice()"/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
  49.                 <input type="button" value=" 重置 " οnclick="resetQueryPrice()"/>  
  50.             </td>  
  51.             <td>&nbsp;</td>  
  52.         </tr>  
  53.     </table>  

 

 

Java代码   收藏代码
  1. 3)struts2的Action中getter方法这样设置:  
  2. private String Area_b_text;//第二个select中的text  
  3. public void setArea_b(String areaB) {  
  4.       
  5.     Area_b = areaB;  
  6. }  
  7. public String getArea_b_text() throws UnsupportedEncodingException {  
  8.       
  9.     return java.net.URLDecoder.decode(Area_b_text,"UTF-8");//前台获得时自动转为UTF-8编码格式  
  10. }  

 

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

###其他资料

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");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值