我想为树结构创建自定义Java JSP标记,例如:

<ul>
   <li>
      <a href="#">Root</a>
      <ul>
          <li><a href="#">Node</a></li>
          <li><a href="#">Node</a></li>
          <li><a href="#">Node</a></li>
          <li>
              <a href="#">Node</a>
              <ul>
                  <li><a href="#">Node</a></li>
                  <li><a href="#">Node</a></li>
              </ul>
          </li>
      </ul>
  </li>
</ul>




在JSP文件中,XML应该如下所示:

<lib:treeview>
    <lib:treenode path="/some/path.do" label="Root">
    <lib:treenode path="/some/path.do" label="NodeLabel"/>
    <lib:treenode path="/some/path.do" label="NodeLabel"/>
    <lib:treenode path="/some/path.do" label="NodeLabel"/>
    </lib:node>
</lib:treeview>


是否有人实现了像这样的嵌套结构?哪些类最好使用?也许有人有一些有用的链接?请帮助 :)

最佳答案

还没有测试过,类似这样的东西应该可以工作:

treeview.tag:

<%@ tag description="treeview" %>
<ul>
    <jsp:doBody />
</ul>


treenode.tag:

<%@ tag description="treenode" %>
<%@ attribute name="label" required="true" type="java.lang.String" %>
<%@ attribute name="path" required="true" type="java.lang.String" %>
<li>
    <a href="${path}">${label}</a>
    <jsp:doBody />
</li>


test.jsp:

<lib:treeview>
    <lib:treenode label="root" path="#">
        <lib:treeview>
            <lib:treenode label="node" path="#">
            <lib:treenode label="node" path="#">
            <lib:treenode label="node" path="#">
        </lib:treeview>
    </lib:treenode>
</lib:treeview>


输出:

<ul>
    <li>
        <a href="#">root</a>
        <ul>
            <a href="#">node</a>
            <a href="#">node</a>
            <a href="#">node</a>
        </ul>
    </li>
</ul>

关于java - 嵌套树结构的Java JSP自定义标记。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7998545/

10-15 21:06