`
左博涯
  • 浏览: 59185 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

修改struts2-showcase动态树,改由从数据表中读取,并且用递归方式生成

阅读更多

系统采用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

 

 

 

 

分享到:
评论
1 楼 clwbeyond 2009-12-25  
你好,我将你的代码重新配置了一下,但是能跑起来了,主要有一个很奇怪的问题:
public Category getTreeRootNode() {
System.out.println("开始递归!");
Tree tree = treeDao.findById(0);
return treeService.createTree(tree);
}

这样子的话,提交一次:http://localhost:8080/InfoSystem/tree/showDynamicTreeAction.action
而上面的这代码执行两次,而树却没有显示出来。
如果用这代码:
public Category getTreeRootNode() {
System.out.println("开始递归!");
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
treeService = (TreeService)factory.getBean("treeService");
treeDao = (TreeDAO)factory.getBean("treeDao");
Tree tree = treeDao.findById(0);
return treeService.createTree(tree);
}
提交一次:http://localhost:8080/InfoSystem/tree/showDynamicTreeAction.action
而上面的上面的代码只执行一次,效果也如博主所说的一样。

请问博主,这是为什么呢?能不能将你的代码给我发一下clwbeyond@163.com,谢谢了。

相关推荐

Global site tag (gtag.js) - Google Analytics