struts2动态树的例子(转)
来源:互联网 发布:九阴真经女号捏脸数据 编辑:程序博客网 时间:2024/05/29 05:05
修改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 }
- 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>
- <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;
- }
- }
- //其它方法省略
- }
- 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;
- }
- }
- 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;
- }
- }
- 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);
- }
- }
- 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>
- <!-- 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>
- <action name="showDynamicTreeAction"
- class="treeAction">
- <result>/ajaxTags/treeTag/treeExampleDynamic.jsp</result>
- </action>
部署后访问以下地址就可以看到动态树了
- struts2动态树的例子(转)
- struts2动态树的例子(转)
- 第一个struts2的例子(转)
- struts2完整的例子(2)
- Struts2的简单例子
- 完整的struts2例子
- struts2的上传例子
- Struts2 Interceptor的例子
- Struts2学习(二)---一个struts2的登陆例子
- Struts2 + Spring 整合简单例子(转)
- [转]Struts2+hibernate例子
- 动态的小例子
- 动态Sql的例子
- 动态代理的例子
- 动态绑定的例子
- struts2.0简单的例子
- struts2.0的简单例子
- struts2.0简单的例子
- Hibernate缓存机制
- 【MariaDB】安装MariaDB,与MySQL并存
- 20个很有用的PHP类库
- 类目,延展,协议
- Ubuntu中获取ROOT权限的方法
- struts2动态树的例子(转)
- MySQL:按前缀批量删除表格
- 【Android】Handler应用(三):从服务器端分页加载更新ListView
- UTF-8使用纯真IP数据库乱码问题(已解决)
- 服务器迁移到虚拟机上
- 1790. Single Round Match
- 【IMPDP】ORA-00959: tablespace 'YOUFEE_LOB' does not exist
- Thinkpad SL400安装黑苹果10.8.4全纪录
- 图像处理经典图片Lena背后的故事