Struts2中关于s:tree
Struts2中可以通过s:tree标签轻松的实现一个树状结构。 下面是一个s:tree的具体实现的例子。数的结构用mysql数据库类维护。 1.创建数据库表 DROP TABLE IF EXISTS CATEGORY_MASTER ; CREATE TABLE CATEGORY_MASTER( CATEGORY_ID int auto_increment NOT NULL, CATEGORY_NAME varchar(50) NOT NULL, PARENT_CATEGORY_ID int DEFAULT -1 NOT NULL, PRIMARY KEY (CATEGORY_ID) )
2.创建数据库连接类 参看 Struts2中DB操作的一个例子(1) 3.创建Category.java,用来保存DB数据和页面提交的数据 package s2.ex.data; public class Category { private int id; private String name; private Category[] children; private int parentNodeId; public Category[] getChildren() { return children; } public void setChildren(Category[] children) { this.children = children; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getParentNodeId() { return parentNodeId; } public void setParentNodeId(int parentNodeId) { this.parentNodeId = parentNodeId; } }
4.创建对数据库表category_master操作的类 package s2.ex.svc; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import s2.ex.data.Category; import s2.ex.db.DBConn; public class CategoryService { private static CategoryService instance; private CategoryService() { } public static CategoryService getInstance() { if (instance == null){ instance = new CategoryService(); } return instance; } public Category[] getCategoryList() { Connection conn = DBConn.getConnection(); final String SQL = "SELECT * FROM category_master"; ResultSet rs = null; try { rs = conn.createStatement().executeQuery(SQL); ArrayList
上面getChildren方法里面用到了递归
9.通过 http://localhost:8080/s2/test/DisplayTree.action来测试,结果如下 展开后 节点追加 点击一个节点时
5.创建action类 package s2.ex.action; import s2.ex.data.Category; import s2.ex.svc.CategoryService; import com.opensymphony.xwork2.ActionSupport; public class SimpleTreeAction extends ActionSupport {private static final long serialVersionUID = 1L; private Category root; private Category[] parentNodeIds; private Category editCategory; public String displayTree() { root = new Category(); root.setId(-1); root.setName("root"); root.setChildren(CategoryService.getInstance().getAllCategory()); return SUCCESS; } public Category getRoot() { return root; } public void setRoot(Category root) { this.root = root; } public String goTreeEdit(){ this.parentNodeIds = CategoryService.getInstance().getCategoryList(); return SUCCESS; } public Category[] getParentNodeIds() { return parentNodeIds; } public void setParentNodeIds(Category[] parentNodeIds) { this.parentNodeIds = parentNodeIds; } public Category getEditCategory() { return editCategory; } public void setEditCategory(Category editCategory) { this.editCategory = editCategory; } public String saveTreeNode() { if (CategoryService.getInstance().addNewTreeNode(this.editCategory)){ return SUCCESS; }else { return INPUT; } } }
6.创建TreeIndex.jsp,用来显示树 【Struts2中关于s:tree】
8.配置struts.xml
推荐阅读
- 热闹中的孤独
- Shell-Bash变量与运算符
- JS中的各种宽高度定义及其应用
- 2021-02-17|2021-02-17 小儿按摩膻中穴-舒缓咳嗽
- 深入理解Go之generate
- 异地恋中,逐渐适应一个人到底意味着什么()
- 我眼中的佛系经纪人
- 《魔法科高中的劣等生》第26卷(Invasion篇)发售
- “成长”读书社群招募
- 2020-04-07vue中Axios的封装和API接口的管理