在 gsps (grails) 上显示列表

发布于 2024-11-16 22:35:36 字数 2122 浏览 1 评论 0原文

我是 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>

所发生的情况是,给出的问题是正确的,但答案当然不是。对于每个问题,都会显示所有答案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 = {
    }
}

I'm new to grails 1.3.7 and I try to access my database and to show my data on a gsp. Now Ive got the following problem: I've got a list of questions (listofQuestions) and a list of answers (listofAnswers). To each question belongs one Lpicanswer object which contains various answers (answera, answerb)

So when I create those lists, in the end I've got one list containing the questions and one list containing lpicanswer-objects. each lpicanswerobject has an lpicid (which is the id of the question), so that they are related to each other.

Here is the code to create those lists:

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

now I want to show them on my gsp. here is what I do:

<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>

what happens is, that the questions are given out correct, but the answers of course not. For each question all answersa, all answersb, etc are shown (like: [answera-from-question1, answera-from-question2] and so on) how can I solve this?

any help will be apreciated! :-)

[EDIT] Here is the structure of lpicquestions and lpicanswers, thanks for helping!! :-)

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 = {
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一页 2024-11-23 22:35:36

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

aList is not an object or map. So you can't put something like:
${alist.answera}

change the view to.

<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>

if assuming that cacheService.questionList contains list of id for Lpicquestions
change

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文