【发布时间】:2015-08-18 11:15:55
【问题描述】:
亲爱的 grails 专家
我正在向我的 gsp 视图页面发送一份出版物列表。我可以很好地看到列表,但是在将其中一个出版物添加到书籍列表中并呈现出版物 gsp 视图页面后,出版物列表不会被视为出版物列表,而是作为字符串。因此,当我尝试获取每个出版物时,它都会给我字符。
我使用的是grails 2.2.0的版本。
这里我在控制器条目中发送出版物:
def books{
def unit = Unit.get(params.id)
def period = Period.get(params.periodId)
def publications = []
if (unit) {
def units = unit.downHierarchy().id
units.removeAll { null }
publications = Publication.publicationsOfUnits(units)?.listDistinct()?.findAll{it.date.getAt(Calendar.YEAR) == period.year}?.sort{ it.date }
}
[publications : publications, id: unit.id, unitId: unit.id, periodId: period.id]
}
这是我的查看页面:
<table>
<g:each in="${publications}" var="p">
<g:if test="${p instanceof publicationBook}">
<tr>
<td>${p.authors.join(",")}</td>
<td>${p.title}</td>
<td>
<a href="#" onclick="$('#author').val('${p.authors.join(',')}');$('#titleAndSubtitle').val('${p.title}');$('#unitId').val('${unitId}');$('#periodId').val('${periodId}');$('#id').val('${unitId}');bookForm.submit();">Add to Books</a>
</td>
</tr>
</g:if>
</g:each>
</g:table>
在这里我将出版物添加到书籍中:
def saveBook = {
saveEntry("book", new Book())
render(view:"books", model: params)
}
这是我将出版物列表再次发送给控制器进行渲染的书籍表单:
<g:form name="bookForm" controller="entries" action="saveBook" class="default" method="post">
<g:hiddenField name="entryId" value="${entry?.id}"/>
<g:hiddenField name="unitId" value="${unitId}"/>
<g:hiddenField name="periodId" value="${periodId}"/>
<g:hiddenField name="publications" value="${publications}"/>
<div class="buttons">
<button class="button icon icon_arrow_right leave_empty" onclick="$('#leaveEmpty').val('true');bookForm.submit();"
</div>
</g:form>
我将非常感谢任何解决问题的建议或帮助..
【问题讨论】:
-
${(p instanceof publicationBook).getClass()}是否返回class java.lang.Boolean? -
更好:请在 if 的右大括号之后写上
println publications.getClass(),并向我们展示它打印的内容。 -
打印“println class ...publicationBook”
-
嗯,它应该打印 ArrayList 或其他可以运行迭代的类。你能检查一下
Publication.publicationsOfUnits(units)?.listDistinct()?.findAll{it.date.getAt(Calendar.YEAR) == period.year}?.sort{ it.date }是否真的返回了一个列表/数组/集合?也许它只有一个元素,它表示为自身而不是列表。