/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.gcg.struts.action; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.MappingDispatchAction; import com.gcg.Ajaxtree; import com.gcg.service.Ajax; import com.gcg.struts.form.TreeForm; /** * MyEclipse Struts * Creation date: 04-01-2010 * * XDoclet definition: * @struts.action path="/tree" name="treeForm" input="/tree/treeadd.jsp" scope="request" validate="true" * @struts.action-forward name="success" path="/index.jsp" */ public class TreeAction extends MappingDispatchAction { /* * Generated Methods */ /** * Method execute * @param mapping * @param form * @param request * @param response * @return ActionForward */ private Ajax ajax; public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { TreeForm treeForm = (TreeForm) form; return null; } public ActionForward ajax(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Long parentid = Long.parseLong(request.getParameter("id")); Long level = Long.parseLong(request.getParameter("level").trim()); if(level==2L){ List<Map<Object,Object>> ls = ajax.getLevel2(parentid); response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); PrintWriter out=response.getWriter(); out.print(parseTreeInfoToXM(ls ,parentid ,level).toString()); out.flush(); out.close(); }else if(level == 3L){ List<Map<Object,Object>> ls = ajax.getLevel3(parentid); response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); PrintWriter out=response.getWriter(); out.print(parseTreeInfoToXM(ls ,parentid ,level).toString()); out.flush(); out.close(); }else{ System.out.println(level); throw new IllegalArgumentException("参数level非法"); } return null; } public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException { List<Map<Object,Object>> ls = ajax.getLevel1(); response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); PrintWriter out=response.getWriter(); StringBuffer sb = parseTreeInfoToXM(ls ,0L ,0L); System.out.println(sb); out.print(sb); out.flush(); out.close(); return null; } public StringBuffer parseTreeInfoToXM(List ls ,Long parentid , Long level){ StringBuffer sb = new StringBuffer(); sb.append("<?xml version='1.0' encoding='UTF-8'?>"); sb.append("<trees>"); if(parentid != 0){ sb.append("<parentid>" + parentid + "</parentid>"); } if(level != 0 ){ sb.append("<level>" + level + "</level>"); } for(int i=0 ;i<ls.size(); ++i){ sb.append("<tree>"); Map<Object,Object> map = (Map)ls.get(i); Boolean isHaveChild = (Boolean)map.get("isHaveChild"); if(isHaveChild == true){ sb.append("<isHaveChild>true</isHaveChild>"); }else{ sb.append("<isHaveChild>false</isHaveChild>"); } Ajaxtree ajaxtree = (Ajaxtree)map.get("ajaxtree"); Long id = ajaxtree.getId(); sb.append("<id>" + id + "</id>"); String name = ajaxtree.getName(); sb.append("<name>" + name + "</name>"); sb.append("</tree>"); } sb.append("</trees>"); return sb; } public Ajax getAjax() { return ajax; } public void setAjax(Ajax ajax) { this.ajax = ajax; } }
package com.gcg.serviceimpl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.gcg.Ajaxtree; import com.gcg.AjaxtreeDAO; import com.gcg.service.Ajax; public class AjaxTreeService implements Ajax { private AjaxtreeDAO ajxtreeDAO; // public void addTreeNode(Ajaxtree ajaxtree) throws RuntimeException { // // TODO Auto-generated method stub // try { // ajxtreeDAO.save(ajaxtree); // } catch (RuntimeException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // throw e; // } // // } // // public void deleteTreeNod(Long id) throws RuntimeException { // // TODO Auto-generated method stub // try { // ajxtreeDAO.deleteByID(id); // } catch (RuntimeException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // throw e; // } // // } // // public List<Map<Object, Object>> getAjaxResult(Long id) throws RuntimeException { // // TODO Auto-generated method stub // List<Map<Object, Object>> childAllInfo = new ArrayList(); // try { // // List<Object> childInfo = ajxtreeDAO.getChildrens(id); // // for(int i=0 ;i<childInfo.size() ;++i ){ // Map<Object,Object> childSumInfo = new HashMap(); // Ajaxtree ajaxtree = (Ajaxtree)childInfo.get(i); // Long childId = ajaxtree.getId(); // childSumInfo.put(i, ajaxtree); // // boolean isHaveChild = ajxtreeDAO.isHaveChild(childId); // childSumInfo.put("isHaveChild", isHaveChild); // childAllInfo.add(childSumInfo); // } // } catch (RuntimeException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // throw e; // } // return childAllInfo; // } // // public List<Object> getPidNullInfo() throws RuntimeException { // // TODO Auto-generated method stub // List<Object> ls = null; // try { // ls = ajxtreeDAO.getPidNullIInfo(); // } catch (RuntimeException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // throw e; // } // return ls; // } // public List<Map<Object, Object>> getAjaxResultPIDisNull() // throws RuntimeException { // List<Map<Object, Object>> childAllInfo = null; // try { // childAllInfo = new ArrayList(); // // List<Object> childInfo = ajxtreeDAO.getPidNullIInfo(); // for(int i=0 ;i<childInfo.size() ;++i ){ // Map<Object,Object> childSumInfo = new HashMap(); // Ajaxtree ajaxtree = (Ajaxtree)childInfo.get(i); // Long childId = ajaxtree.getId(); // childSumInfo.put("ajaxtree", ajaxtree); // // boolean isHaveChild = ajxtreeDAO.isHaveChild(childId); // childSumInfo.put("isHaveChild", isHaveChild); // childAllInfo.add(childSumInfo); // } // } catch (RuntimeException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // throw e; // } // return childAllInfo; // } public List getLevel1() throws RuntimeException { List<Map<Object, Object>> childAllInfo = null; try { childAllInfo = new ArrayList(); List<Ajaxtree> childInfo = ajxtreeDAO.getLeve1(); for(int i=0 ;i<childInfo.size() ;++i ){ Map<Object,Object> childSumInfo = new HashMap(); Ajaxtree ajaxtree = (Ajaxtree)childInfo.get(i); Long childId = ajaxtree.getId(); childSumInfo.put("ajaxtree", ajaxtree); boolean isHaveChild = ajxtreeDAO.isHaveChild(childId); childSumInfo.put("isHaveChild", isHaveChild); childAllInfo.add(childSumInfo); } } catch (RuntimeException e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } return childAllInfo; } public List getLevel2(Long id) throws RuntimeException { List<Map<Object, Object>> childAllInfo = null; try { childAllInfo = new ArrayList(); List<Ajaxtree> childInfo = ajxtreeDAO.getLeve2(id); for(int i=0 ;i<childInfo.size() ;++i ){ Map<Object,Object> childSumInfo = new HashMap(); Ajaxtree ajaxtree = (Ajaxtree)childInfo.get(i); Long childId = ajaxtree.getId(); childSumInfo.put("ajaxtree", ajaxtree); boolean isHaveChild = ajxtreeDAO.isHaveChild(childId); childSumInfo.put("isHaveChild", isHaveChild); childAllInfo.add(childSumInfo); } } catch (RuntimeException e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } return childAllInfo; } public List getLevel3(Long id) throws RuntimeException { List<Map<Object, Object>> childAllInfo = null; try { childAllInfo = new ArrayList(); List<Ajaxtree> childInfo = ajxtreeDAO.getLeve3(id); for(int i=0 ;i<childInfo.size() ;++i ){ Map<Object,Object> childSumInfo = new HashMap(); Ajaxtree ajaxtree = (Ajaxtree)childInfo.get(i); Long childId = ajaxtree.getId(); childSumInfo.put("ajaxtree", ajaxtree); boolean isHaveChild = ajxtreeDAO.isHaveChild(childId); childSumInfo.put("isHaveChild", isHaveChild); childAllInfo.add(childSumInfo); } } catch (RuntimeException e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } return childAllInfo; } public AjaxtreeDAO getAjxtreeDAO() { return ajxtreeDAO; } public void setAjxtreeDAO(AjaxtreeDAO ajxtreeDAO) { this.ajxtreeDAO = ajxtreeDAO; } }
package com.gcg; import java.util.HashSet; import java.util.Set; /** * Ajaxtree entity. * * @author MyEclipse Persistence Tools */ public class Ajaxtree implements java.io.Serializable { // Fields private Long id; private Ajaxtree ajaxtree; private String name; private Set ajaxtrees = new HashSet(0); // Constructors /** default constructor */ public Ajaxtree() { } /** minimal constructor */ public Ajaxtree(String name) { this.name = name; } /** full constructor */ public Ajaxtree(Ajaxtree ajaxtree, String name, Set ajaxtrees) { this.ajaxtree = ajaxtree; this.name = name; this.ajaxtrees = ajaxtrees; } // Property accessors public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public Ajaxtree getAjaxtree() { return this.ajaxtree; } public void setAjaxtree(Ajaxtree ajaxtree) { this.ajaxtree = ajaxtree; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Set getAjaxtrees() { return this.ajaxtrees; } public void setAjaxtrees(Set ajaxtrees) { this.ajaxtrees = ajaxtrees; } <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.gcg.Ajaxtree" table="AJAXTREE" schema="SYSTEM"> <id name="id" type="java.lang.Long"> <column name="ID" precision="10" scale="0" /> <generator class="sequence" /> </id> <many-to-one name="ajaxtree" class="com.gcg.Ajaxtree" fetch="join"> <column name="PID" precision="10" scale="0" /> </many-to-one> <property name="name" type="java.lang.String"> <column name="NAME" length="50" not-null="true" /> </property> <set name="ajaxtrees" inverse="true" lazy="true"> <key> <column name="PID" precision="10" scale="0" /> </key> <one-to-many class="com.gcg.Ajaxtree" /> </set> </class> </hibernate-mapping> package com.gcg; import java.util.List; import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.LockMode; import org.springframework.context.ApplicationContext; import org.springframework.dao.DataAccessException; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; /** * A data access object (DAO) providing persistence and search support for * Ajaxtree entities. Transaction control of the save(), update() and delete() * operations can directly support Spring container-managed transactions or they * can be augmented to handle user-managed Spring transactions. Each of these * methods provides additional information for how to configure it for the * desired type of transaction control. * * @see com.gcg.Ajaxtree * @author MyEclipse Persistence Tools */ public class AjaxtreeDAO extends HibernateDaoSupport { private static final Log log = LogFactory.getLog(AjaxtreeDAO.class); // property constants public static final String NAME = "name"; protected void initDao() { // do nothing } public void save(Ajaxtree transientInstance) { log.debug("saving Ajaxtree instance"); try { getHibernateTemplate().save(transientInstance); log.debug("save successful"); } catch (RuntimeException re) { log.error("save failed", re); throw re; } } public Boolean isHaveChild(Long id){ List<Integer> ls = null; try { String query = "select count(*) from Ajaxtree a inner join a.ajaxtrees c where c.ajaxtree.id = ? "; ls = getHibernateTemplate().find(query ,id ); } catch (DataAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } return ls.size() > 0; } public List<Object> getChildrens(Long id){ List<Object> ls = null; try { String query = "select a.ajaxtrees from Ajaxtree a where a.id = ? "; ls = getHibernateTemplate().find(query , id); } catch (DataAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } return ls; } public List<Ajaxtree> getLeve1(){ List<Ajaxtree> ls = null; try { String query = " from Ajaxtree a where a.ajaxtree =null "; ls = getHibernateTemplate().find(query); } catch (DataAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } return ls; } public List<Ajaxtree> getLeve2(Long id){ List<Ajaxtree> ls = null; try { String query = "select a.ajaxtrees from Ajaxtree a where a.ajaxtree =null and a.id=?"; ls = getHibernateTemplate().find(query, id); } catch (DataAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } return ls; } public List<Ajaxtree> getLeve3(Long id){ List<Ajaxtree> ls = null; try { String query = "select a.ajaxtrees from Ajaxtree a where a.ajaxtree !=null and a.id=?"; ls = getHibernateTemplate().find(query, id); } catch (DataAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } return ls; } public List<Object> getPidNullIInfo(){ List<Object> ls = null; try { String query = "select a.ajaxtrees from Ajaxtree a where a.ajaxtree=null "; ls = getHibernateTemplate().find(query); } catch (DataAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } return ls; } public void delete(Ajaxtree persistentInstance) { log.debug("deleting Ajaxtree instance"); try { getHibernateTemplate().delete(persistentInstance); log.debug("delete successful"); } catch (RuntimeException re) { log.error("delete failed", re); throw re; } } public void deleteByID(Long ID) { log.debug("deleting Ajaxtree instance"); try { String query = "delete from Ajaxtree a where a.id = ? "; getHibernateTemplate().bulkUpdate(query, ID); log.debug("delete successful"); } catch (RuntimeException re) { log.error("delete failed", re); throw re; } } public Ajaxtree findById(java.lang.Long id) { log.debug("getting Ajaxtree instance with id: " + id); try { Ajaxtree instance = (Ajaxtree) getHibernateTemplate().get( "com.gcg.Ajaxtree", id); return instance; } catch (RuntimeException re) { log.error("get failed", re); throw re; } } public List findByExample(Ajaxtree instance) { log.debug("finding Ajaxtree instance by example"); try { List results = getHibernateTemplate().findByExample(instance); log.debug("find by example successful, result size: " + results.size()); return results; } catch (RuntimeException re) { log.error("find by example failed", re); throw re; } } public List findByProperty(String propertyName, Object value) { log.debug("finding Ajaxtree instance with property: " + propertyName + ", value: " + value); try { String queryString = "from Ajaxtree as model where model." + propertyName + "= ?"; return getHibernateTemplate().find(queryString, value); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } public List findByName(Object name) { return findByProperty(NAME, name); } public List findAll() { log.debug("finding all Ajaxtree instances"); try { String queryString = "from Ajaxtree"; return getHibernateTemplate().find(queryString); } catch (RuntimeException re) { log.error("find all failed", re); throw re; } } public Ajaxtree merge(Ajaxtree detachedInstance) { log.debug("merging Ajaxtree instance"); try { Ajaxtree result = (Ajaxtree) getHibernateTemplate().merge( detachedInstance); log.debug("merge successful"); return result; } catch (RuntimeException re) { log.error("merge failed", re); throw re; } } public void attachDirty(Ajaxtree instance) { log.debug("attaching dirty Ajaxtree instance"); try { getHibernateTemplate().saveOrUpdate(instance); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public void attachClean(Ajaxtree instance) { log.debug("attaching clean Ajaxtree instance"); try { getHibernateTemplate().lock(instance, LockMode.NONE); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public static AjaxtreeDAO getFromApplicationContext(ApplicationContext ctx) { return (AjaxtreeDAO) ctx.getBean("AjaxtreeDAO"); } } }
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript" language="JavaScript"> var xmlrequest; function createXMLHttpRequest() { if(window.XMLHttpRequest) { //Mozilla 浏览器 xmlrequest = new XMLHttpRequest(); } else if (window.ActiveXObject) { // IE浏览器 try { xmlrequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlrequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } } } function initNav() { createXMLHttpRequest(); var uri = "${pageContext.request.contextPath}/ajaxInitTree.do"; xmlrequest.open("POST", uri, true); xmlrequest.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded"); xmlrequest.onreadystatechange = processResponse; xmlrequest.send(null); } function processResponse(){ if (xmlrequest.readyState == 4) { // 判断对象状态 if (xmlrequest.status == 200) { // 信息已经成功返回,开始处理信息 var doc = xmlrequest.responseXML; var trees = doc.getElementsByTagName("tree"); // alert(trees.length); for(var i=0;i<trees.length;++i){ var ishavechild = trees.item(i).getElementsByTagName('isHaveChild').item(0).firstChild.nodeValue; var id = trees.item(i).getElementsByTagName('id').item(0).firstChild.nodeValue; var name = trees.item(i).getElementsByTagName('name').item(0).firstChild.nodeValue; var parentid = 0; var level = 1; createTree(parentid ,ishavechild , id , name ,level); } } else { //页面不正常 window.alert("您所请求的页面有异常。"); } } } function createTree(parentid ,ishavechild , id ,name ,level){ if(parentid == 0){ var level = level + 1; var vdiv = document.createElement("div"); vdiv.setAttribute("id",id); if(ishavechild == 'true'){ vdiv.innerHTML ="<div id="+'node'+id+''+level+''+">"+ "<a href="+'javascript:expandtree('+id+','+level+');>' + "<img id="+'img'+id +' '+level+ " src='img/plus.gif'>" + "</a>"+ "<img id="+'folder'+id+' '+level+" src='img/folder.gif'>"+ "<a href="+'javascript:expandtree('+id+','+level+');>'+name+"</a>"+ "</div>"; }else{ vdiv.innerHTML ="<div class='node'>"+ "<img src='img/folder.gif' id='folder'"+id+">"+ "<a href='#'>"+name+"</a>"+ "</div>"; } document.getElementById("nav").appendChild(vdiv); }else{ var parentDiv = document.getElementById('node'+parentid+''+level); var level =parseInt(level) + 1; alert(parentDiv.innerHTML); var vdiv = document.createElement("div"); vdiv.setAttribute("id",id); if(ishavechild == 'true'){ vdiv.innerHTML ="<div id="+'node'+id+''+level+''+">"+ "<a href="+'javascript:expandtree('+id+','+level+');>' + "<img id="+'img'+id +' '+level+ " src='img/plus.gif'>" + "</a>"+ "<img id="+'folder'+id+' '+level+" src='img/folder.gif'>"+ "<a href="+'javascript:expandtree('+id+','+level+');>'+name+"</a>"+ "</div>"; }else{ vdiv.innerHTML ="<div class='node'>"+ "<img src='img/folder.gif' id='folder'"+id+">"+ "<a href='#'>"+name+"</a>"+ "</div>"; } parentDiv.appendChild(vdiv); } } function expandtree(parentid ,level){ createXMLHttpRequest(); var uri = "${pageContext.request.contextPath}/ajaxAppendTree.do?id="+parentid+"&level="+level; xmlrequest.open("POST", uri, true); xmlrequest.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded"); xmlrequest.onreadystatechange = processExpandtree; xmlrequest.send(null); } function processExpandtree(){ if (xmlrequest.readyState == 4) { // 判断对象状态 if (xmlrequest.status == 200) { // 信息已经成功返回,开始处理信息 var doc = xmlrequest.responseXML; var trees = doc.getElementsByTagName("tree"); var parentid = doc.getElementsByTagName("parentid").item(0).firstChild.nodeValue; var level = doc.getElementsByTagName("level").item(0).firstChild.nodeValue // alert(trees.length); for(var i=0;i<trees.length;++i){ var ishavechild = trees.item(i).getElementsByTagName('isHaveChild').item(0).firstChild.nodeValue; var id = trees.item(i).getElementsByTagName('id').item(0).firstChild.nodeValue; var name = trees.item(i).getElementsByTagName('name').item(0).firstChild.nodeValue; createTree(parentid ,ishavechild , id ,name ,level); } } else { //页面不正常 window.alert("您所请求的页面有异常。"); } } } </script> <title>AJAX 树</title> </head> <body onload="initNav()"> <div id="nav"> </div> </body> </html>