Java Ajax implementation cascade drop-down box

Source: Internet
Author: User
Tags rtrim

CopyCode The Code is 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 = 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 = This. Dom. getrootelement (). getchildren (). iterator ();
Do ...{
Element element = (element) iterator. Next ();
If (class1id. inclusignorecase (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 = This. Dom. getrootelement (). getchildren (). iterator ();
Do ...{
Element element = (element) iterator. Next ();
If (class1id. inclusignorecase (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. inclusignorecase (element. getattributevalue ("ID ")))...{
Class2element = element;
Break;
}
} While (ITER. hasnext ());

If (class2element! = NULL )... {
iterator iter2 = class2element. getchildren (). iterator ();
do... {
element = (element) iter2.next ();
productclass Product = new productclass (element. getattributevalue ("ID"), element. getattributevalue ("classname");
products. add (product);
}while (iter2.hasnext ();
}< br> return (productclass []) products. toarray (New productclass [0]);
}< br> else return NULL;
}< BR >}









































JSP:
(1) getclass. JSP acts as the business layer for Ajax calls
<% @ 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 () + "|");
}< BR >}< br>
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 () + "|");
}< BR >%>

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




Function dochange ()...{
VaR F = Document. Forms [0];
Send_request ("get", "getclass. jsp? Class1id = "+ F. select11.value +" & class2id = ", null," text ", populateclass2 );
}
Function dochange2 ()...{
VaR F = Document. Forms [0];
Send_request ("get", "getclass. jsp? Class1id = "+ F. select11.value +" & class2id = "+ F. select12.value, null," text ", populateclass3 );
}
Function populateclass2 ()...{
VaR F = Document. Forms [0];
If (http_request.readystate = 4 )...{
If (http_request.status = 200 )...{
VaR list = http_request.responsetext;
VaR classlist = List. Split ("| ");
F. select12.options. Length = 1;
For (VAR I = 0; I <classlist. Length-1; I ++ )...{
VaR temp = trim (classlist [I]). Split (",");
F. select12.add (New Option (temp [1], temp [0]);
}
}
}
}

Function populateclass3 ()...{
VaR F = Document. Forms [0];
If (http_request.readystate = 4 )...{
If (http_request.status = 200 )...{
VaR list = http_request.responsetext;
VaR classlist = List. Split ("| ");
F. select13.options. Length = 1;
For (VAR I = 0; I <classlist. Length-1; I ++ )...{
VaR temp = trim (classlist [I]). Split (",");
F. select13.add (New Option (temp [1], temp [0]);
}
}
}
}
Function ltrim (STR)
...{
VaR whitespace = new string ("");
VaR S = new string (STR );
If (whitespace. indexof (S. charat (0 ))! =-1)
...{
VaR J = 0, I = S. length;
While (j <I & whitespace. indexof (S. charat (j ))! =-1)
...{
J ++;
}
S = S. substring (J, I );
}
Return S;
}
Function rtrim (STR)
...{
VaR whitespace = new string ("");
VaR S = new string (STR );
If (whitespace. indexof (S. charat (S. Length-1 ))! =-1)
...{
VaR I = S. Length-1;
While (I> = 0 & whitespace. indexof (S. charat (I ))! =-1)
...{
I --;
}
S = S. substring (0, I + 1 );
}
Return S;
}
Function trim (STR)
...{
Return rtrim (ltrim (STR ));
}
</SCRIPT>
<%
Classservice service = new classservice ();
Productclass [] classes = service. getallclass1 ();
%>
<Meta http-equiv = "Content-Type" content = "text/html; charset = ISO-8859-1">
<Title> insert title here </title>
</Head>
<Body> <center>
<Form name = "classform" method = "Post" Action = "">
<Select name = "select11" id = "select11" onchange = "dochange (this. Value)">
<Option value = ""> select category 1 </option>
<%
For (INT I = 0; I <classes. length; I ++ )...{
Out. println ("<option value = '" + classes [I]. GETID () + "'>" + classes [I]. getclassname () + "</option> ");
}
%>
</SELECT>

<Select name = "select12" id = "select12" onchange = "dochange2 (this. Value)">
<Option value = ""> select category 2 </option>
</SELECT>

<Select name = "select13" id = "select13">
<Option value = ""> select category 3 </option>
</SELECT>
</Form>
</Center> </body>
</Html>

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.