多级下拉菜单的级连显示问题。
在这里,我为了实现公司-部门级连显示问题,我编写了leader_add_competence_dialogquery.jsp ,web.xml,SelectCropDepartServlet.java 类。
其中注意的是leader_add_competence_dialogquery中的.jspChange_Select() javascript函数中的/selectCropDepart是在web.xml中定义的servlet类。我们在实现应用当中需要修改的用红字标出。
其中leader_add_competence_dialogquery.jsp代码如下:
<%@ page contentType="text/html; charset=gbk" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="pub.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>添加领导权限</title>
<link href="../css/putong.css" type="text/css" rel="stylesheet">
<base target="_self">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
</head>
<script language="javascript">
var req;
window.onload=function(){
}
function Change_Select(){
var zhi = document.getElementById('Corp').value;
var url = "../selectCropDepart?id=" + escape(zhi);
//alert(url);
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET", url, true);
req.onreadystatechange = callback;
req.send(null);
}
}
function callback() {
if (req.readyState == 4) {
if (req.status == 200) {
parseMessage();
// update the HTML DOM based on whether or not message is valid
}else{
alert ("Not able to retrieve description" +req.status+req.statusText);
}
}
}
function parseMessage() {
var xmlDoc = req.responseXML.documentElement;
var xSel = xmlDoc.getElementsByTagName('select');
var select_root = document.getElementById('Depart');
select_root.options.length=0;
for(var i=0;i<xSel.length;i++)
{
var xValue = xSel[i].childNodes[0].firstChild.nodeValue;
var xText = xSel[i].childNodes[1].firstChild.nodeValue;
var option = new Option(xText,xValue);
try
{
select_root.add(option);
}
catch(e){
}
}
}
function AddLeaderCompetence(){
var key=document.all.HiddenKey.value; document.from1.action="leader_ctl.jsp?Oper=addCompetence&Key="+key;
document.from1.submit();
}
</script>
<body>
<%
String strUserID=request.getParameter("Key");
session.setAttribute("EmployUserID",strUserID);
%>
<form name="from1" action="leader_ctl.jsp" method="post">
<table cellSpacing="0" cellPadding="0" width="100%" bgColor="#bfcae6" border="0" background="../images/subtitle.gif">
<tr>
<td width="99%" height="26"><FONT face="宋体">
<DIV align="left"><FONT face="宋体" color="#ffffff"> 当前位置:系统管理>>领导设置>>添加领导权限 </FONT>
</DIV>
</FONT>
</td>
</tr>
</table>
<input type="hidden" name="HiddenKey" value="<%=strUserID%>" >
<table width="100%" border="0" cellpadding="2" cellspacing="1" bgcolor="#AABDE0">
<tr bgcolor="#FFFFFF">
<td align="right">公司:</td>
<td>
<SELECT name="Corp" id="Corp" onChange="Change_Select()" >
<OPTION value="all" selected>所有公司</OPTION></SELECT></td>
</tr>
<tr bgcolor="#FFFFFF">
<td align="right">部门:</td>
<td><SELECT name="Depart" id="Depart">
<OPTION value="all" selected>所有部门</OPTION></SELECT></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" align="center"><input type="button" onClick="AddLeaderCompetence();" class="button" style="width:80px" value="保存"> <input type="button" class="button" value="关闭" onClick="window.close();" style="width:80px"> </td>
</tr>
</table>
</form>
</body>
<%
Conn conn=new Conn();
String m1Script="";
//String m2Script="";
m1Script=PubClass.BindAllCorpList("document.all.Corp");
//m2Script=PubClass.BindAllDepartList("document.all.Depart",""+session.getAttribute("cropid"));
//sPositionList=conn.bindDropListSel("document.all.PositionName","select * from sys08_Position","PositionName","PositionID");
%>
<%= m1Script%>
</html>
Web.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>selectCropDepart</servlet-name>
<servlet-class>pub.SelectCropDepartServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>selectCropDepart</servlet-name>
<url-pattern>/selectCropDepart</url-pattern>
</servlet-mapping>
</web-app>
SelectCropDepartServlet.java Servlet类代码如下
/**
* 从sys04_Machine表中取数据进行二级下拉菜单(公司,部门)及关联的servlet类
* @author 邓浩
*/
public class SelectCropDepartServlet extends HttpServlet {
public SelectCropDepartServlet() {
super();
}
public void destroy() {
super.destroy(); }
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control", "no-cache");
String targetId = request.getParameter("id").toString();
String xml_start ="<selects>";
String xml_end = "</selects>";
String xml = "";
try{
Conn conn=new Conn();
DataTable dt=new DataTable();
String Sql="select * from sys04_Machine where cropid="+targetId;
////System.out.println("Sql:"+Sql);
xml+= "<select><value>all</value><text>所有部门</text></select>" ;
int i=0;
if (conn.querySql(Sql,dt)){
while (i<dt.getRowCount()){
xml+= "<select><value>"+dt.getItemForName(i,"MachID").toString()+"</value><text>"+dt.getItemForName(i,"MachName").toString()+"</text></select>";
i=i+1;
}
}
String last_xml = xml_start + xml + xml_end;
//System.out.println("last_xml:"+last_xml);
// last_xml = new String(last_xml.getBytes("UTF-8"), "GB2312");
response.getWriter().write(last_xml);
}// try
catch (Exception e){
System.out.print(e.getMessage());
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doGet(request,response);
}
public void init() throws ServletException {
}
}
2006-08-18