【发布时间】:2015-12-21 16:01:17
【问题描述】:
我正在尝试在地图上使用 g:each 标签,但 GSP 无法识别它。
代码是:
if(previousQuestions == null)
{
previousQuestions = []
}
def obj = AnimaisTreeMap.get(curNode)
previousQuestions.push('question':obj.nodeDescription, 'answer': getDescOptionAnswer(optionAnswered))
g:each 标签是:
<g:each in="${previousQuestions}" var="row" status="i">
<div>
<label>${row.question}</label>
<label>${row.answer}</label>
</div>
<br/>
</g:each>
输出是:
[{question=vive na água, answer=Não}, {question=tem listras, answer=Sim}]
据我所知,g:each 读取的对象可能是 JSON 格式“property:value”,但我的对象是“property=value”
我该如何解决?
更新
看到this link后,我找到了解决办法。问题本身在于“前进”功能,我将“previousQuestions”传递给另一个动作。而不是“参数”,正确的方法是“模型”。
forward(action:'index', model: [curNode: nextNode.id,
previousNode: curNode,
curQuestion: curQuestion,
previousQuestions: answersTrace])
当我需要恢复“previousQuestions”时,我必须在控制器中使用“request”注入对象。之后,我的代码就OK了
1: 这个链接
【问题讨论】:
-
显示你的
g.each标签 -
previousQuestions = []这不是地图 -
@injecteer 我刚刚添加了代码。我也更正了标题,因为它是一个对象数组
-
还显示控制器操作如何返回/渲染模型。顺便说一句,您显示的“输出”在哪里?在控制台中?
-
@injecteer 我用“log.info”打印