【发布时间】:2011-09-22 03:11:47
【问题描述】:
我是 grails 1.3.7 的新手,我尝试访问我的数据库并在 gsp 上显示我的数据。现在我遇到了以下问题:我有一个问题列表(listofQuestions)和一个答案列表(listofAnswers)。每个问题都属于一个 Lpicanswer 对象,其中包含各种答案 (answera, answerb)
所以当我创建这些列表时,最后我得到了一个包含问题的列表和一个包含 lpicanswer-objects 的列表。每个 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>
实际情况是,给出的问题是正确的,但答案当然不是。对于每个问题,都会显示所有答案 a、所有答案 b 等(例如:[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 = {
}
}
【问题讨论】: