Welcome

首页 / 脚本样式 / Ajax / JSP中使用EasyUI实现异步加载tree(整合Struts 2)

JSP中使用EasyUI实现异步加载tree(整合Struts 2)2015-01-26首先jsp页面有一ul用于展现Tree

<ul id="mytree"></ul>

加载Tree

<script type="text/javascript">$("#mytree").tree({ url:"treeLoad.action"}); </script>
配置Action

<struts><package name="tree_json" extends="json-default"><action name="treeLoad" method="treeLoad" class="com.home.web.TreeAction"><result type="json"><param name="root">treeNodes</param></result></action></package></struts>
注意:

1.extends是json-default,表示返回json对象格式。

2.result中param的name为root,里面设置的值就是action中要返回的JSON对象

需要封装对象Tree

public class TreeNode {private static final long serialVersionUID = 1L;private String id; // 节点idprivate String text; // 显示的节点文本private String state = "open"; // 节点状态,"open"或者"closed",默认是"open"private boolean checked; // 指明检查节点是否选中.public TreeNode() {}public TreeNode(String id, String text, String state, boolean checked) {this.id = id;this.text = text;this.state = state;this.checked = checked;}//...省略setXX() getXX()}
表结构如图

<img width="" height="" " src="http://img.ddvip.com/2014/0905/201409051228502161.gif"/>

首先查询所有parentid为空值的数据,然后同时判断该节点下是否有子节点,如果有则状态是关闭状态

当继续展开树的时候 会将该ID值传入,然后查询该节点的子节点。

action方法实现

import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import com.home.util.ConnectionManager;/*** 查询数据使用JDBC进行操作 **/public class TreeAction {private List<TreeNode> treeNodes = new ArrayList<TreeNode>(); //返回的JSON数据private String id; // 树组件使用的IDpublic String treeLoad() {Statement sta = null;ResultSet rs = null;try {Connection conn = ConnectionManager.getConnection();sta = conn.createStatement();String sql = "";if (id == null) {//如果id为null则是根节点 sql = "select * from easyui_tree where parentid = """;} else { //查询下面的子节点 sql = "select * from easyui_tree where parentid = " + id;}rs = sta.executeQuery(sql);while (rs.next()) {String id = rs.getString("id");String name = rs.getString("name");TreeNode node = new TreeNode();node.setId(id);node.setText(name);node.setChecked(false);//判断是否有子节点,如果有则closed否则openif(isChildrenNode(id)){node.setState("closed");}else{node.setState("open");}treeNodes.add(node);}// 关闭所有资源ConnectionManager.closeAll(rs, sta, conn);} catch (SQLException e) {e.printStackTrace();}return "success";}/*** 判断是否有子节点* * @return*/public boolean isChildrenNode(String id) {Boolean flag = false;Statement sta = null;ResultSet rs = null;try {Connection conn = ConnectionManager.getConnection();sta = conn.createStatement();String sql = "select * from easyui_tree where parentid = " + id;rs = sta.executeQuery(sql);while (rs.next()) {flag = true;}// 关闭所有资源ConnectionManager.closeAll(rs, sta, conn);} catch (SQLException e) {e.printStackTrace();}return flag;}public List<TreeNode> getTreeNodes() {return treeNodes;}public void setTreeNodes(List<TreeNode> treeNodes) {this.treeNodes = treeNodes;}public String getId() {return id;}public void setId(String id) {this.id = id;}}
对获取Connection的方法进行了封装