将元素保存在 Map/Array/Collection ...... Grails
我是 grails 1.3.7
的新手,我遇到了问题。
我想将不同的元素/参数存储在一个 list/array/map/
中。 要存储的数据如下所示:
id:answera,answerb,answerc,answerd,answere,answerf,answerg,answerh
id
是一个数字
answers
是 booleans
,
所以我有很多 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
地图将是最好的方法,但它实际上与 grails 无关。您是否需要将它们保存到域类/数据库中?
地图会是什么样子...
我认为这不会为您提供一个易于使用的域类。听起来您想要一个 grails 域类来封装答案。就像......
然后你的代码会更容易使用......
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...
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...
And then your code would be easier to use...
Map
看起来是一个显而易见的结构。映射的键应该是 id,映射的值应该是 ListA
Map
seems like the obvious structure. The keys of the map should be the ids and the values of the Map should be either aList<Boolean>
or (probably preferably) a class that encapsulates these 8 booleans.