asp.net|菜單|問題|下拉|顯示
多級下拉式功能表的級連顯示問題。
在這裡,我為了實現公司-部門級連顯示問題,我編寫了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" >
<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" class="button" style="width:80px" value="儲存"> <input type="button" class="button" value="關閉" 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 {
}
}