要做的这颗目录树有3级,思路是将所要查询的内容放到一个集合,1级和2级记录各放一个集合。前台显示时,通过判断所查询的内容的级别,来确定各级内容以及内容的位置
如下是后台代码:
public ActionForward pubTreeSelectList(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
ActionForward forward = null;
Map map = new HashMap();
PubTreeForm theForm = (PubTreeForm) form;
try {
PubTreeVO vo = theForm.getVo();
IPubTreeBll bll = PubTreeBll.getInstance();
// 如果是查询省名牌评价目录
if(vo.getKindId()==13 && vo.getInnerId()==1){
// 省名牌评价目录第一级
PubTreeVO vo1 = new PubTreeVO();
vo1.setKindId(13);
vo1.setInnerId(1);
vo1.setDeepth(0);
Collection deepth1List = bll.get(vo1);
request.setAttribute("deepth1List", deepth1List);
// 省名牌评价目录第二级
PubTreeVO vo2 = new PubTreeVO();
vo2.setKindId(13);
vo2.setInnerId(1);
vo2.setDeepth(2);
Collection deepth2List = bll.get(vo2);
request.setAttribute("deepth2List", deepth2List);
}
// 区局列表显示新增 zhangl 20100510 开始
String tempKindId = request.getParameter("vo.kindId");
String tempInnerId = this.getUserSymbol(request);
if (tempKindId != null) {
if (tempKindId.equals("9")) {
if (tempInnerId != null) {
vo.setInnerId(Integer.parseInt(tempInnerId));
}
}
}
// 区局列表显示新增 zhangl 20100510 完成
map.put("Vo", vo);// 需要查找的条件
map.put("curPage",
new Integer(this.getCurrPageNumber(mapping, form, request,
response)));// 当前页
map.put("pageSize", this.getPageSize());// 每页显示的记录条数
Page page = bll.get(map);
this.setPage(request, page);
this.getSelectMessage(theForm);
forward = mapping.findForward("pubTreeSelectList");
} catch (Exception e) {
logger.info(e);
return forward = this.forwardExceptionPage(mapping, request, e);
}
return forward;
}
前台部分代码如下:
<c:forEach var="vo" items="${pageList}" varStatus="status">
<tr class="<c:choose><c:when test="${status.index%2==0}">oddRow</c:when><c:otherwise>evenRow</c:otherwise></c:choose>"
οnclick="javascript:document.getElementById('<c:out value="${vo.id}"/>').click();" >
<td>
<input type="checkbox" name="key" id="<c:out value="${vo.id}" />"
value="<c:out value="${vo.id}" />"
onClick="setCheckValue('<c:out value="${vo.id}" />','<c:out value="${vo.id}" />')" >
</td>
<c:if test="${pubTreeForm.vo.kindId!=13}">
<td><c:out value="${vo.code}"/></td>
<td><c:out value="${vo.codeName}"/></td>
</c:if>
<c:if test="${pubTreeForm.vo.innerId==1 && pubTreeForm.vo.kindId==13}">
<c:choose>
<c:when test="${vo.deepth == 0}">
<td><c:out value="${vo.codeName}"/></td>
<td></td>
<td></td>
</c:when>
</c:choose>
<c:choose>
<c:when test="${vo.deepth == 2}">
<c:forEach var="vo1" items="${deepth1List}" varStatus="status1">
<c:if test="${vo.parentId == vo1.id}">
<td><c:out value="${vo1.codeName}" /></td>
</c:if>
</c:forEach>
<td><c:out value="${vo.codeName}"/></td>
<td></td>
</c:when>
</c:choose>
<c:choose>
<c:when test="${vo.deepth == 3}">
<c:forEach var="vo2" items="${deepth2List}" varStatus="status2">
<c:if test="${vo.parentId==vo2.id}">
<c:forEach var="vo3" items="${deepth1List}" varStatus="status3">
<c:if test="${vo2.parentId==vo3.id}">
<td><c:out value="${vo3.codeName}" /></td>
</c:if>
</c:forEach>
<td><c:out value="${vo2.codeName}"/></td>
</c:if>
</c:forEach>
<td><c:out value="${vo.codeName}"/></td>
</c:when>
</c:choose>
</c:if>
最终可以达到的效果如图:
这个是查询所有记录
这个是查询二级内容的:
这个是查询三级内容的: