struts2 + ajax 中文乱码问题解决
2009-06-29 11:38
537 查看
在发送ajax请求之前,对参数escape()两次( 例:name=escape(escape("张三")); ),然后再发送请求到服务器,服务器接收参数后,对参数unescape(),就可以得到正确的参数( 例:String name=unescape(request.getParameter("name")); ).
private String unescape(String src) {
StringBuffer tmp = new StringBuffer();
tmp.ensureCapacity(src.length());
int lastPos = 0, pos = 0;
char ch;
while (lastPos < src.length()) {
pos = src.indexOf("%", lastPos);
if (pos == lastPos) {
if (src.charAt(pos + 1) == 'u') {
ch = (char) Integer.parseInt(src
.substring(pos + 2, pos + 6), 16);
tmp.append(ch);
lastPos = pos + 6;
} else {
ch = (char) Integer.parseInt(src
.substring(pos + 1, pos + 3), 16);
tmp.append(ch);
lastPos = pos + 3;
}
} else {
if (pos == -1) {
tmp.append(src.substring(lastPos));
lastPos = src.length();
} else {
tmp.append(src.substring(lastPos, pos));
lastPos = pos;
}
}
}
return tmp.toString();
}
private String unescape(String src) {
StringBuffer tmp = new StringBuffer();
tmp.ensureCapacity(src.length());
int lastPos = 0, pos = 0;
char ch;
while (lastPos < src.length()) {
pos = src.indexOf("%", lastPos);
if (pos == lastPos) {
if (src.charAt(pos + 1) == 'u') {
ch = (char) Integer.parseInt(src
.substring(pos + 2, pos + 6), 16);
tmp.append(ch);
lastPos = pos + 6;
} else {
ch = (char) Integer.parseInt(src
.substring(pos + 1, pos + 3), 16);
tmp.append(ch);
lastPos = pos + 3;
}
} else {
if (pos == -1) {
tmp.append(src.substring(lastPos));
lastPos = src.length();
} else {
tmp.append(src.substring(lastPos, pos));
lastPos = pos;
}
}
}
return tmp.toString();
}
相关文章推荐
- struts2_Ajax_post_解决了中文乱码问题
- struts2 url 中文乱码问题解决方法
- 解决使用AJAX传中文参数乱码问题
- 一句话解决AJAX中文乱码问题[推荐]
- 遇到AJAX中文乱码问题终极解决方法
- 解决AJAX传送中文会导致乱码的问题
- 解决struts2以GET方式提交表单中文乱码问题
- struts2 中文乱码问题,自定义过滤器通用解决方法
- 关于struts2中表单提交时,中文乱码问题的解决
- (Struts2解决篇)Struts2解决中文乱码问题
- Struts2中解决POST请求和GET请求中的中文乱码问题
- asp+AJAX 用escape解决url传递中文乱码的问题
- ajax返回中文时乱码的问题解决
- ajax向后台请求数据包含中文乱码问题解决
- JSP,mysql,tomcat下(基于struts2)中文及其乱码问题的解决
- JAVA中应用AJAX的中文乱码问题的解决
- 解决Ajax中文乱码问题
- php+ajax解决中文乱码问题
- 解决Ajax加载JSon数据中文乱码问题
- Ajax解决中文乱码问题