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.创建数据库连接类
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<Category> aryResult = new ArrayList<Category>(); while (rs.next()) { Category category = new Category(); category.setId(rs.getInt("CATEGORY_ID")); category.setName(rs.getString("CATEGORY_NAME")); aryResult.add(category); } Category[] result = new Category[aryResult.size()]; result = aryResult.toArray(result); return result; } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null){ rs.close(); } if (conn != null) { if (!conn.isClosed()) { conn.close(); } } } catch (SQLException e) { e.printStackTrace(); } } return null; } public boolean addNewTreeNode(Category category) { Connection conn = DBConn.getConnection(); final String SQL = "INSERT INTO category_master(CATEGORY_NAME,PARENT_CATEGORY_ID) VALUES(?,?)"; PreparedStatement pre = null; try { pre = conn.prepareStatement(SQL); pre.setString(1, category.getName()); pre.setInt(2, category.getParentNodeId()); int result = pre.executeUpdate(); if (result >= 0) { return true; } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (pre != null){ pre.close(); } if (conn != null) { if (!conn.isClosed()) { conn.close(); } } } catch (SQLException e) { e.printStackTrace(); } } return false; } public Category[] getAllCategory() { Connection conn = DBConn.getConnection(); final String SQL = "SELECT * FROM category_master where PARENT_CATEGORY_ID=-1"; ResultSet rs = null; try { rs = conn.createStatement().executeQuery(SQL); ArrayList<Category> aryResult = new ArrayList<Category>(); while (rs.next()) { Category category = new Category(); category.setId(rs.getInt("CATEGORY_ID")); category.setName(rs.getString("CATEGORY_NAME")); category.setChildren(this.getChildren(category.getId())); aryResult.add(category); } Category[] result = new Category[aryResult.size()]; result = aryResult.toArray(result); return result; } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null){ rs.close(); } if (conn != null) { if (!conn.isClosed()) { conn.close(); } } } catch (SQLException e) { e.printStackTrace(); } } return null; } public Category[] getChildren(int categoryId) { Connection conn = DBConn.getConnection(); final String SQL = "SELECT * FROM category_master where PARENT_CATEGORY_ID=?"; PreparedStatement pre = null; ResultSet rs = null; try { pre = conn.prepareStatement(SQL); pre.setInt(1, categoryId); rs = pre.executeQuery(); ArrayList<Category> aryResult = new ArrayList<Category>(); while (rs.next()) { Category category = new Category(); category.setId(rs.getInt("CATEGORY_ID")); category.setName(rs.getString("CATEGORY_NAME")); category.setChildren(this.getChildren(category.getId())); aryResult.add(category); } Category[] result = new Category[aryResult.size()]; result = aryResult.toArray(result); return result; } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null){ rs.close(); } if (pre != null){ pre.close(); } if (conn != null) { if (!conn.isClosed()) { conn.close(); } } } catch (SQLException e) { e.printStackTrace(); } } return null; }
上面getChildren方法里面用到了递归
<package name="Test" extends="struts-default" namespace="/test"> <action name="DisplayTree" class="s2.ex.action.SimpleTreeAction" method="displayTree"> <result>TreeIndex.jsp</result> </action> <action name="GoTreeEdit" class="s2.ex.action.SimpleTreeAction" method="goTreeEdit"> <result>TreeEdit.jsp</result> </action> <action name="SaveTreeNode" class="s2.ex.action.SimpleTreeAction" method="saveTreeNode"> <result name="input">TreeEdit.jsp</result> <result type="redirect">DisplayTree.action</result> </action> </package>
展开后
节点追加
点击一个节点时
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,用来显示树
<%@ page contentType="text/html; charset=UTF-8" %> <%@page pageEncoding="utf-8" %> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Tree</title> <s:head theme="ajax" debug="true" /> </head><body> <script language="JavaScript"> function treeNodeSelected(arg) { alert("id["+arg.source.widgetId+"], name["+ arg.source.title+ "] selected"); } dojo.addOnLoad(function() { var s = dojo.widget.byId('treeTestId').selector; dojo.event.connect(s, 'select', 'treeNodeSelected'); });</script> <div style="float:left; margin-right: 50px;"> <s:tree id="treeTestId" theme="ajax" rootNode="root" childCollectionProperty="children" nodeIdProperty="id" nodeTitleProperty="name" treeSelectedTopic="treeSelected"> </s:tree> <s:form action="GoTreeEdit"> <s:submit value="Add New"/> </s:form> </div></body> </html>7.创建用来增加节点的TreeEdit.jsp<%@ page contentType="text/html; charset=UTF-8" %> <%@page pageEncoding="utf-8" %> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Session Test</title> <s:head theme="xhtml"/> </head><body> <s:form action="SaveTreeNode"> <s:textfield label="Name" name="editCategory.name"/> <s:select label="Parent Node" name="editCategory.parentNodeId" headerKey="-1" headerValue="Select Parent Node" list="parentNodeIds" listKey="id" listValue="name" required="false" value="editCategory.parentNodeId"/> <s:submit/> </s:form> </div></body>
8.配置struts.xml
分享到:
相关推荐
struts2 <s:if>标签使用struts2 <s:if>标签使用struts2 <s:if>标签使用struts2 <s:if>标签使用struts2 <s:if>标签使用struts2 <s:if>标签使用
Struts2 Design and Programming: A Tutorial.pdf 是一本介绍Struts2的设计和编程思想的书,也可以作为一本手册来使用
NULL 博文链接:https://shupili141005.iteye.com/blog/667446
struts2.0中s:datetimepicker标签使用范例
针对Struts1的网页版的Struts Design and Programming A Tutorial
Struts 2 技术详解:基于WebWork核心的MVC开发与实践
Struts2_02CURD : 关于Struts2的增、删、改和查 实际业务中数据来自数据库,从DAO层查询,本实例使用静态资源的方式模拟, 主要是关于CURD的操作方式。 Struts2_03Taglib : Struts2常用标签的使用方法 Struts2...
Struts2讲义-作者:吴峻申
Struts2第一篇:流程篇
struts2环境搭建+struts2 新闻发布系统+struts2 留言板 struts2环境搭建:基础框架搭建,简单易上手,...struts2 留言板:struts2+jquery 直接导入myecplise中即可使用,很好用,可以在此基础上添加并完善项目将更完美。
Struts2 Spring2.5集成:系统登陆demo--带lib可直接运行
Struts2漏洞检查工具2019版 警告: 本工具为漏洞自查工具,请勿非法攻击他人网站! ==漏洞编号==============影响版本=========================官方公告==========================================影响范围====...
包含struts2-core-2.5.10.1.jar,struts2-jfreechart-plugin-2.5.10.1.jar,struts2-json-plugin-2.5.10.1.jar,struts2-junit-plugin-2.5.10.1.jar,struts2-bean-validation-plugin-2.5.10.1.jar,struts2-cdi-...
在action里面写了方法获取一个list,再将这个list的值放到jsp页面中显示出来,用的是<s:iterator>标签。
struts2框架的标签库分为三类: 用户标签:生成HTML元素 非用户标签:数据访问、逻辑控制等。 AJAX标签:支持AJAX技术。 用户界面标签又分两类: 表单标签:生成HTML页面的from元素 非表单标签:生成页面上的...
同样重要的是,《Struts2技术内幕:深入解析Struts2架构设计与实现原理》还深入挖掘并分析了Struts2源代码实现中蕴含的大量值得称道的编程技巧和设计模式,这对开发者从Struts2的设计原理上去掌握和悟透Web层开发的...
NULL 博文链接:https://zxymicky.iteye.com/blog/1536476
struts 2 s标签 a标签创建一个HTML超链接,等价于HTML 的 示范代码: <s:a href="login.action">登陆</s:a>