The simple implementation case of Ajax cascading dropdown box

Source: Internet
Author: User
Tags rtrim trim

  This article is mainly on the Ajax cascading dropdown box Simple implementation of the case is introduced, the need for friends can come to the reference, I hope to help you.

Java class     code required are as follows: 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{       elementElement= (Element) iterator.next ();       productclass product=new Productclass ( Element.getattributevalue ("id"),                                       element.getattributevalue ("ClassName");            Products.add (product);     }while (Iterator.hasnext ());  & nbsp   return (productclass[]) Products.toarray (new productclass[0]);       }      & Nbsp;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")) {  &NBSp     classelement=element;        break;       }     }WHI Le (Iterator.hasnext ());        if (classelement!=null) {       iterator iter= Classelement.getchildren (). Iterator ();       do{        Element element= ( Element) Iter.next ();        Productclass product=new Productclass (element.getattributevalue ("id" ),                                  & nbsp     Element.getattributevalue ("ClassName");                  Products.add (product);       }while (Iter.hasnext ());      return (productclass[)) Products.toarray (new productclass[0]);     }      else{       return N ull;     }    &NBSP;}&NBsp         public productclass[] Getallclass3byid (String class1id,string class2id) {    Collection products = new ArrayList ();    element class1element = null;    Element class2element = nul l;      Iterator iterator = This.dom.getRootElement (). GetChildren (). Iterator ();    do {  & nbsp  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 = (Ele ment) Iter.next ();      if (Class2id.equalsignorecase (Element.getattributevalue ("id")) {       class2element = element;    &NBSP;  break;     }     }while (Iter.hasnext ());       if ( Class2element!=null) {      Iterator iter2 = Class2element.getchildren () iterator ();      Do {       element Element = (Element) Iter2.next ();       productclass product = n EW Productclass (element.getattributevalue ("id"), Element.getattributevalue ("ClassName"));       products.add (product);     }while (Iter2.hasnext ());     }      Return (productclass[]) Products.toarray (new productclass[0]);   }    else return null; }  }      <?xml version= "1.0" encoding= "UTF-8"?>  <! DOCTYPE class SYSTEM "Product.dtd" >  <class>    <class1 classname= "Computer Accessories" id= "1" >  & nbsp   &NBSP;&LT;CLASS2 classname= "Memory" id= "1" >        &NBSP;&LT;CLASS3 id= "1"Classname= "Kingmax" ></class3>        &NBSP;&LT;CLASS3 id= "2" Classname= "Kingston" > </class3>        &NBSP;&LT;CLASS3 id= "3" classname= "Samsung" ></class3>        &NBSP;&LT;CLASS3 id= "4" classname= "Hydadi" ></class3>          &LT;CLASS3 id= "5" classname= "IBM" ></class3>        </class2>      & Nbsp;<class2 classname= "Hard Drive" id= "2" >        &NBSP;&LT;CLASS3 id= "6" classname= "hithait" > </class3>        &NBSP;&LT;CLASS3 id= "7" classname= "IBM" ></class3>        &NBSP;&LT;CLASS3 id= "8" classname= "Samsung" ></class3>         < CLASS3 id= "9" classname= "Westdata" ></class3>       </class2>    </ class1>      &LT;CLASS1 classname= "Food Accessories" id= "2" >      &NBSP;&LT;CLASS2 classname= "Hamburger" id= "1" >        &NBSP;&LT;CLASS3 id= "1" classname= "McDonald's" & gt;</class3>        &NBSP;&LT;CLASS3 id= "2" classname= "Ken Base" ></class3>        &NBSP;&LT;CLASS3 id= "3" classname= "Roger Silk" ></class3>       </class2>       &NBSP;&LT;CLASS2 classname= "Drink" id= "2" >        &NBSP;&LT;CLASS3 id= "4" classn Ame= "CocaCola" ></class3>        &NBSP;&LT;CLASS3 id= "5" classname= "Sprite" ></ class3>        &NBSP;&LT;CLASS3 id= "6" classname= "Coffee" ></class3>        &NBSP;&LT;CLASS3 id= "7" classname= "Water" ></class3>       </class2>& nbsp   </class1>  </class>      <?xml version= "1.0" encoding= "GB2312"?>  <! ELEMENT Class (class1+) >  <! ELEMENT Class1 (CLAss2+) >  <! attlist Class1 className nmtoken #REQUIRED >  <! attlist Class1 ID nmtoken #REQUIRED >  <! ELEMENT Class2 (class3+) >  <! Attlist class2 className nmtoken #REQUIRED >  <! attlist class2 ID nmtoken #REQUIRED >  <! ELEMENT CLASS3 empty>  <! Attlist class3 className nmtoken #REQUIRED >  <! Attlist class3 ID nmtoken #REQUIRED >    jsp:  (1) Getclass.jsp Act as a business layer for AJAX calls   <%@ page Contenttyp E= "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) {&NBsp   for (int i=0;i<classes.length;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< classes.length;i++) {     out.print (Classes[i].getid () + "," +classes[i].getclassname () + "|");    } } } %>    (2) divmenu.jsp  <%@ page contenttype= "text/html; charset=gb2312 "%>  <%@ page import=" com.ajaxlab.ajax.* "%>  <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > 
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.