用ajax技術讀取數據庫內容實現二級聯動下拉選擇菜單


—————————————————————這是ajax(javascript)代碼———————————————————————————

function send_request(callback, urladdress, isReturnData){      
        var xmlhttp = getXMLHttpRequest();
        xmlhttp.onreadystatechange = function(){
            	if (xmlhttp.readyState == 4) {//readystate 爲4即數據傳輸結束
 				    try{
				    	if(xmlhttp.status == 200){
							if(isReturnData && isReturnData==true){
								callback(xmlhttp.responseText);
							}
						}else{
							callback("抱歉,沒找到此頁面:"+ urladdress +"");
						}
			        } catch(e){
			        	callback("抱歉,發送請求失敗,請重試 " + e);
			        }
			   }
        }
        xmlhttp.open("POST", urladdress, true);
        xmlhttp.send(null);
}

function getXMLHttpRequest() {
        var xmlhttp;
		if (window.XMLHttpRequest) {
			try {
				xmlhttp = new XMLHttpRequest();
				xmlhttp.overrideMimeType("text/html;charset=UTF-8");//設定以UTF-8編碼識別數據
			} catch (e) {}
		} else if (window.ActiveXObject) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				try {
					xmlhttp = new ActiveXObject("Msxml2.XMLHttp");
				} catch (e) {
					try {
						xmlhttp = new ActiveXObject("Msxml3.XMLHttp");
					} catch (e) {}
				}
			}
		}
        return xmlhttp;
}
————————————————————————這是客戶端表單jsp代碼——————————————————————————————
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><script type="text/javascript" src="<%=request.getContextPath()%>/js/xmlhttp.js"></script><script type="text/javascript">function getProcess(value) {var process = document.getElementById('process');send_request(function(value2) {process.innerHTML = value2;}, "getProcessType.action?value="+value, true);}</script></head><body><div><form action="" method="post" name="form"><select name="process" onchange="getProcess(this.value)"><option value="0" selected>請選擇流程種類</option><option value="Y">業務流程</option><option value="G">管理流程</option><option value="Z">支持流程</option></select><div id="process"><select name="smallclass"><option value="一級流程名稱" selected>請選擇一級流程名稱</OPTION></select></div><input type="submit" value="提交"></form></div></body></html>


————————————————————————這是服務端action代碼——————————————————————————————


這裏是我的業務邏輯,每個邏輯不同,所以這段代碼這只是爲了把想要顯示的內容放在request範圍內,然後return到下一個頁面.javabean中有我的MyProcess類和它的屬性

public String getProcessType(){
		HttpServletRequest request=ServletActionContext.getRequest();
		String value=request.getParameter("value");
		List<MyProcess> MyProcesses= processService.getProcessByType(value);
		for(MyProcess p:MyProcesses){
			System.out.println(p.getName());
		}
		request.setAttribute("list",MyProcesses);
		return SUCCESS;
	}


————————————————————————這是服務端jsp代碼——————————————————————————————
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/page/share/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

</head>
<body>
<select name="smallclass">
<option value="一級流程名稱" selected>請選擇一級流程名稱</OPTION>
     <c:forEach items="${list}" var="myprocess">
     <option value="${myprocess.processID}" > 
        ${myprocess.name}
     </option>
     </c:forEach>
</select>
</body>
</html>


這個過程差不多就這些!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章