2015-12-21 68 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])