2015-12-21 63 views
1

我试图在地图上使用g:each标记,但GSP无法识别它。GSP/Groovy-g:每个都有对象阵列

的代码是:

if(previousQuestions == null) 
    { 
     previousQuestions = [] 
    } 
    def obj = AnimaisTreeMap.get(curNode) 

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

而g:每个标签是:

​​

的输出是:

[{question=vive na água, answer=Não}, {question=tem listras, answer=Sim}] 

据我知道对象克:每个读取可能在JSON格式“属性:值”,但我的对象是“属性=值”

我该如何解决它?

修订

看到this link后,我找到了解决办法。问题本身就是“向前”的功能,我把“之前的问题”传递给了另一个动作。而不是“参数”,正确的方法是“模型”。

 forward(action:'index', model: [curNode: nextNode.id, 
             previousNode: curNode, 
             curQuestion: curQuestion, 
             previousQuestions: answersTrace])     

当我需要恢复“previousQuestions”时,我必须在控制器中使用“request”注入对象。在此之后,我的代码是OK

1:此链接

+1

显示你的'g.each'标签 – injecteer

+1

'previousQuestions = []'这不是一个地图 – injecteer

+0

@ injecteer我刚刚添加了代码。我也修改了标题,因为是一组对象,而不是 –

回答

1

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

我相信这是你想要什么:

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

同样的结果......我也试过以下 高清singleItem =新的LinkedHashMap() singleItem.question = obj.nodeDescription singleItem.answer = getDescOptionAnswer(optionAnswered) previousQuestions .push(singleItem) –

+0

你看的东西很复杂def singleItem = [:]会做同样的事情。你需要看看上面发送的对象的类,尝试gsp中的.getClass和控制器中它们是否有所不同?如果是这样,它可能是你如何在GSP上销售变量 – Vahid

+0

,该对象是在String读取的 –

0

试试这个:

//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])