首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

递归展示无限级 导航菜单

2012-09-23 
递归显示无限级 导航菜单菜单的实体package com.fenjin.bank.modelimport java.util.HashSetimport java

递归显示无限级 导航菜单

菜单的实体 

 

 

package com.fenjin.bank.model;

import java.util.HashSet;
import java.util.Set;

/**
 * Menu entity. @author MyEclipse Persistence Tools
 */

public class Menu implements java.io.Serializable {

 // Fields

 /**
  *
  */
 private static final long serialVersionUID = -6930624483390675234L;
 private Integer id;
 private Menu menu;
 private String name;
 private Integer sortId;
 private String urlPath;
 public String getUrlPath() {
  return urlPath;
 }

 public void setUrlPath(String urlPath) {
  this.urlPath = urlPath;
 }

 private Set<Menu> menus = new HashSet<Menu>(0);

 // Constructors

 /** default constructor */
 public Menu() {
 }

 /** full constructor */
 public Menu(Menu menu, String name, Set<Menu> menus,int sortId) {
  this.menu = menu;
  this.name = name;
  this.menus = menus;
  this.sortId=sortId;
 }

 // Property accessors

 public Integer getSortId() {
  return sortId;
 }

 public void setSortId(Integer sortId) {
  this.sortId = sortId;
 }

 public Integer getId() {
  return this.id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 public Menu getMenu() {
  return this.menu;
 }

 public void setMenu(Menu menu) {
  this.menu = menu;
 }

 public String getName() {
  return this.name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public Set<Menu> getMenus() {
  return this.menus;
 }

 public void setMenus(Set<Menu> menus) {
  this.menus = menus;
 }

}

菜单的hbm文件 

 

 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.fenjin.bank.model.Menu" table="menu" catalog="bank">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <many-to-one name="menu" class="com.fenjin.bank.model.Menu" fetch="select">
            <column name="pid" />
        </many-to-one>
        <property name="urlPath" type="java.lang.String">
            <column name="urlPath" length="1000" />
        </property>
       
          <property name="name" type="java.lang.String">
            <column name="name" length="400" />
        </property>
          <property name="sortId" type="java.lang.Integer">
            <column name="sortId" />
        </property>
       
        <set name="menus" inverse="true">
            <key>
                <column name="pid" />
            </key>
            <one-to-many class="com.fenjin.bank.model.Menu" />
        </set>
    </class>
</hibernate-mapping>

action层部门代码

 

public void  getMenuHtml(int id,int level){
  
  StringBuffer strPre = new StringBuffer("");
  for(int i=0; i<level; i++) {
   strPre.append("----");
  }
  menuList=service.findByPid(id);
  for(Menu m:menuList){
   sb.append("<li> <a href='#'>");
   sb.append(m.getName()+m.getId());
   sb.append("</a>"+"\n");
   
   System.out.println(strPre+m.getName()+m.getId());
   if(service.findByPid(m.getId()).size()>0){
    sb.append("<ul>"+"\n");
    getMenuHtml(m.getId(),level+1);
    sb.append("</ul>"+"\n");
   }
  }
 }

 

public String execute() throws Exception {
  
  sb.append("<head>"
 + "<script type='text/javascript' src='jss/jquery.js'></script>"
 + "<script type='text/javascript' src='jss/jquery.dimensions.js'></script>"
 + "<script type='text/javascript' src='jss/jquery.positionBy.js'></script>"
 + "<script type='text/javascript' src='jss/jquery.jdMenu.js'></script>"
 + "<link rel='stylesheet' href='css/jquery.jdMenu.css' type='text/css' />"
 +   "</head>"
 +  "<body>");
   
  sb.append("<div style='border: 1px solid blue; '> <ul class='jd_menu jd_menu_slate'>"+"\n");
  getMenuHtml(0,0);
  sb.append("</ul> </div>");
  sb.append("</body> </html>");
  System.out.println(sb.toString());
  response.getWriter().print(sb.toString());
  return null;
 }

前台 ajax 加载

 <script type="text/javascript">
 $(document).ready(function() {
 $("#lanmu-body").load("listMenu");
});
</script>

 

热点排行