【问题标题】:Grails validation not workingGrails 验证不起作用
【发布时间】:2015-06-07 10:50:32
【问题描述】:

我正在尝试对域类对象进行一些验证,但是当我调用“验证”方法时出现了一个奇怪的异常。我有一个输入一些数据的表格: 这是我的表单的代码:

<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 的方法是“create”。

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()”时抛出异常。我不明白我在这里做错了什么。提前致谢!

【问题讨论】:

  • 你试过添加getter和setter吗?
  • 它有效。 Grails 不应该默认生成这些方法吗?如果我没记错的话,我可以为域类的对象调用 setter 和 getter 方法,而无需编写这些方法。我仍在学习 Grails,并且不时犯一些愚蠢的错误。发布您的答案,以便我将其标记为正确答案。
  • 您使用的是哪个版本的 Grails?

标签: validation grails domain-object


【解决方案1】:

您应该将 getter 和 setter 添加到您的域类中以删除它。

Grails 编译器默认会生成这些方法,例如。

myObject.myValue = "hello"; 

一样
myObject.setMyValue("hello");

但是,它显然并不完美,并且会出现这样的错误!如果您使用的是 Jira Issue 解决此问题,因此您可以通过升级您的 Grails 版本来解决它。

【讨论】:

  • 问题是我使用的是 Grails 2.4.4。这有点奇怪,但它奏效了,所以我很高兴。
  • 嗯.. 是的,这很奇怪。也许你应该对这个问题发表评论!
猜你喜欢
  • 1970-01-01
  • 2012-05-26
  • 2017-02-13
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多