JSP中使用EasyUI實現非同步載入tree(整合Struts 2)

來源:互聯網
上載者:User

首先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;   // 節點id      private 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="gif/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; // 樹組件使用的ID            public 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否則open                  if(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的方法進行了封裝

更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/webkf/ajax/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.