我有一個域遞歸在GSP
class Node {
String nodeId
String label
Node parent
}
在GSP頁面我要開始與根並打印其所有的孩子(注意我有父母的引用,而不是孩子)。任何想法如何做到這一點? 我很抱歉,但我是新的,真的很困惑。我想打印從根(不是root)開始的所有內容,並且root沒有父節點(它的null)。所以我寫了
<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>
在上面的代碼中不確定parent_node_intance應該是什麼。節點列表以root開頭。我不想打印該文件,而是從其他所有根目錄作爲父文件開始。
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>
收到以下錯誤其中我肯定是根
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
好主意調用模板遞歸。我想我會使用查找邏輯將'getChildren()'方法添加到'Node',而不是將其擴散。 – Steinar 2014-10-01 08:47:56
@Steinar小心! 'Node.getChildren()'不會有TX上下文,所以如果你不注意,你可以得到一個LIE – injecteer 2014-10-01 09:11:40