加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

重构Grails命令验证

发布时间:2020-12-14 16:33:00 所属栏目:大数据 来源:网络整理
导读:在我的应用程序中,我有很多代码如下: def add(CreatePersonCommand command) { if(command.hasErrors()) { redirect(action: 'add') return }} 我不喜欢这种重复,所以我想重构它.我希望Grails会有如下内容: @Validate(action:'someAction')def add(CreateP
在我的应用程序中,我有很多代码如下:

def add(CreatePersonCommand command) {
    if(command.hasErrors()) {
        redirect(action: 'add')
        return
    }
}

我不喜欢这种重复,所以我想重构它.我希望Grails会有如下内容:

@Validate(action:'someAction')
def add(CreatePersonCommand command) {

}

要么

def add(@Valid CreatePersonCommand command) {
}

哪个会自动验证命令,并在出现错误时重定向到GSP.我尝试用拦截器和过滤器创建这样的东西,但我失败了,因为我无法访问过滤器中的操作和命令.

也许我错过了一些东西,但有没有更好的方法来处理这个问题,或者实现类似上面的东西?

解决方法

考虑到问题中的评论,我提出了一个时间性的解决方法.

有一个像这样的命令:

@grails.validation.Validateable
class FooCommand implements Serializable {
    String username
    String email
    static constraints = {
        username nullable: false,blank: false,minSize: 6
        email email: true,nullable: false,blank: false
    }
}

并且具有动作索引的Controller仅在params验证命令对象时执行,并且在验证错误的情况下重定向到动作错误:

class FooController {
    def index() {
        render 'ok!'
    }
    def error() {
        render 'errors = ' + params.errors
    }
}

然后,您可以为所有请求(或您想要的请求)定义过滤器,并根据请求尝试的控制器和操作,您可以使用所需的参数验证命令对象,如下所示:

class FooFilters {
    def filters = {
        all(controller:'*',action:'*') {
            before = {
                if (params.controller == 'foo' && (!params.action || params.action == 'index')) {
                    FooCommand cmd = new FooCommand(params.subMap(['username','email']))
                    if (!cmd.validate()) {
                        redirect controller: 'foo',action: 'error',params: [errors:cmd.errors]
                        return false
                    }
                }
                return true
            }
        }
    }
}

可以通过多种方式改进此代码.但我认为这足以作为一个示例解决方案.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读