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" onclick="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

 

經測試,中文得以正常在後臺顯示。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章