AJAX中URL的参数带中文

package action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AddPerson extends ActionSupport {

	public String add() {
		 String name = ServletActionContext.getRequest().getParameter("name");
		 String age = ServletActionContext.getRequest().getParameter("age");
		 String add = ServletActionContext.getRequest().getParameter("add");
		 System.out.println("name:"+name);
		 try {
				name = URLDecoder.decode(name, "UTF-8");
				age = URLDecoder.decode(age, "UTF-8");
				add = URLDecoder.decode(add, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			System.out.println("name:" + name);
			System.out.println("age:" + age);
			System.out.println("add:" + add);
				return this.SUCCESS;
	}
}

 今天朋友让帮忙处理乱码的问题,具体的操作为:当在页面选择某项执行修改操作,将弹出一个修改信息对话框,里面可能会输入中文,当点击提交按钮后,通过Javascript中国的AJAX通信,给后台Struts2中的Aciton传递信息进行处理。

其实这么做已经有问题了,应该直接使用Struts传递,但是因为之前是别人写的,就这么传,那也只能继续这么写,不然项目要改的太多~~(管理太混乱了)

很显然,传给Action的参数出现了乱码~

其实很简单,前台得到后需要进行编码,后台再进行解码。

以为Url中中文是需要进行编码的。

 

我就自己写了测试的代码,

 

前台页面如下:

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/thickbox_plus.js"></script>
<script type="text/javascript" src="js/myfunction.js"></script>

<link rel="stylesheet" type="text/css" href="css/thickbox.css"/>

<title>Insert title here</title>
</head>
<body>

Name:<input type="text" id="name"></input>
<br/>
Age:<input type="text" id="age"></input>
<br/>
Address:<input type="text" id="address"></input>
<br/>
<input type="button" value="Add person" οnclick="submit()"></input>

</body>
</html>

提交相应的javascript代码 ,这里需要使用encodeURI进行编码,而且要执行两次,因为是测试,也就不管反馈数据了。

 

function submit() {
	var name = $("#name").val();
	var age = $("#age").val();
	var add = $("#address").val();

	var url = "toAdd.action?name=" + name + "&age=" + age + "&add=" + add;
	url = encodeURI(url);
	url=encodeURI(url); 
	alert("URL:" + url);
	$.get(url);

	}

 

后台的Action如下:

 

package action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AddPerson extends ActionSupport {

	public String add() {
		String name = ServletActionContext.getRequest().getParameter("name");
		String age = ServletActionContext.getRequest().getParameter("age");
		String add = ServletActionContext.getRequest().getParameter("add");
		System.out.println("name:" + name);
		try {
			name = URLDecoder.decode(name, "UTF-8");
			age = URLDecoder.decode(age, "UTF-8");
			add = URLDecoder.decode(add, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println("name:" + name);
		System.out.println("age:" + age);
		System.out.println("add:" + add);

		return this.SUCCESS;
	}
}

 

 

 

name:%E4%BD%A0%E5%A5%BD
name:你好
age:么
add:不dsf2

 

经测试,中文得以正常在后台显示。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值