初学者 Groovy
我正在遵循 Graeme Keith Rocher 所著的《Grails 权威指南》中的代码示例,并且遇到了一个相当不寻常的绊脚石。
本质上,存在 2 个域类 - 书签和书签。标签。
书签:
class Bookmark {
static hasMany = [tags:Tag]
URL url
String title
String notes
Date dateCreated = new Date()
}
标签:
class Tag{
static belongsTo= Bookmark
Bookmark bookmark
String name
}
我被指示启动 Grails 控制台(这与 groovy 控制台相同)并创建一个新对象,如下所示。
def b = new Bookmark(url: new URL('http://grails.org/'), title:'Grails', notes:'Groovy')
结果是:
Result: Bookmark : null
根据本书,GORM 自动提供了 addTag 方法的实现。所以我编码......
b.addTag( new Tag(name: 'grails'))
只是为了得到错误消息:
Exception thrown: No such property: b for class: ConsoleScript1
groovy.lang.MissingPropertyException: No such property: b for class: ConsoleScript1 at ConsoleScript1.run(ConsoleScript1:2)
作者没有在书中解释这一点。我想知道是否有人可以帮助我?
谢谢。
I'm following the code examples in 'The Definitive Guide to Grails' by Graeme Keith Rocher, and have come across a rather unusual stumbling block.
Essentially, 2 domain classes exist - Bookmark & Tag.
Bookmark:
class Bookmark {
static hasMany = [tags:Tag]
URL url
String title
String notes
Date dateCreated = new Date()
}
Tag:
class Tag{
static belongsTo= Bookmark
Bookmark bookmark
String name
}
I'm instructed to launch the Grails Console (is this the same as the groovy console)and create a new object as follows.
def b = new Bookmark(url: new URL('http://grails.org/'), title:'Grails', notes:'Groovy')
This results in:
Result: Bookmark : null
According to the book, GORM automatically provides an implementation of an addTag method. So I code...
b.addTag( new Tag(name: 'grails'))
Only to get whammed with the error message:
Exception thrown: No such property: b for class: ConsoleScript1
groovy.lang.MissingPropertyException: No such property: b for class: ConsoleScript1 at ConsoleScript1.run(ConsoleScript1:2)
The author hasn't accounted for this in the book. I was wondering if anyone could help me out?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在读这本书的第一版吗?如果是这样,那就太过时了。自 0.5 起,add* 方法已被弃用。它已被 addTo* 取代,因此请这样做:
假设您的代码示例不应定义两次书签(复制和粘贴错误?)并且标签可能如下所示:
Are you reading the 1st edition of the book? If so it's quite outdated. The add* methods have been deprecated since 0.5. It was replaced by addTo* so do this instead:
Assuming your code example shouldn't have Bookmarks defined twice (copy and paste error?) and Tag might look like this:
groovy 控制台与 grails 控制台不同。要访问 grails 控制台,请在应用程序目录中输入
grails console
- 您应该会获得一个 Java GUI 应用程序。该示例可能会起作用,因为 grails 向标准 Groovy 添加了一些内容。另外,您的问题不是 addTag 方法,而是您定义的项目
b
无法找到。尝试将整个脚本立即输入控制台并执行它,而不是逐行执行它。The groovy console is not the same as the grails console. To access the grails console, type
grails console
in your application directory - you should get a Java GUI app. It's possible that the example will work then because grails add some stuff to the standard Groovy.Also, your problem isn't the addTag method, but the item
b
that you defined which cannot be found. Try entering the whole script into the console at once and executing it, instead of executing it line by line.