var deparementids =$("#deparementid").val();
$.ajax({
url: '<%=request.getContextPath()%>/contractAction_departmentuser.action?deparementids='+deparementids,
type: "post",
dataType: "xml",
timeout: 3000,
async : false,
error: function(){
alert(数据加载错误);
},
success: function(xml){
$("#employeeid").empty();
$("#xm").append("<option value=\"\">---请选择---</option>");
$(xml).find("result").each(function(){
var employeeid = $(this).find("employeeid").text();
var employeename = $(this).find("employeename").text();
$("#employeeid").append("<option value="+employeeid+">"+ employeename +"</option>");
$("#employeename").val(employeename);
});
}
});
}
JSP
<td height="25" bgcolor="#EEEEEE" width="10%">
<div align="center" class="STYLE1">
<div align="center">合同签订部门</div>
</div>
</td>
<td height="25" bgcolor="#FFFFFF" width="10%">
<div align="center" class="STYLE1">
<div align="center">
<select name="deparementid" id="deparementid" οnchange="getEmp()" style="width:60%; margin:auto; outline:none;">
<option value="">请选择</option>
<c:forEach items="${deparementList}" var="p" varStatus="i">
<option value="${p.id}">${p.name}</option>
</c:forEach>
</select>
</div>
</div>
</td>
<td height="25" bgcolor="#EEEEEE" width="10%">
<div align="center" class="STYLE1">
<div align="center">合同签订人</div>
</div>
</td>
<td height="25" bgcolor="#FFFFFF" width="10%">
<div align="center" class="STYLE1">
<div align="center">
<select name="employeeid" id="employeeid" style="width:60%; margin:auto; outline:none;">
<option value="">---请选择---</option>
</select>
</div>
</div>
</td>
controller层
private Long deparementids;// 获取部门id,根据部门id,二级菜单联动出部门下的员工
private HttpServletResponse response;// 设置xml格式
public Long getDeparementids() {
return deparementids;
}
public void setDeparementids(Long deparementids) {
this.deparementids = deparementids;
}
public HttpServletResponse getResponse() {
return response;
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
/**
* 部门员工二级菜单联动,根据传来的部门id查找出部门下的员工信息,把这个员工结果集返回到添加页面
*
* @throws Exception
*/
public void departmentuser() throws Exception {
List<User> getPersonnel = userService.getBydeparementId(deparementids);
PrintWriter out = null;
response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");// 指定返回值的编码方式,必须放在out声明之前
try {
out = response.getWriter();
response.setContentType("text/xml");
out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.print("<results>");
for (User bean : getPersonnel) {
out.print("<result>");
out.print("<employeeid>");
out.print(bean.getId());
out.print("</employeeid>");
out.print("<employeename>");
out.print(bean.getName());
out.print("</employeename>");
out.print("</result>");
}
out.print("</results>");
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
out.close();
}
}