Recursion in GSP page

你。 提交于 2019-12-20 02:36:22

问题


I have a domain

class Node {   

    String nodeId
    String label

    Node parent    
}

In the GSP page I want to start with the root and print all its children (note I have a reference to parent and not the child). Any idea how to do this ? I am sorry but I am new and really confused. I want to print everything going down from root(not root) and root has no parent(its null). So i have written

<g:each in="${nodes}" var="node">
<g:if test="${node.parent!=null}">

  ${node.label}
  <g:render template="node" model="[nodes:Node.findAllByParent(node)]" />
</g:if>
</g:each>

In the above code not sure what parent_node_intance should be. The nodes list starts with root. I dont want to print that but start from everything else that has root as parent.

node.gsp

<g:if test="${nodes}">
<ul>
  <g:each in="${nodes}" var="node">
    <li>
      ${node.label}
      <g:render template="node" model="[nodes:Node.findAllByParent(node)]" />
    </li>
  </g:each>
</ul>
</g:if>

Getting the following error which I am sure is caused by the root

2014-10-02 12:28:21,693 [http-bio-8080-exec-1] ERROR errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [GET] /taxonomy - parameters:
outputFormat: concept
hierarchyId: lp
Cannot invoke method findAllByParent() on null object. Stacktrace follows:
Message: Error evaluating expression [[nodes:Node.findAllByParent(node)]] on line [11]: Cannot invoke method findAllByParent() on null object
    Line | Method
->>   11 | run       in C:/Users/U6021072/Documents/workspace-ggts-3.6.0.RELEASE/ae-and-sdx-analysis-ui/target/work/plugins/taxonomy-toolkit-for-grails-0.02-SNAPSHOT/grails-app/views/taxonomy/concept.gsp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Caused by NullPointerException: Cannot invoke method findAllByParent() on null object
->>   11 | doCall    in C__Users_U6021072_Documents_workspace_ggts_3_6_0_RELEASE_ae_and_sdx_analysis_ui_target_work_plugins_taxonomy_toolkit_for_grails_0_02_SNAPSHOT_grails_app_views_taxonomy_concept_gsp$_run_closure4
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    198 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread

回答1:


So, you want to print all the nodes that have the known parent?

What about something like:

<g:each in="${Node.findAllByParent(parent_node_instance)}" var="node">
  ${node}<br>
</g:each>

But since you are talking about recursion, I think you also want to print all descendants.

Create a _node.gsp template:

<g:each in="${nodes}" var="node">
  ${node}<br>
  <g:render template="node" model="[nodes:Node.findAllByParent(node)]" />
</g:each>

And call with:

<g:render template="node" model="[nodes:Node.findAllByParent(parent_node_instance)]" />

You can easily add a depth variable to the model to indent each generation properly, or use the <ul> and <li> elements in the template.

parent_node_instance is the root you want to start printing your tree from, it can be the absolute root or any node in the tree.

findAllByParent() is a dynamic finder function. See http://grails.org/doc/latest/guide/GORM.html#finders for details.




回答2:


you can also use the @collection/@bean:

_node.gsp

<div class="fancy nested">
  id:${it.nodeId} - label:${it.label}
  <g:render template="node" collection="${Node.findAllByParent(it.node)}" />
</div>

main.gsp

<g:render template="node" bean="${rootNode}" />


来源:https://stackoverflow.com/questions/26129073/recursion-in-gsp-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!