我有一個應用程序,用戶可以填寫一張表格,並保存一些預設置爲快速重新人口Grails的選擇不設置默認值
域
class Person {
String name
TeenageMutantNinjaTurtle favorite
static constraints = {
name blank:false, unique:true
favorite nullable:true
}
@Override
public String toString() { name }
}
package tmnt
class TeenageMutantNinjaTurtle {
String name
String colorHeadband
static constraints = {
name inList:["Leonardo", "Donatello", "Raphael", "Michelangelo"]
colorHeadband inList:["blue", "purple", "red", "orange" ]
}
@Override
public String toString() { "${name}" }
}
控制器
class PersonController {
def choose = {
if(session.user) {
def person = Person.findByName(session.user.username)
[
teenageMutantNinjaTurtleInstanceList: TeenageMutantNinjaTurtle.list(),
person : person,
favorite : person.favorite
]
}
}
def pickFavoriteTurtle = { TurtleCommandObject tut ->
def turtle = tut.turtleName
def choice = TeenageMutantNinjaTurtle.findByName(turtle)
String message = "Turtle not chosen "
if(choice){
def person = Person.findByName(tut.personName)
person.favorite = choice
person.save()
message = "Made ${person}'s favorite turtle ${choice}"
}
else {
message += "could not find ${choice}"
}
render message
}
查看
<div>
<h1>Hello ${person} </h1>
<g:form action="pickFavoriteTurtle">
<g:hiddenField name="personName" value="${person}" />
<g:select name="turtleName" from="${teenageMutantNinjaTurtleInstanceList}" value="${favorite}" />
<g:submitToRemote name="pickFavoriteTurtle"
url="[action:'pickFavoriteTurtle']"
value="Save your favorite turtle" />
</g:form>
</div>
最喜歡的是從來沒有最初選擇的價值,即使我可以證明,它的計算結果等於TRUE作爲User guide描述。是什麼賦予了?
爲什麼是「PERSONNAME」字段設置爲$ {}人的價值?此字段是否正確填充?該視圖可能正在吐出錯誤並忽略填充選擇字段。用於數據綁定的 – therin
。該字段確實填寫正確,並不是問題。 –