JS中文转换(UTF-8),中文乱码解决办法,url传递中文乱码解决
2016-07-19 12:06
926 查看
转自:http://jun1986.iteye.com/blog/1056732
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代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201607/5d44497d6be8a8e3f8f76cb77ebdd2b3.png)
1)js代码:
/*模糊查询价格策略*/
function queryPrice()
{
var checkMoney = true;
var textMoney = $("#textMoney");
var textArea = $("#textArea");//地区错误提示位置
/**
* 点击查询,判断至少选择了一个面值,否则不能查询
*/
var obj = document.getElementsByName("money");
for( var i=0; i<obj.length; i++)
{
if(obj[i].checked)
{
checkMoney = true;
break;
}
else
{
checkMoney = false;
}
}
if( ($("#Area_a").val() != 'no') && ($("#Area_b").val() != 'no') && checkMoney == true )
{
var checkText=$("#Area_b").find("option:selected").text();//###这里得到select被选中option的text
var Area_b_text = encodeURI(encodeURI(checkText));
$("#form1").attr("action","priceStrategy_querAllPriceStrategy2?Area_b_text="+Area_b_text);
$("#form1").submit();
}
else
{
textArea.html("<font color='red'>选择地区!</font>");
textMoney.html("<br><font color='red'>至少选择一种面值!</font>");
}
}
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201607/5d44497d6be8a8e3f8f76cb77ebdd2b3.png)
2)jsp页面
<table border=1 width="100%">
<tr>
<td colspan="3">查询操作</td>
</tr>
<tr>
<td>
地区:
</td>
<td>
<select id="Area_a" name="Area_a" size="1" onchange="getAllCityOrProvince()">
<c:choose>
<c:when test="${ Area_a == 'nei' }">
<option value="no">==请选择==</option>
<option value="nei" selected="selected">省内</option>
<option value="wai">省外</option>
</c:when>
<c:when test="${ Area_a == 'wai' }">
<option value="no">==请选择==</option>
<option value="nei">省内</option>
<option value="wai" selected="selected">省外</option>
</c:when>
<c:otherwise>
<option value="no" selected="selected">==请选择==</option>
<option value="nei">省内</option>
<option value="wai">省外</option>
</c:otherwise>
</c:choose>
</select>
<select id="Area_b" name="Area_b" size="1">
<c:choose>
<c:when test="${ Area_b_text != null }">
<option value="${ Area_b }">${ Area_b_text }</option>
</c:when>
<c:otherwise>
<option value="no"> </option>
</c:otherwise>
</c:choose>
</select>
</td>
<td><span id="textArea"></span></td>
</tr>
<tr>
<td>
</td>
<td>
<input type="button" value=" 查询 " onclick="queryPrice()"/>
<input type="button" value=" 重置 " onclick="resetQueryPrice()"/>
</td>
<td> </td>
</tr>
</table>
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201607/5d44497d6be8a8e3f8f76cb77ebdd2b3.png)
3)struts2的Action中getter方法这样设置:
private String Area_b_text;//第二个select中的text
public void setArea_b(String areaB) {
Area_b = areaB;
}
public String getArea_b_text() throws UnsupportedEncodingException {
return java.net.URLDecoder.decode(Area_b_text,"UTF-8");//前台获得时自动转为UTF-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");
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代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201607/5d44497d6be8a8e3f8f76cb77ebdd2b3.png)
1)js代码:
/*模糊查询价格策略*/
function queryPrice()
{
var checkMoney = true;
var textMoney = $("#textMoney");
var textArea = $("#textArea");//地区错误提示位置
/**
* 点击查询,判断至少选择了一个面值,否则不能查询
*/
var obj = document.getElementsByName("money");
for( var i=0; i<obj.length; i++)
{
if(obj[i].checked)
{
checkMoney = true;
break;
}
else
{
checkMoney = false;
}
}
if( ($("#Area_a").val() != 'no') && ($("#Area_b").val() != 'no') && checkMoney == true )
{
var checkText=$("#Area_b").find("option:selected").text();//###这里得到select被选中option的text
var Area_b_text = encodeURI(encodeURI(checkText));
$("#form1").attr("action","priceStrategy_querAllPriceStrategy2?Area_b_text="+Area_b_text);
$("#form1").submit();
}
else
{
textArea.html("<font color='red'>选择地区!</font>");
textMoney.html("<br><font color='red'>至少选择一种面值!</font>");
}
}
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201607/5d44497d6be8a8e3f8f76cb77ebdd2b3.png)
2)jsp页面
<table border=1 width="100%">
<tr>
<td colspan="3">查询操作</td>
</tr>
<tr>
<td>
地区:
</td>
<td>
<select id="Area_a" name="Area_a" size="1" onchange="getAllCityOrProvince()">
<c:choose>
<c:when test="${ Area_a == 'nei' }">
<option value="no">==请选择==</option>
<option value="nei" selected="selected">省内</option>
<option value="wai">省外</option>
</c:when>
<c:when test="${ Area_a == 'wai' }">
<option value="no">==请选择==</option>
<option value="nei">省内</option>
<option value="wai" selected="selected">省外</option>
</c:when>
<c:otherwise>
<option value="no" selected="selected">==请选择==</option>
<option value="nei">省内</option>
<option value="wai">省外</option>
</c:otherwise>
</c:choose>
</select>
<select id="Area_b" name="Area_b" size="1">
<c:choose>
<c:when test="${ Area_b_text != null }">
<option value="${ Area_b }">${ Area_b_text }</option>
</c:when>
<c:otherwise>
<option value="no"> </option>
</c:otherwise>
</c:choose>
</select>
</td>
<td><span id="textArea"></span></td>
</tr>
<tr>
<td>
</td>
<td>
<input type="button" value=" 查询 " onclick="queryPrice()"/>
<input type="button" value=" 重置 " onclick="resetQueryPrice()"/>
</td>
<td> </td>
</tr>
</table>
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201607/5d44497d6be8a8e3f8f76cb77ebdd2b3.png)
3)struts2的Action中getter方法这样设置:
private String Area_b_text;//第二个select中的text
public void setArea_b(String areaB) {
Area_b = areaB;
}
public String getArea_b_text() throws UnsupportedEncodingException {
return java.net.URLDecoder.decode(Area_b_text,"UTF-8");//前台获得时自动转为UTF-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");
相关文章推荐
- Extjs4.0 最新最全视频教程
- Javascript中toFixed方法的改进
- 5个常见可用性错误和解决方案
- js数组实现图片轮播
- js可突破windows弹退效果代码
- JSP脚本漏洞面面观
- 使用BAT一句话命令实现快速合并JS、CSS
- js显示当前星期的起止日期的脚本
- 爆炸式的JS圆形浮动菜单特效代码
- js select常用操作控制代码
- JS实现不使用图片仿Windows右键菜单效果代码
- 从jsp发送动态图像
- 原生js结合html5制作小飞龙的简易跳球
- js 页面模块自由拖动实例
- js实现小鱼吐泡泡在页面游动特效
- js 提交和设置表单的值
- PHP VBS JS 函数 对照表
- node.js抓取并分析网页内容有无特殊内容的js文件
- PHP+JS实现大规模数据提交的方法
- 仿51JOB的地区选择效果(可选择多个地区)