在网上看到很多关于extjs树的文章,他们说的都是如何还回json格式,但是并没有涉及到后台数据库表的结构,本文简单介绍下从表的设计到前台的展示。
sql:
create table tree(
id int primary key,
parent_id int not null,
code varchar(50)
)
insert into tree values(1,0,'根节点');
insert into tree values(2,1,'一级菜单');
insert into tree values(3,1,'一级菜单');
insert into tree values(4,2,'二级菜单');
insert into tree values(5,2,'二级菜单');
insert into tree values(6,3,'二级菜单');
insert into tree values(7,4,'三级菜单');
insert into tree values(8,4,'三级菜单');
读取数据库表信息
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.wang.util.Menu;
public class AllBook {
public static final String URL = "jdbc:sqlserver://localhost:14333;DatabaseName=gm";
public static final String serName = "sa";
public static final String Pwd = "jinbingchuan";
public List<Tree> fillbook() {
List<Tree> bookList = new ArrayList<Tree>();
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
con = getConnection();
st = con.createStatement();
String sql = "select * from tree order by id desc"; // 查询数据SQL语句s
rs = st.executeQuery(sql); // 获取结果集
while (rs.next()) {
Tree tree = new Tree();
tree.setId(rs.getInt("id"));
tree.setCode(rs.getString("Code"));
tree.setParentId(rs.getInt("parent_id"));
bookList.add(tree);
}
} catch (Exception ex) {
ex.printStackTrace(); // 输出出错信息
}
finally{
try {
rs.close();
st.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 关闭连接
}
return bookList;
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(URL, serName, Pwd); // 得到连接
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public void recurison(List<Tree> books,Menu menu){
if(hasChildren(books,menu)){
menu.setLeaf(false);
menu.setId(menu.getId());
menu.setText(menu.getText());
List<Menu> children = getChildren(books,menu);
menu.setChildren(children);
for(Menu n:children){
recurison(books,n);
}
}
else{
menu.setLeaf(true);
menu.setId(menu.getId());
menu.setText(menu.getText());
}
}
public Menu getRoot(List<Tree> books){
for(Tree b:books){
if(b.getParentId().intValue() == 0){
Menu menu = new Menu();
menu.setId(b.getId());
menu.setText(b.getCode());
return menu;
}
}
return null;
}
public boolean hasChildren(List<Tree> books,Menu menu){
for(Tree b:books){
if(b.getParentId().intValue() == menu.getId().intValue())
return true;
}
return false;
}
public List<Menu> getChildren(List<Tree> books,Menu menus){
List<Menu> bookList = new ArrayList<Menu>();
for(Tree b:books){
if(b.getParentId().intValue() == menus.getId().intValue()){
Menu menu = new Menu();
menu.setId(b.getId());
menu.setText(b.getCode());
bookList.add(menu);
}
}
return bookList;
}
public Menu getAllBooks() {
List<Tree> books = fillbook();
Menu menu = getRoot(books);
recurison(books,menu);
return menu;
}
}
tree.java如下:
package dao;
public class Tree {
private Integer id;
private Integer parentId;
private String Code;
public Tree() {
super();
// TODO Auto-generated constructor stub
}
public Tree(Integer id, Integer parentId, String code) {
super();
this.id = id;
this.parentId = parentId;
Code = code;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public String getCode() {
return Code;
}
public void setCode(String code) {
Code = code;
}
@Override
public String toString() {
return "Book [BookName="
+ ", Code=" + Code + ", id=" + id + ", parentId=" + parentId
+ "]";
}
}
Menu.java:
import java.util.List;
public class Menu {
private String text;
private Integer id;
private String cls;
private boolean leaf;
private String href;
private List<Menu> children;
private String hrefTarget;
public String getHrefTarget() {
return hrefTarget;
}
public void setHrefTarget(String hrefTarget) {
this.hrefTarget = hrefTarget;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Integer getId() {
return id;
}
public void setId(Integer string) {
this.id = string;
}
public String getCls() {
return cls;
}
public void setCls(String cls) {
this.cls = cls;
}
public boolean isLeaf() {
return leaf;
}
public void setLeaf(boolean string) {
this.leaf = string;
}
public List<Menu> getChildren() {
return children;
}
public void setChildren(List<Menu> children) {
this.children = children;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
}
servlet:
package com.wang.servlet.ext;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import com.wang.util.Menu;
import dao.AllBook;
public class BookServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String menuString = null;
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
AllBook allBook = new AllBook();
Menu menu = allBook.getAllBooks();
PrintWriter out = response.getWriter();
JSONArray jsonObject = JSONArray.fromObject(menu);
try {
menuString = jsonObject.toString();
System.out.println("menuString=="+menuString);
} catch (Exception e) {
e.printStackTrace();
}
out.print(menuString);
}
}
前台页面显示
<script type="text/javascript">
Ext.onReady(function() {
var Tree = Ext.tree;
var tree = new Tree.TreePanel({
el:'tree-div',
autoScroll:true,
animate:true,
enableDD:true,
containerScroll: true,
loader: new Tree.TreeLoader({
dataUrl:'<%=path%>/BookServlet'
})
});
// set the root node
var root = new Tree.AsyncTreeNode({
text: 'Root',
draggable:false,
id:'source'
});
tree.setRootNode(root);
// render the tree
tree.render();
tree.expandAll();
});
</script>
</head>
<body>
<div id="tree-div"></div>
</body>
显示效果: