public class TreeNode implements java.io.Serializable {
private Integer id;
private String text;//
private String iconCls;//
private Boolean checked = false;//
private Map<String, Object> attributes;//
private List<TreeNode> children;//
private String state = "open";// (open,closed)
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Boolean getChecked() {
return checked;
}
public void setChecked(Boolean checked) {
this.checked = checked;
}
public Map<String, Object> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
public List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getIconCls() {
return iconCls;
}
public void setIconCls(String iconCls) {
this.iconCls = iconCls;
}
}
public class Menu {
private Integer id;
private String name;
private String ciconcls;
private String url;
private Set<Menu> menus;
private Menu menu;
/**
* @return the ciconcls
*/
public String getCiconcls() {
return ciconcls;
}
/**
* @param ciconcls the ciconcls to set
*/
public void setCiconcls(String ciconcls) {
this.ciconcls = ciconcls;
}
/**
* @return the menus
*/
public Set<Menu> getMenus() {
return menus;
}
/**
* @param menus the menus to set
*/
public void setMenus(Set<Menu> menus) {
this.menus = menus;
}
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
/**
* @return the menu
*/
public Menu getMenu() {
return menu;
}
/**
* @param menu the menu to set
*/
public void setMenu(Menu menu) {
this.menu = menu;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<class name="com.acca.entity.Menu" table="t_menu">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="ciconcls"/>
<property name="url"/>
<set name="menus" cascade="all" inverse="true">
<key>
<column name="pid"></column>
</key>
<one-to-many class="com.acca.entity.Menu"/>
</set>
<many-to-one name="menu" class="com.acca.entity.Menu" column="pid"></many-to-one>
</class>
</hibernate-mapping>
private List jsonList = new ArrayList();//action
/**
*
*
* @return
* @throws IOException
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public String makeMenuTree() throws IOException {
jsonList=menuService.tree(menu, true);
return SUCCESS;
}
<action name="makeMenuTree" class="menuAction" method="makeMenuTree">
<result type="json">
<param name="root">jsonList</param>
</result>
</action>
public List<TreeNode> tree(Menu menu, Boolean b) {
List<Object> param = new ArrayList<Object>();
String hql = "from Menu m where m.menu is null";
if (menu != null && menu.getId() != null) {
hql = "from Menu m where m.menu.id = ?";
param.add(menu.getId());
}
List<Menu> l = menuDao.find(hql, param);
List<TreeNode> tree = new ArrayList<TreeNode>();
for (Menu t : l) {
tree.add(tree1(t, b));
}
return tree;
}
/**
*
* @param t
* @param recursive
* @return
*/
@SuppressWarnings("unused")
private TreeNode tree1(Menu t, boolean recursive) {
TreeNode node = new TreeNode();
node.setId(t.getId());
node.setText(t.getName());
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put("url", t.getUrl());
node.setAttributes(attributes);
if (t.getCiconcls() != null) {
node.setIconCls(t.getCiconcls());
} else {
node.setIconCls("");
}
if (t.getMenus() != null && t.getMenus().size() > 0) {
node.setState("closed");
if (recursive) {//
List<Menu> l = new ArrayList<Menu>(t.getMenus());
//Collections.sort(l, new MenuComparator());//
List<TreeNode> children = new ArrayList<TreeNode>();
for (Menu r : l) {
TreeNode tn = tree1(r, true);
children.add(tn);
}
node.setChildren(children);
}
}
return node;
}
<script type="text/javascript" src="js/timer.js">
$().ready(function() {
$('#tree').tree({
url:'makeMenuTree.action' ,
lines : true,
onClick : function (node) {
if (node.attributes) {
Open(node.text, node.attributes.url);
}
}
});
</script>
<ul id="tree" class="easyui-tree"></ul>