欢迎来到福编程网,本站提供各种互联网专业知识!

AJAX级联下拉框的简单实现案例

发布时间:2014-02-20 作者: 来源:转载
本篇文章主要是对AJAX级联下拉框的简单实现案例进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助

需要的JAVA类
复制代码 代码如下:
package com.ajaxlab.ajax;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import com.ajaxlab.ajax.ProductClass;

public class ClassService {
private Document dom;
public ClassService(){
try{
SAXBuilder builder=new SAXBuilder();
this.dom=builder.build(ClassService.class.getResource("product.xml"));
}catch(Exception e){
e.printStackTrace();
}
}
public ProductClass[] getAllClass1(){
Collection products=new ArrayList();
Iterator iterator=this.dom.getRootElement().getChildren().iterator();
do{
Element element=(Element)iterator.next();
ProductClass product=new ProductClass(element.getAttributeValue("id"),
element.getAttributeValue("className"));
products.add(product);
}while(iterator.hasNext());
return (ProductClass[])products.toArray(new ProductClass[0]);

}

public ProductClass[] getAllClass2ById(String class1Id){
Collection products=new ArrayList();
Element classElement=null;
Iterator iterator=this.dom.getRootElement().getChildren().iterator();
do{
Element element=(Element)iterator.next();
if(class1Id.equalsIgnoreCase(element.getAttributeValue("id"))){
classElement=element;
break;
}
}while(iterator.hasNext());

if(classElement!=null){
Iterator iter=classElement.getChildren().iterator();
do{
Element element=(Element)iter.next();
ProductClass product=new ProductClass(element.getAttributeValue("id"),
element.getAttributeValue("className"));
products.add(product);
}while(iter.hasNext());
return (ProductClass[])products.toArray(new ProductClass[0]);
}
else{
return null;
}
}


public ProductClass[] getAllClass3ById(String class1Id,String class2Id) {
Collection products = new ArrayList();
Element class1Element = null;
Element class2Element = null;

Iterator iterator = this.dom.getRootElement().getChildren().iterator();
do {
Element element = (Element)iterator.next();
if(class1Id.equalsIgnoreCase(element.getAttributeValue("id"))) {
class1Element = element;
break;
}
}while(iterator.hasNext());

if(class1Element!=null) {
Iterator iter = class1Element.getChildren().iterator();
do {
Element element = (Element)iter.next();
if(class2Id.equalsIgnoreCase(element.getAttributeValue("id"))) {
class2Element = element;
break;
}
}while(iter.hasNext());

if(class2Element!=null) {
Iterator iter2 = class2Element.getChildren().iterator();
do {
Element element = (Element)iter2.next();
ProductClass product = new ProductClass(element.getAttributeValue("id"),element.getAttributeValue("className"));
products.add(product);
}while(iter2.hasNext());
}
return (ProductClass[])products.toArray(new ProductClass[0]);
}
else return null;
}
}











































JSP:
(1)getClass.jsp 充当业务层供ajax调用
<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="com.ajaxlab.ajax.*"%>
<%
String class1Id = request.getParameter("class1Id");
String class2Id = request.getParameter("class2Id");
if("".equals(class1Id)) class1Id = null;
if("".equals(class2Id)) class2Id = null;
ClassService service = new ClassService();
if((class1Id!=null)&&(class2Id==null)) {
ProductClass[] classes = service.getAllClass2ById(class1Id);
if(classes!=null) {
for(int i=0;i out.print(classes[i].getId()+","+classes[i].getClassName()+"|");
}
}
}
else if((class1Id!=null)&&(class1Id!=null)) {
ProductClass[] classes = service.getAllClass3ById(class1Id,class2Id);
if(classes!=null) {
for(int i=0;i out.print(classes[i].getId()+","+classes[i].getClassName()+"|");
}
}
}
%>

(2)divmenu.jsp
<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="com.ajaxlab.ajax.*"%>





<%
ClassService service = new ClassService();
ProductClass[] classes = service.getAllClass1();
%>

Insert title here



<select name="select11" id="select11" onchange="doChange(this.value)">

<%
for(int i=0;i out.println("");
}
%>

<select name="select12" id="select12" onchange="doChange2(this.value)">

<select name="select13" id="select13">





相关推荐