当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Repeat code in JSTL

问题描述:

I need help, I have a JSTL code I need to use it again if a condition is met, such as:

<c:forEach items="${arbol.subTree}" var="arbol">

name = <c:out value="${arbol.nombre}"/>

<c:if test="${arbol.subTree!=null}">

/ / do the last foreach

</ c: if>

</ c: forEach>

If anyone can help me, if you have doubt, I'm traveling and need a tree forEach do that every time you have an available node.

As the trees may have thousands of nodes down, as I call the above code without having to write thousands of times?.

网友答案:

JSTL is not meant to do any recursive method at all. If you want/need to display a tree, then it would be better passing the tree structure from Java to JavaScript using JSON, then evaluate the JSON object in JavaScript and apply any recursive method you want/need.

In Java, turn your structure into a JSON string. This can be done using jackson:

YourTree tree = ...
ObjectMapper mapper = new ObjectMapper();
request.setAttribute("tree", mapper.writeValueAsString(tree));

In JavaScript, get the JSON string:

<script type="text/javascript">
    window.onload = function() {
        var tree = JSON.parse('${tree}');
        //do what you want/need with your tree structure...
    }
</script>
分享给朋友:
您可能感兴趣的文章:
随机阅读: