是否可以在 Grails 中为生成的(脚手架的)状态下拉框设置默认值?我希望这是我可以在域类或控制器类中做的事情:
class State {
String name
String code
static mapping = { sort name: "asc" }
String toString() {
return this.name;
}
}
class Person implements Comparable {
String firstName
String lastName
State state
String toString() {
return this.lastName + ", " + this.firstName;
}
}
我已经尝试在 State 域类中显式设置String name="Pennsylvania"
和但没有运气。String code="PA"
我在 Bootstrap.groovy 中引导状态数据:
def init = { servletContext ->
if (!State.count()) {
new State(name: "Alabama", code:"AL").save(failOnError: true)
new State(name: "Alaska", code:"AK").save(failOnError: true)
new State(name: "Arizona", code:"AZ").save(failOnError: true)
new State(name: "Arkansas", code:"AR").save(failOnError: true)
....
}
}
更新:
PersonController.groovy:
def create() {
// [personInstance = new Person(params)]
def personInstance = new Person(params)
personInstance.state = new State("Pennsylvania", "PA")
[personInstance: personInstance]
}
状态.groovy:
public State(String name, String code) {
this.name = name
this.code = code
}