系统采用Struts2+Spring+Hibernate架构,classpath分层结构如下图:
表结构如下图:
表映射为pojo为类Tree.java代码如下:
private Integer id;
private String name;
private String children;
private Tree parent;
public Tree() {
}
public Tree(Integer id, String name, String children) {
this.id = id;
this.name = name;
this.children=children;
}
public Tree(Integer id, String name, String children, Tree parent) {
this.id = id;
this.name = name;
this.children = children;
this.parent = parent;
}
//省略get,set }
Hibernate映射文件tree.hbm.xml
<hibernate-mapping> <class name="zoboya.Tree" table="tree" > <id name="id" type="java.lang.Integer"> <generator class="native" /> </id> <property name="name" type="string"> <column name="name" length="255" /> </property> <property name="children" type="string"> <column name="children" length="255" /> </property> <many-to-one name="parent" class="zoboya.Tree" fetch="select"> <column name="parentid" not-null="true" /> </many-to-one> </class> </hibernate-mapping>
TreeDao.java代码如下:
public class TreeDAO extends HibernateDaoSupport implements ITreeDAO {
private static final Log log = LogFactory.getLog(TreeDAO.class);
public Tree findById(java.lang.Integer id) {
log.debug("getting Tree instance with id: " + id);
try {
Tree instance = (Tree) getSession().get("zoboya.Tree", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
//其它方法省略
}
节点类,Category
public class Category
{
//保存了所有节点数据的Map
private static Map<Long, Category> catMap = new HashMap<Long, Category>();
//用作节点的id
private long id;
//用作节点的标题
private String name;
//包含子节点对象的列表
private List<Category> children;
public static Category getById(long id)
{
return catMap.get(id);
}
public Category(long id, String name,List<Category> children){
this.id=id;
this.name=name;
this.children=children;
catMap.put(id, this);
}
public Category(long id, String name, Category... children)
{
this.id = id;
this.name = name;
//初始化Category对象的children属性,并放入子对象
this.children = new ArrayList<Category>();
for (Category child : children)
{
this.children.add(child);
}
catMap.put(id, this);
}
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public List<Category> getChildren()
{
return children;
}
public void setChildren(List<Category> children)
{
this.children = children;
}
}
Service层的类TreeService
public class TreeService implements ITreeService {
private ITreeDAO treeDao;
public ITreeDAO getTreeDao() {
return treeDao;
}
public void setTreeDao(ITreeDAO treeDao) {
this.treeDao = treeDao;
}
//递归方法
public Category createTree(Tree tree){
System.out.println("Name= "+tree.getName());
//如果有儿子就循环调用自己去找,把找到的所有儿子生成一个list作为children加入到父中
List<Category> children=new ArrayList<Category>();
if (tree.getChildren()!=null) {
String[] childrenId=tree.getChildren().split(",");
for (int i = 0; i < childrenId.length; i++) {
//递归
Tree branch=treeDao.findById(Integer.parseInt(childrenId[i]));
Category child = createTree(branch);
children.add(child);
}
}
Category me=new Category(tree.getId(),tree.getName(), children);
return me;
}
}
Action
public class ShowDynamicTreeAction extends ActionSupport
{
private static final long serialVersionUID = 7437613038859411044L;
private ITreeService treeService;
private Category category;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Override
public String execute() throws Exception {
this.category =this.getTreeRootNode();
return SUCCESS;
}
public void setTreeService(ITreeService treeService) {
this.treeService = treeService;
}
public Category getTreeRootNode()
{ Tree tree=new Tree(0,"ZH根目录","1,5"); //选择构造一个根目录,其子目录用逗号隔开!
System.out.println("开始递归!");
return treeService.createTree(tree);
}
}
在applicationContext.xml文件中加入
<!-- DAO --> <bean id="treeDao" class="zoboya.struts2.dao.TreeDAO"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- Services --> <bean id="treeService" class="zoboya.struts2.service.TreeService"> <property name="treeDao"> <ref bean="treeDao"/> </property> </bean> <!-- Action --> <bean id="treeAction" class="zoboya.struts2.action.ShowDynamicTreeAction" singleton="false"> <property name="treeService"> <ref bean="treeService"/> </property> </bean>
在struts.xml中加入
<action name="showDynamicTreeAction" class="treeAction"> <result>/ajaxTags/treeTag/treeExampleDynamic.jsp</result> </action>
部署后访问以下地址就可以看到动态树了
http://localhost:8080/项目名/ajaxTags/treeTag/showDynamicTreeAction.action