生成ul li嵌套树结构

时间:2013-09-23 13:22:20

标签: java html mysql jsp

我有Employee表。在那张桌子上我有reporting_to字段。是父母子女关系。我想在ul li中显示嵌套的JSP html树。

这是我的mysql表。

enter image description here

我想要像

这样的东西
<ul>
<li>root
 <ul>
  <li>1</li>
  <li>2
      <ul>
       <li>1</li>
       <li>2</li>
      </ul>
  </li>
 </ul>
</li>
</ul>

这是我的2个java函数

public ResultSet getSavedTree() throws SQLException{
        ResultSet rs = null;
        try {   
             s = ds.createStatement();               

             rs = s.executeQuery("SELECT * FROM pep.employee_tree order by reporting_to;");

        } catch (Exception e) {
            e.printStackTrace();
        }

        return rs;

        }

    public ResultSet getPertiSavedTree(int sapCode) throws SQLException{
        ResultSet rs = null;
        try {   
             s = ds.createStatement();           

             rs = s.executeQuery("SELECT * FROM pep.employee_tree WHERE reporting_to = "+sapCode+" order by reporting_to;");

        } catch (Exception e) {
            e.printStackTrace();
        }

        return rs;

        }

这是我的HTML

<ul  id="org" style="display:none">
            <li><a href="#" class="ui-btn ui-shadow ui-btn-corner-all ui-btn-inline ui-btn-hover-e ui-btn-up-e"><span class="ui-btn-inner"><span class="ui-btn-text" style="font-size:10px;">3214657890<br/>Root<br/>SAP<br/>SSE</span></span></a>
                <ul id="main_child_ul" class="children">


              <%
               ResultSet SavedEmployeesTree = empTree.getSavedTree();

               String children[];
               String sapCode="";
                while (SavedEmployeesTree.next()){
                    ResultSet SavedEmployeesTreeChild = empTree.getPertiSavedTree(Integer.parseInt(SavedEmployeesTree.getString("sap_code")));
                        while(SavedEmployeesTreeChild.next())
                        {
                            if(SavedEmployeesTree.getString("sap_code").equals(SavedEmployeesTreeChild.getString("reporting_to")))
                            {
                    %>

                            <li><a href="#" class="ui-btn ui-shadow ui-btn-corner-all ui-btn-inline ui-btn-hover-b ui-btn-up-b"><span class="ui-btn-inner"><span class="ui-btn-text" style="font-size:10px;"><%=SavedEmployeesTreeChild.getString("sap_code")%></span></span></a>



            <%              }
                            else
                            {%>
                                <ul class="children">
                                <li><a href="#" class="ui-btn ui-shadow ui-btn-corner-all ui-btn-inline ui-btn-hover-b ui-btn-up-b"><span class="ui-btn-inner"><span class="ui-btn-text" style="font-size:10px;"><%=SavedEmployeesTreeChild.getString("sap_code")%></span></span></a></li>
                                </ul>
                            <%}
                        }
                } 

            %>
            </li>
               </ul>




            </li>
        </ul>

0 个答案:

没有答案