gpt4 book ai didi

list - 在gsps上显示列表(grails)

转载 作者:行者123 更新时间:2023-12-02 15:38:14 24 4
gpt4 key购买 nike

我是grails 1.3.7的新手,我尝试访问数据库并在gsp上显示数据。现在,我遇到了以下问题:我有一个问题列表(listofQuestions)和一个答案列表(listofAnswers)。每个问题都属于一个Lpicanswer对象,其中包含各种答案(answera,answerb)

因此,当我创建这些列表时,最后我得到了一个包含问题的列表和一个包含lpicanswer-objects的列表。每个lpicanswer对象都有一个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 = {
}
}

最佳答案

aList不是对象或 map 。因此,您不能输入以下内容:
$ {alist.answera}

将 View 更改为。

<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);
}

关于list - 在gsps上显示列表(grails),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6469941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com