在GSP页面递归 [英] Recursion in GSP page

查看:127
本文介绍了在GSP页面递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个域

 类节点{

字符串nodeId
字符串标签

Node父母
}

在我想要的GSP页面从根开始并打印所有子项(注意我有一个对父项的引用,而不是对子项的引用)。任何想法如何做到这一点?
我很抱歉,但我是新的,真的很困惑。我想打印从根(不是root)开始的所有内容,并且root没有父节点(它的null)。所以我写了

 < g:each in =$ {nodes}var =node> 

$ {node.label}
< / g:if>
< / g:每个>

在上面的代码中,不确定parent_node_intance应该是什么。节点列表以root开头。我不想打印该文件,而是从其他所有根目录作为父文件开始。

node.gsp

 < ; g:if test =$ {nodes}> 
< ul>
< li>
$ {node.label}
< / li>
< / g:每个>
< / ul>
< / g:if>

获取以下错误,我确定这是由

  2014-10-02 12:28:21,693 [http-bio-8080-exec-1]错误errors.GrailsExceptionResolver  -  NullPointerException发生在处理请求时:[GET] / taxonomy  - 参数:
outputFormat:概念
hierarchyId:lp
无法在null对象上调用方法findAllByParent()。 Stacktrace如下所示:
消息:在[11]行上计算表达式[[nodes:Node.findAllByParent(node)]]时出错:无法在空对象
上调用方法findAllByParent()Line |方法
- >> 11 |运行在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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
由NullPointerException引起:无法在null对象上调用方法findAllByParent()
- >> 11 | do_all_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 | java.util.concurrent.ThreadPoolExecutor中的runWorker
| 615 |跑 。 。 。在java.util.concurrent.ThreadPoolExecutor $ Worker
^ 745 |在java.lang.Thread中运行


解决方案

打印所有拥有已知父级的节点?



类似这样的事情:

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

但是既然你在谈论递归,我想你也想打印所有的后代。



创建一个 _node.gsp 模板:

 < g:每个in =$ {nodes}var =node> 
$ {node}< br>
< g:render template =nodemodel =[nodes:Node.findAllByParent(node)]/>
< / g:每个>

然后调用:

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

您可以轻松地向模型添加深度变量以正确缩进每一代,或使用< ul> < li> 元素。

> parent_node_instance 是要开始打印树的根,它可以是绝对根或树中的任何节点。



findAllByParent()是一个动态查找器函数。请参阅 http://grails.org/doc/latest/guide/GORM.html#发现者了解详情。

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

解决方案

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.

这篇关于在GSP页面递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆