当我提交表单时,从 Register.gsp
页面可以很好地呈现该表单,然后转到列表页面。但是问题是它不保存任何数据。如果我通过dbconsole
添加数据,则 list.gsp
显示数据。可能是个愚蠢的问题,但是我是Grails的初学者。提前致谢。
域类:
package userreg
class Customer {
String name
Date birthday
String gender
String email
static constraints = {
name blank: false
email blank: false,unique:true
}
}
Controller :
package userreg
class CustomerController {
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
def index ={
render(view:'register')
}
def register()
{
}
def save ={
def customer=new Customer(params)
customer.save flush:true
redirect action:"list"
}
def list()
{
def customers=Customer.list()
[customers:customers]
}
}
查看-注册:
register.gsp
<!doctype html>
<head>
<title>Registration </title>
</head>
<body>
<div class="body">
<g:form controller="customer" action="save" >
<table>
<tr><td>Name</td><td><g:textField name="name"/> </td></tr>
<tr><td>Birthday</td><td><g:datePicker name="date" value="${new Date()}"
noSelection="['':'-Choose-']"/></td></tr>
<tr><td>Gender</td><td><g:radio name="gender" value="female"/>Female
<g:radio name="gender" value="male"/>Male</td></tr>
<tr><td>Email</td><td><g:textField name="email" value="user@email.com"/>
</td></tr>
<tr><td></td><td><g:submitButton name="save" value="save" /> </td></tr>
</table>
</g:form>
<div>
</body>
</html>
列表-list.gsp:
<!doctype html>
<head>
<title>List of Customers </title>
</head>
<body>
<table border=1>
<tr>
<th>Name</th>
<th>Gender</th>
<th> Birthday</th>
</tr>
<g:each in="${customers}" var="customer">
<tr>
<td>${customer.name}</td>
<td>${customer.gender}</td>
<td>${customer.birthday}</td>
</tr>
</g:each>
</table>
</body>
</html>
最佳答案
您的模型未保存,可能是因为它存在验证错误。
更改
def save ={
def customer=new Customer(params)
customer.save flush:true
redirect action:"list"
}
对此
def save ={
def customer=new Customer(params)
customer.save flush:true, failOnError:true
redirect action:"list"
}
它将引发错误,说明无法保存模型的原因。
关于database - 无法手动将数据保存在Grails中的数据库中,但是使用dbconsole可以很好地工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46159688/