2017-09-11 60 views
0

Register.gsp頁面當我提交表單時,它呈現良好並進入列表頁面。但問題是它不保存任何數據。如果我通過dbconsole添加數據,則list.gsp顯示數據。可能是愚蠢的問題,但我在Grails中很初學。提前致謝。無法手動將數據保存到Grails中的數據庫中,但使用dbconsole時效果不錯

域類:

package userreg 

class Customer { 

String name 
Date birthday 
String gender 
String email 


static constraints = { 
    name blank: false 
    email blank: false,unique:true 
    } 
} 

控制器:

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="[email protected]"/> 
    </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> 
+0

你的問題是什麼?你想做什麼,你嘗試了什麼,你得到了什麼結果?更新問題的主體。提醒:這裏沒有人想要爲你調試你的代碼。你需要表明你願意做這項工作。 – jdv

回答

0

你的模型沒有可能節省,因爲它有驗證錯誤。

變化

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

它會拋出一個錯誤解釋爲何沒能保存模型的原因。

+1

更好的辦法是檢查調用'.save(...)'的返回值,或者檢查'errors'屬性。 –