如何从过滤器访问命令对象,它有可能吗? [英] How to access command objects from filter and is it possible at all?

查看:122
本文介绍了如何从过滤器访问命令对象,它有可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常写这样的内容:

 


def myAction {MyActionCommand cmd - >
if(cmd.hasErrors()){
return render(status:HttpServletResponse.SC_BAD_REQUEST);
} else {
//实际行动逻辑
}


因此,我想将这种常见模式提取到一些可重用的位置。过滤器看起来很不错,但是我找不到从过滤器获取命令对象的方式。尝试像这样(在过滤器封闭):

 


formValidation(控制器: '*',action:'*'){
before = {cmd - >
if(cmd.hasErrors()){
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
返回false;
} else {
return true;















<在Grails 1.3.7兼容解决方案中穿插。是否有可能?

解决方案

不,不可能做你正在问的问题。 Command对象不是像Controller,Service等完整的框架构件,因此它们不会将其验证逻辑添加到它们中,除非它们是Controller操作的参数。为此,过滤器中的Command对象不会有 .validate() .hasErrors()方法作为另一种选择,您可以使用 @Validateable 注释:



http://grails.org/doc/latest/guide/7.%20Validation.html#7.5%20Validation%20Non%20Domain%20and%20Command%20Object%20Classes



将您的命令对象作为常规的Groovy类移动到 src / groovy ,并用 @Validateable 。然后在你的过滤器中你可以这样做:

$ $ $ $ $ $ $ $ $ $ $ $ def $ valid $ ()){
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
返回false;
} else {
return true;
}

确保将可验证类的包名添加到 grails.validateable.packages 在Config.groovy中列出。


I often write something like:



    def myAction{ MyActionCommand cmd ->
       if( cmd.hasErrors() ){
          return render(status:HttpServletResponse.SC_BAD_REQUEST );
       }else{
          // actual action logic
       }


So, I'd like to extract that common pattern into some reusable location. Filter looks like good candidate, but I can't find the way to get command object from the filter. Tryed something like this (in filters closure):



    formValidation( controller:'*', action:'*' ){
       before = { cmd ->
          if( cmd.hasErrors() ){
              response.sendError( HttpServletResponse.SC_BAD_REQUEST );
              return false;
          }else{
              return true;
          }
       }
    }


Intersted in grails 1.3.7 compatible solution. Is it possible at all?

解决方案

No, it isn't possible to do what you are asking. Command Objects are not full framework artifacts like Controller, Service, etc, and so they do not get their validation logic added to them, unless they are a parameter to a Controller action. To that end a Command Object in a filter wouldn't have a .validate() or .hasErrors() method to check against.

As another option you could use the @Validateable annotation:

http://grails.org/doc/latest/guide/7.%20Validation.html#7.5%20Validation%20Non%20Domain%20and%20Command%20Object%20Classes

Move your Command Object to src/groovy as a regular Groovy class and annotate it with @Validateable. Then in your filter you can do:

def validObj = new MyValidateable(params)
if (!validObj.validate()) {
    response.sendError( HttpServletResponse.SC_BAD_REQUEST );
    return false;
} else {
    return true;
}

Make sure you add the package name of your validateable class to the grails.validateable.packages List in Config.groovy.

这篇关于如何从过滤器访问命令对象,它有可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆