gpt4 book ai didi

grails - Grails设置脚手架下拉框的默认值

转载 作者:行者123 更新时间:2023-12-02 15:34:46 27 4
gpt4 key购买 nike

是否可以在Grails中为生成的(脚手架)状态下拉框设置默认值?我希望这是我可以在域类或 Controller 类中执行的操作:

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]
}

State.groovy:
public State(String name, String code) {
this.name = name
this.code = code
}

最佳答案

如果要为“新Person”页面设置状态下拉列表的默认值,则可能要使Person的“state”成员的默认值为在Bootstrap中创建的“Pennsylvania”状态。

实际上,我认为一种更好的方法是实际生成PersonController,然后修改“create”闭包以设置Person.state的初始值。

关于grails - Grails设置脚手架下拉框的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13354674/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com