论坛首页 编程语言技术论坛

flex和java交互实现实现无限接树形结构

浏览 3877 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-07-30  

数据库表(Category)的设计为:

id       parentId     type       name       description
13         null       search    最新舆情      描述
1          13         search    区域          描述
4          13         search    机构部门      描述
2          1          search    第一区域      描述
5          1          search    第二区域      描述

id        为主键
parentId  为外键

 

java中利用递归的做法把数据库中的记录拼成xml格式,在拼xml时用的是jdom
public String findSubCategoyies(final Category category, final String type, final boolean recursive) {
  final List subCategoyies = new ArrayList();
  return (String)this.getHibernateTemplate().execute(new HibernateCallback() {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
    Element rootElement = new Element("menus");//xml中的根元素
    rootElement.setAttribute("id", category.getId()+"");//设置属性
    rootElement.setAttribute("label", category.getName());//设置属性
    findDiGuiCategories(session, category, type, recursive, subCategoyies, rootElement);//递归的调用
                                //组合xml
    OutputStream os = null;
    String xmlStr = "";
    try {
     os = new ByteArrayOutputStream();
     Format format = Format.getPrettyFormat();
     format.setEncoding("gb2312");//设置编码
     Document doc = new Document(rootElement);
     XMLOutputter xop = new XMLOutputter(format);
     xop.output(doc, os);
     xmlStr = os.toString();
     System.out.println(xmlStr);
     os.close();
     
    } catch (IOException e) {
     e.printStackTrace();
    }    
    return xmlStr;
   }
  });
  
 } 
 
  //循环调用熟悉java的朋友都知道下面的写法是Hibernate的HQL语句。
 public List findDiGuiCategories(Session session, Category category,
   String type, boolean recursive, List categories, Element rootElement) {
  if (session == null)
   return null;
  Query query = null;
  String hql = "from Category c where 1=1";
  if (category == null) {
   hql += " and c.parent is null";
  } else {
   hql += " and c.parent = " + category.getId().longValue() + "";
  }

  if (type == null) {
   hql += " and c.type is null";
  } else {
   hql += " and c.type = '" + type + "'";
  }

  hql += " order by c.name asc";

  query = session.createQuery(hql);

  List directSubDirs = query.list();
  if (Utilities.isNullOrEmpty(directSubDirs))
   return null;
  if (recursive) {
   Iterator it = directSubDirs.iterator();
   while (it.hasNext()) {
    Category subCategory = (Category) it.next();
    categories.add(subCategory);
    Element childElement = new Element("node");
    childElement.setAttribute("id", subCategory.getId()+"");
    childElement.setAttribute("label", subCategory.getName());
    rootElement.addContent(childElement);
    findDiGuiCategories(session, subCategory, type, true,
      categories, childElement);
   }
  } else {
   categories.addAll(directSubDirs);
  }
  return categories;
 }


Flex中as类的写法:
 //通过URLLoader来和java交互
 public function newConsensus():void {
  var url:String = "http://localhost:8080/FlexWebCrawl/categoryAction!tree.do";
  var request:URLRequest = new URLRequest(url);
  var loader:URLLoader   = new URLLoader();
  request.method = "post";
  loader.load(request);
  loader.addEventListener(Event.COMPLETE, resultNewConsensus);
   
 }
  
 public function resultNewConsensus(event:Event):void {
  menus = XML(event.target.data);
             var results:XMLList = menus.node;//获得接点
            this.objMainApp.myTree.dataProvider = results;  
   
 }

 

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics