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
经测试,中文得以正常在后台显示。