Grails 控制器和域

发布于 2024-09-18 19:58:23 字数 472 浏览 3 评论 0原文

我只是有一个简单的问题,因为我仍在学习 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 技术交流群。

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

发布评论

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

评论(1

橘香 2024-09-25 19:58:23

我认为无论 readFile 返回什么(地图?一本书?),您都需要迭代列表。你所拥有的没有任何方法来索引列表。我不知道 $list.author 可以解决什么问题。

假设 readFile 返回一个 Map 我认为类似的东西

list.each { mymap => new Book(mymap).save() }

是在你想要的方向上(尽管我的闭包语法可能是错误的)。

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

list.each { mymap => new Book(mymap).save() }

is in the direction of what you want (though my closure syntax is probably wrong).

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