Grails 控制器和域
我只是有一个简单的问题,因为我仍在学习 grails。我有一个控制器类,它只读取文本文件并加载包含信息的列表。我想通过此列表填充我的域之一。我可以这样做吗?
例如,我有一个 bookController,控制器会执行类似的操作,
//psuedocode
import bookdemo.book
readFile(bookData) into list
int a = list.size
a.times {
new Book(Author:"$list.author", Title:"$list.title").save()
}
由于某种原因,它不会将其添加到列表中。我继续查看 grails,但没有找到任何相关内容,或者我错过了它。当我打印列表时,它已被填充。当我打印这本书时,它会打印出“bookdemo.book”。
如果这是非常基本的,我很抱歉,在发布这个问题的同时,我将继续挖掘 grails.org。
I just had a quick question as I'm still learning grails. I have a controller class that just reads a text file and loads a list with information. I'm wanting to populate one of my domains through this list. Am I able to do it like this?
For instance, I have a bookController, the controller does something like
//psuedocode
import bookdemo.book
readFile(bookData) into list
int a = list.size
a.times {
new Book(Author:"$list.author", Title:"$list.title").save()
}
For some reason it doesn't add it to the list. I went ahead and looked through grails but I didn't find anything relevant or I missed it. When I print the list, it's populated. When I print the book it prints out 'bookdemo.book'
My apologies if this is very basic, I'm going to continue digging through grails.org while this question is posted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为无论 readFile 返回什么(地图?一本书?),您都需要迭代列表。你所拥有的没有任何方法来索引列表。我不知道 $list.author 可以解决什么问题。
假设 readFile 返回一个 Map 我认为类似的东西
是在你想要的方向上(尽管我的闭包语法可能是错误的)。
I think whatever readFile returns (a map? a Book?), you need to iterate through the list. What you have doesn't have any way of indexing the list. I don't see what $list.author can resolve to.
Assuming readFile returns a Map I would think something like
is in the direction of what you want (though my closure syntax is probably wrong).