2017-10-18 8 views
0

Je ne peux pas afficher une structure arborescente parent enfant imbriquée dans la page jsp. En raison d'une erreur de flux de stackover qui est causée parRendu L'affichage parent enfant provoque l'exception StackOverflow

<code><jsp:include page=""/></code> 

Mon DB:

Id || Name || Parentid || 
1 || animal|| 0   || 
2 || Dog || 1   || etc. 

classe Model:

public class Node { 
    private int id; 
    private String name; 
    private int parentId; 
    private List<Node> children; 

    public Node() { 
    } 

    public Node(String name, int parentId, List<Node> children) { 
     this.name = name; 
     this.parentId = parentId; 
     this.children = children; 
    } 

classe DAO:

@Override 
    public List<Node> nodelist() { 

     String sql = "select * from tree"; 
     List<Node> listNode = jdbcTemplate.query(sql, new RowMapper<Node>() { 

      @Override 
      public Node mapRow(ResultSet rs, int rowNum) throws SQLException { 
       Node node = new Node(); 
        node.setId(rs.getInt("id")); 
       node.setName(rs.getString("name")); 
       node.setParentId(rs.getInt("parent_id")); 

       return node; 


      } 

     }); 

     return listNode; 
    } 

Dans le contrôleur i lier l'objet comme ci-dessous:

List<Node> rootNodes; 

@Autowired 
    TreeDao treeDao; 


    @RequestMapping(value = "/node", method = RequestMethod.GET) 
    public ModelAndView getallnodes(ModelAndView model) throws IOException { 

     //menuObj = menuDao.listCategory(); 
     List<Node> listnode = treeDao.nodelist(); 

     getInfiniteTree(listnode); 
     model.addObject("rootNodes",rootNodes); 
     model.setViewName("tree"); 

     return model; 
    } 

    public List<Node> getInfiniteTree(List<Node> nodes) { 
     return findRootNodes(nodes); 
    } 

    private List<Node> findRootNodes(List<Node> nodes) { 
     rootNodes = new ArrayList<Node>(); 
     for (Node node : nodes) { 
      if (node.getParentId() == 0) { 
       rootNodes.add(node); 
       findChildNodes(node, nodes); 
      } 
     } 



System.out.println("rootnodes "+rootNodes); 


    Gson gson = new Gson(); 
    System.out.println(gson.toJson(rootNodes)); 
    //I got the correct json format data here. 
     //Collections.sort(rootNodes); 
     return rootNodes; 
    } 



    private void findChildNodes(Node parentNode, List<Node> nodes) { 
     List<Node> children = new ArrayList<Node>(); 
     parentNode.setChildren(children); 
     for (Node node : nodes) { 
      if (node.getParentId() == parentNode.getId()) { 
       children.add(node); 
       findChildNodes(node, nodes); 
      } 
     } 

     //Collections.sort(children); 
    } 

Dans un autre projet java swing i tester le code similaire et obtenu follwing résultat cordes:

nodes [Node{id=1, name=供应链部, parentId=0, sort=3, children=[Node{id=5, name=物流部, parentId=1, sort=1, children=[]}, Node{id=6, name=采购部, parentId=1, sort=2, children=[]}]}, Node{id=2, name=技术部, parentId=0, sort=1, children=[Node{id=7, name=开发部, parentId=2, sort=2, children=[Node{id=12, name=后端部, parentId=7, sort=2, children=[]}, Node{id=13, name=前端部, parentId=7, sort=2, children=[Node{id=14, name=diee, parentId=13, sort=2, children=[]}, Node{id=15, name=diee, parentId=13, sort=2, children=[]}]}]}, Node{id=8, name=测试部, parentId=2, sort=1, children=[]}, Node{id=9, name=运维部, parentId=2, sort=3, children=[]}]}, Node{id=3, name=行政部, parentId=0, sort=2, children=[Node{id=10, name=招聘部, parentId=3, sort=1, children=[]}, Node{id=11, name=人事部, parentId=3, sort=2, children=[]}]}, Node{id=4, name=公关部, parentId=0, sort=4, children=[]}] 

Le problème est que je ne peux pas l'afficher à l'aide récursion jsp, j'ai essayé ci-dessous le code:

tree.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 

    </head> 
    <body> 
     <c:set var="menuitem" value="${rootNodes}" scope="request" /> 
     <jsp:include page="menuitem.jsp" /> 
    </body> 
</html> 

menuitem.jsp:

Il n'est pas compris la page à travers les enfants infinis et comme je suis un nouveau dans la récurrence JSP s'il vous plaît ignorer mes fautes. Je suis resté ici quelques jours. Exception:

Don't know how to iterate over supplied "items" in &lt;forEach&gt; 
org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/menuitem.jsp at line 14 

11: 
12: 
13: <ul> 
14:  <c:forEach var ="menuitems" items="${menuitem}"> 
15:   <li><a href="#">${menuitems.name}</a></li> 
16:   <ul> 
17: <c:if test="${fn:length(menuitems.children) gt 0}"> 

Répondre

0

Enfin, je résolus de cette manière:

tree.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 

    </head> 
    <body> 



     <c:set var="menuitem" value="${rootNodes}" scope="request" /> 
     <jsp:include page="menuitem.jsp" /> 
    </body> 
</html> 

Et menuitem.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 




<ul> 
    <c:forEach var ="menuitems" items="${menuitem}"> 
     <li><a href="#">${menuitems.name}</a></li> 
     <ul> 
    <c:if test="${fn:length(menuitems.children) gt 0}"> 
     <li class="droprightMenu"> 
      <c:out value="Has children"></c:out> 
     <%--<c:forEach var="menuitemc" items="${menuitems.children}">--%> 
      <c:set var="menuitem" value="${menuitems.children}" scope="request" /> 
      <jsp:include page="menuitem.jsp" /> 
     <%--</c:forEach>--%> 
     </li> 
    </c:if> 
     </ul> 
     </c:forEach> 
</ul> 

espère que ça va aider quelqu'un.

1

menuitem.jsp

<c:forEach var="menuitem" items="${menuitem.children}"> 
    <li><a href="#" id="${menuitem.id}"><i class="fa fa-circle-o"></i> <span>${menuitem.name} </span></a></li> 
    <c:set var="menuitem" value="${menuitem}" scope="request" /> 
    <jsp:include page="/WEB-INF/views/menuitem.jsp" /> 
</c:forEach> 

Je pense que le problème réside ici. Essayez de renommer var="menuitem" en var="childmenuitem. Je crois que vous continuez actuellement à passer le même objet à votre sous-menu, provoquant une boucle infinie.

+0

dans les deux étiquettes et ? – RidwanulHaque