在我的应用程序中,我有很多代码如下:
def add(CreatePersonCommand command) {
if(command.hasErrors()) {
redirect(action: 'add')
return
}
}
Run Code Online (Sandbox Code Playgroud)
我不是这种重复的粉丝,所以我想重构它.我希望Grails会有如下内容:
@Validate(action:'someAction')
def add(CreatePersonCommand command) {
}
Run Code Online (Sandbox Code Playgroud)
要么
def add(@Valid CreatePersonCommand command) {
}
Run Code Online (Sandbox Code Playgroud)
哪个会自动验证命令,并在出现错误时重定向到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
}
}
Run Code Online (Sandbox Code Playgroud)
控制器只有在参数验证命令对象时才执行操作,以及在验证错误时重定向到的index操作:error
class FooController {
def index() {
render 'ok!'
}
def error() {
render 'errors = ' + params.errors
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以为所有请求(或您想要的请求)定义一个过滤器,并根据请求尝试进行的控制器和操作,您可以使用所需的参数验证命令对象,如下所示:
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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
可以通过多种方式改进此代码。但我认为作为示例解决方案就足够了。