2015-04-02 211 views
1

我想對域類對象做一些驗證,但是當我調用'validate'方法時,我得到一個奇怪的異常。我有一個表格,我輸入一些數據: Html form 這裏是我的表單代碼:Grails驗證不起作用

<body> 
<div id='register'> 
    <div class='inner'> 
     <div class='fheader'>Register</div> 

     <g:if test='${flash.message}'> 
      <div class='login_message'>${flash.message}</div> 
     </g:if> 

     <form action='<g:createLink controller="register" action="create" />' method='POST' id='registerForm' class='cssform' autocomplete='off'> 
      <p> 
       <label for='username'>Username:</label> 
       <input type='text' class='text_' name='username' id='username'/> 
      </p> 

      <p> 
       <label for='password'>Password:</label> 
       <input type='password' class='text_' name='password' id='password'/> 
      </p> 

      <p> 
       <label for='passwordConfirmation'>Confirm:</label> 
       <input type='password' class='text_' name='passwordConfirm' id='passwordConfirmation'/> 
      </p> 

      <p> 
       <input type='submit' id="submit" value='REGISTER'/> 
      </p> 
     </form> 
    </div> 
</div> 
</body> 

這裏是我的控制器代碼。接收POST的方法是'創建'。

package genealogicaltree 

import gentree.security.UserCommand 
import grails.converters.JSON 
import org.springframework.security.access.annotation.Secured 

@Secured('permitAll') 
class RegisterController { 

    static allowedMethods = [index:'GET', create:['POST']] 

    def springSecurityService 

    def index() { 
     render(view: 'register') 
    } 

    def create() { 
     def userCommand = new UserCommand(params) 
     if(userCommand.validate()) 
      render "valid" 
     else 
      render "invalid" 
    } 
} 

這裏是我的域類看起來像:

class UserCommand{ 
    public String username 
    public String password 
    public String passwordConfirm 
    static constraints = { 
     username size: 6..15, blank: false 
     password size: 6..15, blank: false 
     passwordConfirm blank: false 
    } 
} 

使用調試我可以看到,從POST數據綁定到userCommand對象: enter image description here

不過,我得到這樣的錯誤信息: enter image description here

這裏是控制檯輸出:

Error | 
2015-04-02 15:41:57,180 [http-bio-8080-exec-7] ERROR errors.GrailsExceptionResolver - NotReadablePropertyException occurred when processing request: [POST] /GenealogicalTree/register/create - parameters: 
passwordConfirm: password 
username: username 
password: *** 
Invalid property 'username' of bean class [gentree.security.UserCommand]: Bean property 'username' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?. Stacktrace follows: 
Message: Invalid property 'username' of bean class [gentree.security.UserCommand]: Bean property 'username' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? 
    Line | Method 
->> 20 | create in genealogicaltree.RegisterController 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
| 198 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter 
|  63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter 
|  53 | doFilter in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter 
|  49 | doFilter in grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter 
|  82 | doFilter in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter 
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor 
| 615 | run  in java.util.concurrent.ThreadPoolExecutor$Worker 
^ 745 | run . . . in java.lang.Thread 

代碼調用'userCommand.validate()'時引發異常。我不明白我在這裏做錯了什麼。提前致謝!

+1

你有沒有嘗試添加getters和setter? – christopher 2015-04-02 20:51:56

+0

它的工作原理。 Grails不應該默認生成這些方法嗎?如果我記住了正確的話,我可以爲域類的對象調用setter和getters方法,而無需編寫方法。我仍然在學習Grails,並且時不時地犯下愚蠢的錯誤。發佈您的答案,以便我可以將其標記爲正確的答案。 – Zan 2015-04-02 21:14:07

+0

您使用的是哪個版本的Grails? – christopher 2015-04-02 21:32:41

回答

1

您應該將getter和setter添加到您的域類中以刪除此項。

默認情況下,Grails編譯器將生成這些方法,例如。

myObject.myValue = "hello"; 

相同

myObject.setMyValue("hello"); 

然而,這顯然是不完美的,你會得到這樣的錯誤!如果您使用的是Grails版本< 2.3.2,那麼這是一個已知的bug,並且它有一個Jira Issue,所以您可以通過升級您的Grails版本來解決它。

+0

事情是我正在使用Grails 2.4.4。這有點奇怪,但它工作,所以我很高興。 – Zan 2015-04-02 21:38:57

+0

呃..是的,這很奇怪。也許你應該評論這個問題! – christopher 2015-04-02 21:46:42