SelectProvAction.java package com.xxx.hjw.action;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import com.opensymphony.xwork2.ActionSupport;
import com.xxx.hco.HcoFactory;
import com.xxx.hco.entity.HbtRegionEntity;
public class SelectProvAction extends ActionSupport{
private static final long serialVersionUID = 1L;
/**
* dom4j生成XML文件
* @param id
* @return
*/
public String getProv(){
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/xml;charset=UTF-8");
Map<Integer,String> map = new HashMap<Integer,String>();
List<HbtRegionEntity> list = HcoFactory.getHbtRegionRemote().findListById(Integer.valueOf(request.getParameter("regionId")));
for(HbtRegionEntity hre : list){
map.put(hre.getRegionId(), hre.getNAME());
}
Document doc = DocumentHelper.createDocument();
Element rootElt = doc.addElement("items");
for (Iterator<Map.Entry<Integer, String>> iter=map.entrySet().iterator(); iter.hasNext();) {
Map.Entry<Integer, String> entry = iter.next();
Element itemElt = rootElt.addElement("item");
Element idElt = itemElt.addElement("id");
idElt.setText(String.valueOf(entry.getKey()));
Element nameElt = itemElt.addElement("name");
nameElt.setText(entry.getValue());
}
String xmlString = doc.asXML();
//System.out.println(xmlString);
try {
response.getWriter().write(xmlString);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
页面js
<script type="text/javascript">
function selectProv(field) {
var xmlHttp = null;
//表示当前浏览器不是ie,如ns,firefox
if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = "private/hbhjw/selectProv.action?regionId=" + field.value + "&time=" + new Date().getTime();
//设置请求方式为GET,设置请求的URL,设置为异步提交
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange=function() {
//Ajax引擎状态为成功
if (xmlHttp.readyState == 4) {
//HTTP协议状态为成功
if (xmlHttp.status == 200) {
var doc = xmlHttp.responseXML;
var items = doc.getElementsByTagName("item");
//取得省下拉列表
var provSelect = document.getElementById("statUnitCode");
//清除省下拉列表中的值
provSelect.options.length = 0;
var o = new Option("=选择区=", 0);
provSelect.add(o);
for (var i=0; i<items.length; i++) {
//var id = items[i].childNodes[0].childNodes[0].nodeValue;
//var name = items[i].childNodes[1].childNodes[0].nodeValue;
var id = items[i].childNodes[0].firstChild.nodeValue;
var name = items[i].childNodes[1].firstChild.nodeValue;
//alert(id + "," + name);
//构造下拉列表的Option对象
var o = new Option(name, id);
//向下拉列表中添加选项
provSelect.add(o);
}
}else {
alert("请求失败,错误码=" + xmlHttp.status);
}
}
};
//将设置信息发送到Ajax引擎
xmlHttp.send(null);
}
</script>