将元素保存在 Map/Array/Collection ...... Grails

发布于 2024-11-16 21:31:04 字数 577 浏览 2 评论 0原文

我是 grails 1.3.7 的新手,我遇到了问题。

我想将不同的元素/参数存储在一个 list/array/map/ 中。 要存储的数据如下所示:

id:answera,answerb,answerc,answerd,answere,answerf,answerg,answerh

id是一个数字

answersbooleans

所以我有很多 ids(好吧,也许 20 个),每个 8 个answers-booleans。 如何最好地存储它们,以便我可以轻松地再次访问它们?

谢谢:-)

[编辑] 非常感谢这些答案,我现在就尝试一下! :-)

我现在有一个包含 id (int) 和一个代表我的答案的对象的地图(它是一个 pojo,其中包含布尔值 answera、answerb 等...)

现在我将此地图提供给 gsp。我怎么知道从中获取数据?感谢您的帮助! :-)

I'm new to grails 1.3.7 and I have a problem.

I want to store different elements/paramters in one list/array/map/whatever..
the data to be stored looks like this:

id : answera, answerb, answerc, answerd, answere, answerf, answerg, answerh

id is a number

answers are booleans

so Ive got a lot of ids (well, maybe 20) and for each one 8 answers-booleans.
How do I store them the best, so that I can access them very easy again?

Thank you :-)

[EDIT] Thanks a lot for those answers, I will try it out now! :-)

I have now a map containing an id (int) and an object representing my answers (its a pojo which contains booleans answera, answerb, etc...)

Now I give this map to a gsp. How do I know get the data out of it? Thanks for help! :-)

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

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

发布评论

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

评论(2

赠意 2024-11-23 21:31:04

地图将是最好的方法,但它实际上与 grails 无关。您是否需要将它们保存到域类/数据库中?

地图会是什么样子...

def map = [:]
map.put(id1, [new Answer(accepted:true), new Answer(accepted:false)]; 
map.put(id2, [new Answer(accepted:false), new Answer(accepted:false)]; 

我认为这不会为您提供一个易于使用的域类。听起来您想要一个 grails 域类来封装答案。就像......

class Question{

    static hasMany = [answers:Answer]
    Integer id
    Boolean answered

    def hasBeenAnswered(){
        answers.each(){ answer->
            if (answer.accepted){
                answered = true;
                return true;
            }
        }
        return false;
    }

    def acceptAnwser(Answer answer){

        answer.accepted = true;
        this.answered = true;

    }


}

class Answer{

    static belongsTo = [question:Question]

    Integer id
    Boolean accepted
    String text
}

然后你的代码会更容易使用......

def allQuestion = Question.list();
def allUnansweredQuestions = Question.findAllByAnswered(false);
def allAnsweredQuestions = Question.findAllByAnswered(true);

A Map would be the best approach, however it really has nothing to do with grails. Do you need to persist these to a Domain Class/Database?

What a map would look like...

def map = [:]
map.put(id1, [new Answer(accepted:true), new Answer(accepted:false)]; 
map.put(id2, [new Answer(accepted:false), new Answer(accepted:false)]; 

I don't think this would give you an easy domain class to work with. Sounds like you would want a grails domain class to encapsulate the answers. Something like...

class Question{

    static hasMany = [answers:Answer]
    Integer id
    Boolean answered

    def hasBeenAnswered(){
        answers.each(){ answer->
            if (answer.accepted){
                answered = true;
                return true;
            }
        }
        return false;
    }

    def acceptAnwser(Answer answer){

        answer.accepted = true;
        this.answered = true;

    }


}

class Answer{

    static belongsTo = [question:Question]

    Integer id
    Boolean accepted
    String text
}

And then your code would be easier to use...

def allQuestion = Question.list();
def allUnansweredQuestions = Question.findAllByAnswered(false);
def allAnsweredQuestions = Question.findAllByAnswered(true);
逐鹿 2024-11-23 21:31:04

Map 看起来是一个显而易见的结构。映射的键应该是 id,映射的值应该是 List或(可能最好)封装这 8 个布尔值的类。

A Map seems like the obvious structure. The keys of the map should be the ids and the values of the Map should be either a List<Boolean> or (probably preferably) a class that encapsulates these 8 booleans.

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