AJAX级联下拉框的实现方法-创新互联
这篇文章主要介绍“AJAX级联下拉框的实现方法”,在日常操作中,相信很多人在AJAX级联下拉框的实现方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”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
}
}
}
else if((class1Id!=null)&&(class1Id!=null)) {
ProductClass[] classes = service.getAllClass3ById(class1Id,class2Id);
if(classes!=null) {
for(int i=0;i
}
}
}
%>
(2)divmenu.jsp
<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="com.ajaxlab.ajax.*"%>
<%
ClassService service = new ClassService();
ProductClass[] classes = service.getAllClass1();
%>
到此,关于“AJAX级联下拉框的实现方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
文章题目:AJAX级联下拉框的实现方法-创新互联
转载注明:http://hbruida.cn/article/csdpih.html