我是grails 1.3.7的新手,我尝试访问我的数据库并在gsp上显示我的数据。现在我遇到了以下问题:我有一个问题列表(listofQuestions)和一个答案列表(listofAnswers)。每个问题属于一个Lpicanswer对象,其中包含各种答案(answera,answerb)
因此,当我创建这些列表时,最后我有一个包含问题的列表和一个包含lpicanswer对象的列表。每个lpicanswerobject都有一个lpicid(这是问题的id),因此它们彼此相关。
以下是创建这些列表的代码:
List listofQuestions = []
List listofAnswers = []
def ques
def question
def ans
// we create a questions list containing questions
// we create a answers list containing answers-objects for a question
for (int i = 0; i <= cacheService.questionList.size()-1; i++) {
ques = Lpicquestions.get(cacheService.questionList[i]);
question = ques.question;
listofQuestions.add(question);
}
for (int i = 0; i <= cacheService.questionList.size(); i++) {
ans = Lpicanswers.get(cacheService.questionList[i]);
listofAnswers.add(ans);
}
return new ModelAndView("/result/resultdetail", [ qlist : listofQuestions, alist : listofAnswers ]);}
现在我想在我的gsp上显示它们。这是我的工作:
<g:each in="${qlist}">
<b>${it}</b><br/>
${alist.answera}<br/>
${alist.answerb}<br/>
${alist.answerc}<br/>
${alist.answerd}<br/>
${alist.answere}<br/>
${alist.answerf}<br/>
${alist.answerg}<br/>
${alist.answerh}<br/>
</g:each>
会发生什么,问题是正确的,但答案当然不是。对于每个问题,所有答案,所有答案等都显示出来(如:[answera-from-question1,answera-from-question2]等等)我该如何解决这个问题?
任何帮助都会被贬低! : - )
[编辑]这是lpicquestions和lpicanswers的结构,感谢您的帮助!! : - )
package com.lpic
class Lpicquestions {
int lpicchapter
String question
static constraints = {
question(nullable:false, blank:false, maxSize:1000)
lpicchapter(nullable:false, blank:false)
}
}
package com.lpic
class Lpicanswers {
Lpicquestions lpicid
String answera
String answerb
String answerc
String answerd
String answere
String answerf
String answerg
String answerh
static constraints = {
}
}
答案 0 :(得分:1)
aList不是对象或地图。所以你不能把这样的东西:
$ {alist.answera}
将视图更改为。
<g:each var="question" in="${qlist}">
<b>${question}</b><br/>
<g:each var="answer" in="${aList}">
<g:if test="${answer.lpicid?.question == question}">
<b>${answer.answera}</b><br/>
<b>${answer.answerb}</b><br/>
<b>${answer.answerc}</b><br/>
<b>${answer.answerd}</b><br/>
<b>${answer.answere}</b><br/>
<b>${answer.answerf}</b><br/>
<b>${answer.answerg}</b><br/>
<b>${answer.answerh}</b><br/>
</g:if>
</g:each>
</g:each>
如果假设cacheService.questionList包含Lpicquestions的id列表 改变
for (int i = 0; i <= cacheService.questionList.size(); i++) {
//ans = Lpicanswers.get(cacheService.questionList[i]);
ans = Lpicanswers.findWhere(['lpicid' : Lpicquestions.get(cacheService.questionList[i])]);
listofAnswers.add(ans);
}