我有一个 Section 域对象,它看起来像这样:-
class Section {
String name;
Section parent;
int level = 0;
Boolean isRoot = false;
static hasMany = [children:Section]
static mappedBy = [children:'parent']
....
def Boolean isLeaf(){
return children.isEmpty()
}
}
还有一个看起来像这样的 Story 域对象:-
class Story {
String title;
String story;
List link;
List substory;
List picture;
Section publishUnder
....
}
在 Bootsrap.groovy 中,我将这些部分设置为:-
def Section Home = new Section(name:"Home").save(flush: true);
def Section News = new Section(name:"News").save(flush: true);
def Section News_Crime = new Section(name:"Crime",parent:News).save(flush: true);
def Section News_Politics = new Section(name:"Politics",parent:News).save(flush: true);
def Section News_Politics_Labour = new Section(name:"Labour",parent:News_Politics).save(flush: true);
def Section News_Politics_Tories = new Section(name:"Tories",parent:News_Politics).save(flush: true);
def Section News_Politics_LibDems = new Section(name:"LibDems",parent:News_Politics).save(flush: true);
故事在章节下发布。然后我有一个看起来像这样的部分服务:-
@Transactional(readOnly = true)
Section[] getSectionsForCurrentPage(Long sectionID = 0)
{
def rootLevel = 0
def Section selectedSection = Section.get(sectionID)?:Section.findByName("Home")
def Section[] sectionList = []
(sectionList as List).add(0,[sectionInstanceList: Section.findWhere(level:rootLevel)])
if(!selectedSection.isLeaf()){
sectionList[1] = [sectionInstanceList:Section.findWhere(level:1,parent:selectedSection)]//just for testing needs to be iterative
}
}
GSP 模板如下所示:-
<%@ page import="com.keane.reg.content.Section" %>
<header>
<g:each in = "${sectionList}" status="i" var = "sectionInstanceList">
<div id="navLevel_+${i}}" class="headerNav">
<ul class="navLevel_+${i}}">
<g:each in="${sectionInstanceList}" status="j" var="sectionInstance">
<li><g:link action="show" id="${sectionInstance.id}">${fieldValue(bean: sectionInstance, field: "name")} </g:link></li>
</g:each>
</ul>
</div>
</g:each>
</header>
在 Home 控制器中,我这样做:-
def sectionService
def index =
{
List<Section> sectionList = sectionService.getSectionsForCurrentPage()
return [sectionList:sectionList];
}
在索引页面中,我这样做:
<g:render template="/layouts/headernav" />
我得到的标记结果是这样的: -
<header>
</header>
我还在学习 grails,所以这可能很明显,但为什么没有填充标题?有一个包含 Section 对象的列表,我认为我正确地引用了该列表。
任何帮助将非常感激