`
songjindian
  • 浏览: 73915 次
  • 性别: Icon_minigender_1
  • 来自: 河南
文章分类
社区版块
存档分类
最新评论

<s:select>无限树级菜单的实现

阅读更多

最近在做一个公安的项目,因为部门比较多,并且存在无限级的子部门,子子部门…………,所以在显示部门的时候要求以无限树级的形式来显示这个部门的信息。在数据库设计的时候有个部门表,部门表里面有个parentId的字段来说明部门和子部门的关系。需求摆在这,绞尽脑汁,终于想出来了使用递归的方式来实现了,下面是自己递归封装list的代码,与大家共享:

 public List<GroupInfo> listGroupInfo(int compyId,int type,User user){
   DBConn db = null;
		
 List<GroupInfo> groupInfos= new ArrayList<GroupInfo>();
		
  try{
       db = new DBConn();

         String   strSQL   =   "select * from groupInfo where compyId="+compyId+" and type="+type+" and ParentId=0";
		
       if(user.getGrade()>0){
			
			strSQL = strSQL+" and groupId="+user.getGroup().getGroupId()+"";
			
		}
		
		ResultSet rs = db.OpenSql(strSQL);
			String str = "—";
		 while(rs.next()){
			 
			 GroupInfo groupInfo = new GroupInfo();
			 
			 groupInfo.setGroupId(rs.getInt("groupId"));
			 groupInfo.setGroupName(str+rs.getString("groupName"));
			 groupInfo.setMemo(rs.getString("memo"));
			 groupInfo.setType(rs.getInt("type"));
			 groupInfos.add(groupInfo);
			 //递归调用
			 groupInfos.addAll(goback(compyId,type,rs.getInt("groupId"),db,str)) ;
			  
		 }
	    	  
	       logger.info("list groupInfo by compyId success!compyId="+compyId+",type="+type);

		} catch (Exception e) {

			logger.error("list groupInfo by compyId failed!compyId="+compyId+",type="+type,e);

			System.out.println(e);

		} finally {

			db.close();
		}

		return groupInfos;
		
	}
  public List<GroupInfo> goback(int companyId,int type,int groupId,DBConn db,String str){
		
		String   strSQL9  =  "select * from groupInfo where compyId="+companyId+" and type="+type+" and ParentId=?";
		List<GroupInfo> list = new ArrayList<GroupInfo>(); 
		PreparedStatement pst=null;
		
		try {
		
		pst = db.getConn().prepareStatement(strSQL9);
			
		pst.setInt(1, groupId);
		
		ResultSet rs8 = pst.executeQuery();
		while(rs8.next()){
			 
			GroupInfo groupInfo = new GroupInfo();
			 str = str + "—";
			 groupInfo.setGroupId(rs8.getInt("groupId"));
			 groupInfo.setGroupName(str+rs8.getString("groupName"));
			 groupInfo.setMemo(rs8.getString("memo"));
			 groupInfo.setType(rs8.getInt("type"));
			 list.add(groupInfo);
			 //递归调用
			 list.addAll(goback(companyId,type,rs8.getInt("groupId"),db,str));
			logger.info("list groupInfo by compyId success!递归成功!compyId="+companyId+",type="+type);
			 
		}
	} catch (SQLException e) {
		// TNODO Auto-generated catch block
		e.printStackTrace();
	}
	
	return list;
  }

前台显示的就是就是一个<s:select>标签。 

这里面就是使用了递归来实现的无限级的树形菜单。

分享到:
评论

相关推荐

    <S:select>用法

    Address private int aid; private String aname; public int getAid() { ... &lt;s:select name="address" headerKey="-1" headerValue="请选择" list="lis" listKey="aid" listValue="aname" /&gt; &lt;/s:form&gt;

    myeclipse2017 jsp页面 在select里面写 导致后面代码不是彩色问题

    myeclipse2017 jsp页面 &lt;c:if&gt;在select里面写 导致后面代码不是彩色问题 一半黑一半彩色

    <html:select>: 选择默认值

    &lt;html:select&gt;: 选择默认值

    jsp中htmlselect标签的用法

    html:select&gt;标签的用法 &lt;html:select property="theChoice"&gt; &lt;html:options collection="choiceLists" property="value" labelProperty="description" /&gt; &lt;/html:select&gt;

    [JavaScript]Javascript 设置与读取Co 三级select列表

    [JavaScript]Javascript 设置与读取Co 三级select列表。

    Struts2标签库的所有

    &lt;%@ taglib uri="/struts-tags" prefix="s" %&gt; 结合例子,重点掌握以下标签的用法 (1) &lt;s:property&gt; (2) &lt;s:set&gt; (3) &lt;s:if&gt; 、&lt;s:elseif&gt;、&lt;s:else&gt; ...(8) &lt;s:select&gt;、&lt;s:radio&gt;、&lt;s:checkboxlist&gt;

    struts2 标签库 帮助文档

    1. &lt;s:select list=""&gt;&lt;/s:select&gt;-----单选框 2. &lt;s:set name=""&gt;&lt;/s:set&gt;-----赋予变量一个特定范围内的值 3. &lt;s:sort comparator=""&gt;&lt;/s:sort&gt;-----通过属性给list分类 4. &lt;s:submit&gt;&lt;/s:submit&gt;-----提交按钮...

    Struts2标签库

    &lt;%@ taglib uri="/struts-tags" prefix="s" %&gt; 结合例子,重点掌握以下标签的用法 (1) &lt;s:property&gt; (2) &lt;s:set&gt; (3) &lt;s:if&gt; 、&lt;s:elseif&gt;、&lt;s:else&gt; ...(8) &lt;s:select&gt;、&lt;s:radio&gt;、&lt;s:checkboxlist&gt;

    普元 datacell级联保存

    普元 datacell级联保存 &lt;r:datacell entityType=... &lt;select onfocus="selectLV(this)" &gt; &lt;/select&gt; &lt;/r:field&gt; &lt;/r:datacell&gt;

    简单的一个学生管理系统 servlet+JSP

    &lt;select name="currentPage" onchange="test()" id="currentPage"&gt; &lt;c:forEach begin="1" end="${sessionScope.countPage}" var="page"&gt; &lt;c:choose&gt; &lt;c:when test="${sessionScope....

    三大框架下分页源代码

    &lt;s:property value="pageNow"/&gt;/&lt;s:property value="pageCount"/&gt;页 &lt;s:url id="url_pre" value="news_findAll.action"&gt; &lt;s:param name="pageNow" value="pageNow-1"&gt;&lt;/s:param&gt; &lt;/s:url&gt; ...

    数据库分页脚本 jsp

    &lt;/select&gt;页 &lt;c:if test="${pageNumber&lt;totalPages}"&gt; &lt;a href="Posts?pageNumber=${pageNumber+1}"&gt;下一页&lt;/a&gt; &lt;/c:if&gt; &lt;a href="Posts?pageNumber=${totalPages}"&gt;末页&lt;/a&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;

    flex开发对时间控制

    flex 对时间的关注&lt;mx:Script&gt; &lt;![CDATA[ // Event handler for the DateField change event. private function dateChanged(date:Date):void { if (date == null) selection.text = "Date selected: "; ...

    flex导出excel的代码

    &lt;mx:CheckBox change=" {outerDocument.doSelect(data as Object)} " /&gt; &lt;/mx:Component&gt; &lt;/mx:itemRenderer&gt; &lt;/mx:DataGridColumn&gt; &lt;mx:DataGridColumn headerText="names" dataField="names" width="200" /&gt;...

    Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)

    Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)

    eclipse-testng 离线包下载

    eclipse-testng 离线包&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://exslt.org/math" xmlns:testng=...

    HTML5&CSS3网页制作:select元素.pptx

    &lt;select&gt;和&lt;/select&gt;标记用于在表单中添加一个下拉菜单 &lt;option&gt;和&lt;/option&gt;用于定义下拉菜单中的具体选项 每对&lt;select&gt;和&lt;/select&gt;中至少应包含一对&lt;option&gt;和&lt;/option&gt;。 说明 标记名 常用属性 描述 &lt;select&gt; size...

    在线订餐系统基于Javawen

    &lt;td&gt;用户类型:&lt;/td&gt; &lt;td&gt; &lt;select name="type"&gt; &lt;option value="0"&gt;---请选择---&lt;/option&gt; &lt;option value="club"&gt;会员&lt;/option&gt; &lt;option value="admin"&gt;管理员&lt;/option&gt; &lt;/select&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td...

    struts2中#号的用法

    如果对象是当前页面的,则要使用# 1. &lt;s:bean id="locales" name="com.turorial.Locales" &gt;...&lt;/s:bean&gt; &lt;s:property value="#locales"/&gt;

    repeater的简单使用

    &lt;input id="checkAll" type="checkbox" onclick="SelectAllCheckBox(this)"/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;tr&gt; &lt;td align="center"&gt; &lt;asp:Label ID="Label1" runat="server" Text='&lt;%#...

Global site tag (gtag.js) - Google Analytics