我是Grails的新手,并通过InfoQ的“Grails入门”一书进行研究。 通过它我遇到了BootStrap.groovy文件的问题:
我尝试了什么:
即使在我更改了Race类的bootstrapping值后,它看起来还没有被更改。
看起来该值取自BootStrap.groovy以外的其他地方?
我将Grails 2.2.1与PostgreSQL DB一起使用。 课程代码如下:
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')}"
}
}
答案 0 :(得分:0)
BootStrap
应该在grails-app/conf
中。请参阅this。
答案 1 :(得分:0)
尝试通过添加
来打印您保存的新实例的错误print begun.errors
看看是否出现了一些事情。
答案 2 :(得分:0)
您需要删除
def index() { }
来自控制器。
我有同样的问题。现在它已修复。