最近在做一个公安的项目,因为部门比较多,并且存在无限级的子部门,子子部门…………,所以在显示部门的时候要求以无限树级的形式来显示这个部门的信息。在数据库设计的时候有个部门表,部门表里面有个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>标签。
这里面就是使用了递归来实现的无限级的树形菜单。
分享到:
相关推荐
Address private int aid; private String aname; public int getAid() { ... <s:select name="address" headerKey="-1" headerValue="请选择" list="lis" listKey="aid" listValue="aname" /> </s:form>
myeclipse2017 jsp页面 <c:if>在select里面写 导致后面代码不是彩色问题 一半黑一半彩色
<html:select>: 选择默认值
html:select>标签的用法 <html:select property="theChoice"> <html:options collection="choiceLists" property="value" labelProperty="description" /> </html:select>
[JavaScript]Javascript 设置与读取Co 三级select列表。
<%@ taglib uri="/struts-tags" prefix="s" %> 结合例子,重点掌握以下标签的用法 (1) <s:property> (2) <s:set> (3) <s:if> 、<s:elseif>、<s:else> ...(8) <s:select>、<s:radio>、<s:checkboxlist>
1. <s:select list=""></s:select>-----单选框 2. <s:set name=""></s:set>-----赋予变量一个特定范围内的值 3. <s:sort comparator=""></s:sort>-----通过属性给list分类 4. <s:submit></s:submit>-----提交按钮...
<%@ taglib uri="/struts-tags" prefix="s" %> 结合例子,重点掌握以下标签的用法 (1) <s:property> (2) <s:set> (3) <s:if> 、<s:elseif>、<s:else> ...(8) <s:select>、<s:radio>、<s:checkboxlist>
普元 datacell级联保存 <r:datacell entityType=... <select onfocus="selectLV(this)" > </select> </r:field> </r:datacell>
<select name="currentPage" onchange="test()" id="currentPage"> <c:forEach begin="1" end="${sessionScope.countPage}" var="page"> <c:choose> <c:when test="${sessionScope....
<s:property value="pageNow"/>/<s:property value="pageCount"/>页 <s:url id="url_pre" value="news_findAll.action"> <s:param name="pageNow" value="pageNow-1"></s:param> </s:url> ...
</select>页 <c:if test="${pageNumber<totalPages}"> <a href="Posts?pageNumber=${pageNumber+1}">下一页</a> </c:if> <a href="Posts?pageNumber=${totalPages}">末页</a> </form> </body> </html>
flex 对时间的关注<mx:Script> <![CDATA[ // Event handler for the DateField change event. private function dateChanged(date:Date):void { if (date == null) selection.text = "Date selected: "; ...
<mx:CheckBox change=" {outerDocument.doSelect(data as Object)} " /> </mx:Component> </mx:itemRenderer> </mx:DataGridColumn> <mx:DataGridColumn headerText="names" dataField="names" width="200" />...
Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)
eclipse-testng 离线包<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://exslt.org/math" xmlns:testng=...
<select>和</select>标记用于在表单中添加一个下拉菜单 <option>和</option>用于定义下拉菜单中的具体选项 每对<select>和</select>中至少应包含一对<option>和</option>。 说明 标记名 常用属性 描述 <select> size...
<td>用户类型:</td> <td> <select name="type"> <option value="0">---请选择---</option> <option value="club">会员</option> <option value="admin">管理员</option> </select> </td> </tr> <tr> <td...
如果对象是当前页面的,则要使用# 1. <s:bean id="locales" name="com.turorial.Locales" >...</s:bean> <s:property value="#locales"/>
<input id="checkAll" type="checkbox" onclick="SelectAllCheckBox(this)"/> </td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td align="center"> <asp:Label ID="Label1" runat="server" Text='<%#...