在下想完成这样一个简单的功能:
在页面上选择一个带有中文字符的下拉框中的值,引起级联变化。
但是第一个下拉框中的值传给Action后一直是乱码,恳请各位解决。下面贴出页面代码:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Insert title here
label="地区"
list="@xxx.xxx.XClass@XX_LIST"
name="s_district" emptyOption="true"
onchange="buildSelects(this.value,'company');" />
<s:select id="company" label="单位" list="#{'':'--请选择--'}"
name="s_company" />
以下是js代码:
//全局变量定义
var searchReq = createAjaxObj();
var selectUpdateId;// 要变动的Select ID
var arrayIds;// 联动select ID
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else {
if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert("您的浏览器不支持");
}
}
}
function createAjaxObj() {
var httprequest = false;
if (window.XMLHttpRequest) { // if Mozilla, Safari etc
httprequest = new XMLHttpRequest();
if (httprequest.overrideMimeType) {
httprequest.overrideMimeType("text/xml");
}
} else {
if (window.ActiveXObject) { // if IE
try {
httprequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httprequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
}
}
}
}
return httprequest;
}
// Starts the AJAX request.
function buildSelects(propValue, subSelectId) {
// alert("buildSelects");
delSubInfos(subSelectId);
selectUpdateId = subSelectId;
if (searchReq.readyState == 4 || searchReq.readyState == 0) {
// var str = escape(document.getElementById("txtSearch").value);
alert(propValue);
searchReq.open("GET", "select.action?search=" + propValue, true);
searchReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
searchReq.onreadystatechange = handleSearchSuggest;
searchReq.send(null);
}
}
// Called when the AJAX response is returned.
function handleSearchSuggest() {
if (searchReq.readyState == 4) {
var str = searchReq.responseText;
alert(str + ", " + selectUpdateId);
var mes = str.split(",");
delSubInfos(selectUpdateId);
for (i = 0; i < mes.length; i++) {
var infos = mes[i].split("'");
document.getElementById(selectUpdateId).appendChild(createOption(infos[0], infos[1]));
}
}
}
// 创建Select Options
function createOption(propValue, textValue) {
var ops = document.createElement("option");
ops.setAttribute("value", propValue);
ops.appendChild(document.createTextNode(textValue));
return ops;
}
// 删除 Select Options
function clearOptions(selectNode) {
selectNode.length = 1;
// selectNode.childNodes[0].selected=true;
}
// initialization
function initAjax(argumentsMa) {
arrayIds = arguments;
alert(arrayIds[0] + "," + arrayIds[1]);
}
function delSubInfos(targetId) {
var canClear = false;
for (var i = 0; i < arrayIds.length; i++) {
//alert(arrayIds[i] + "," + targetId);
if (arrayIds[i] == targetId) {
canClear = true;
}
if (canClear) {
clearOptions(document.getElementById(arrayIds[i]));
}
}
}
先谢谢各位了
[b]问题补充:[/b]
To energyKey:
1、谢谢,但是。。。修改成POST以后仍然是乱码
2、在Action中有对request进行编码
以下是Action的代码
public class SelectAction extends ActionSupport {
private String search;
public String direct() throws IOException {
HttpServletRequest request = ServletActionContext.getRequest();
request.setCharacterEncoding("UTF-8");
String s_search = URLDecoder.decode(search, "UTF-8");
System.out.println(search + "," + s_search);
// System.out.println("search=" + search);
// 输出信息
String news = null;
try {
news = ProvinceData.PROVINCE_DATA.get(search).toString();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
HttpServletResponse response = ServletActionContext.getResponse();
// 设置编码
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
out.print(news);
// 关闭流
out.close();
return SUCCESS;
}
public String getSearch() {
return search;
}
public void setSearch(String search) {
this.search = search;
}
}