我是 Grails 的新手,通过 InfoQ 的“Grails 入门”一书学习它。运行它时,我遇到了 BootStrap.groovy 文件的问题:
- 我已经按照书上说的进行了编辑。
- 运行应用程序 - 我输入到 BootStrap.groovy 的数据已显示。
- 我已经更新了引导值,但它们没有改变。控制台中未显示任何错误。
我试过的:
- 交换def的位置;
- 将 Runner 的类值设置为可空值;
- 将 .save(failOnError:true) 更改为 .save(flush:true)。
即使在我更改了 Race 类的引导值之后,它看起来也没有改变。
看起来该值是从 BootStrap.groovy 以外的其他地方获取的?
我将 Grails 2.2.1 与 PostgreSQL 数据库一起使用。类的代码如下:
package racetrack
class Runner {
static constraints = {
firstName(blank:false)
lastName(blank:false)
dateOfBirth(nullable:true)
gender(inList:["M","F"])
address(nullable:true)
city(nullable:true)
state(nullable:true)
zipcode(nullable:true)
email(email:true)
}
static hasMany = [registrations:Registration]
/*static mapping = {
sort "email"
}*/
String firstName
String lastName
Date dateOfBirth
String gender
String address
String city
String state
String zipcode
String email
String toString(){
"${firstName} ${lastName} (${email})"
}
}
和
package racetrack
class BootStrap {
def init = { servletContext ->
def begun = new Runner(firstName:"Marathon",
lastName:"Runner",
dateOfBirth:"",
gender:"M",
address:"",
city:"",
state:"",
zipcode:"",
email:"me@me.ru"
)
begun.save(flush:true)
def zabeg = new Race(name:"Run SPB",
startDate:new Date() + 360*30,
city:"Moscow",
state:"Moscow",
cost:10.0,
distance:42.0,
maxRunners:10)
zabeg.save(flush:true)
}
def destroy = {
}
}
编辑:这可能是由于generate-*
运行了任何脚本而发生的吗?
Race.groovy:
package racetrack
class Race {
static hasMany = [registrations:Registration]
String name
Date startDate
String city
String state
BigDecimal distance
BigDecimal cost
Integer maxRunners
static constraints = {
name(blank:false, maxSize:50)
startDate(validator: {
return (it >= new Date())
}
)
city()
state(inList:["Moscow", "Volgograd", "SPb", "NN"])
distance(min:0.0)
cost(min:0.0, max:100.0)
maxRunners(min:0, max:100000)
}
static mapping = {
sort "startDate"
}
BigDecimal inMiles(){
return distance*0.6214
}
String toString(){
return "${name}, ${startDate.format('MM/dd/yyyy')}"
}
}