【问题标题】:GSP/Groovy - g:each with array of objectsGSP/Groovy - g:each 与对象数组
【发布时间】: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”打印

标签: grails groovy gsp


【解决方案1】:

每次调用push() 时,您似乎都在添加两张地图。尝试创建要添加到地图数组中的每个地图,而不是仅仅传递单独的键值对。

我相信这就是你想要的:

previousQuestions.push(['question':obj.nodeDescription, 
                        'answer': getDescOptionAnswer(optionAnswered)])

【讨论】:

  • 同样的结果...我也试过以下 def singleItem = new LinkedHashMap() singleItem.question = obj.nodeDescription singleItem.answer = getDescOptionAnswer(optionAnswered) previousQuestions.push(singleItem)跨度>
  • 你看的东西非常复杂 def singleItem=[:] 会做同样的事情。你需要看看上面发送的对象的类在 gsp 和控制器中尝试 .getClass 它们有什么不同吗?如果是这样,这可能是您渲染变量的方式
【解决方案2】:

试试这个:

//This is a far better way of checking null strings 
//shorter and also checks for '' as well as null
if(!previousQuestion)  {
   previousQuestions = []
} else {
   //this segment you have not provided so as a test ensure it 
   //matches List element above that you have declared - take a look at console
   println "this is probably your issue ${previousQuestions.getClass()} what is class here ????"
}

def obj = AnimaisTreeMap.get(curNode)

// generate a map on the fly good for iterations, 
// even though this is not iterated.

previousQuestions << ['questions': obj.nodeDescription as String, 
'answer':  getDescOptionAnswer(optionAnswered) as String]

//within genrated map above the elements have been converted to as String 
// Because you have not given the workings of getDescriptionAnswer
// in theory you should not need  as String this is just for testing

render (view: 'something', model: [previousQuestions:previousQuestions])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    相关资源
    最近更新 更多