我想对域类对象做一些验证,但是当我调用'validate'方法时,我得到一个奇怪的异常。我有一个表格,我输入一些数据: 这里是我的表单代码: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对象:
不过,我得到这样的错误信息:
这里是控制台输出:
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()'时引发异常。我不明白我在这里做错了什么。提前致谢!
你有没有尝试添加getters和setter? – christopher 2015-04-02 20:51:56
它的工作原理。 Grails不应该默认生成这些方法吗?如果我记住了正确的话,我可以为域类的对象调用setter和getters方法,而无需编写方法。我仍然在学习Grails,并且时不时地犯下愚蠢的错误。发布您的答案,以便我可以将其标记为正确的答案。 – Zan 2015-04-02 21:14:07
您使用的是哪个版本的Grails? – christopher 2015-04-02 21:32:41