Groovy/Grails使用映射作为函数的参数 [英] Groovy / Grails using a map as a parameter of a function

查看:88
本文介绍了Groovy/Grails使用映射作为函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的函数,定义如下:

 def mySimpleFunction(Map myMap) {
   // Function logic here...
  }

但是,当我尝试对此进行编译时,会收到警告消息并生成异常消息,该异常消息指出:[mySimpleFunction]操作接受类型为[java.util.Map]的参数,该参数未标记为Validateable. /p>

如何将该功能标记为可验证?我导入了 org.codehaus.groovy.grails.validation.Validateable

,并将我的课程标记为@Validateable.

要构建我的应用程序,我应该做些什么?

提前谢谢!

解决方案

如果它是一个辅助方法,请将其设为私有.在Grails 2.0+中,公共控制器方法被假定为动作,而参数被认为是可绑定的.这意味着它们必须是数字类型,布尔值,字符串等,或者是命令对象类.

如果在控制器类文件中定义了命令对象类,并且在其他地方定义了命令对象类,则需要将其注释为@Validateable.

由于这是一个辅助方法,而不是操作,所以只需将其设为私有(特别是因为无论如何不能从另一个类调用它):

private mySimpleFunction(Map myMap) {
   // Function logic here...
}

I have a very simple function which I define as follows:

 def mySimpleFunction(Map myMap) {
   // Function logic here...
  }

However, when I try to compile this, I get a warning message and build exception which says that: The [mySimpleFunction] action accepts a parameter of type [java.util.Map] which has not been marked with Validateable.

How can I mark this function as Validateable? I imported the org.codehaus.groovy.grails.validation.Validateable

and have marked my class as @Validateable .

What should I be doing differently in order to get my application to build?

Thank you in advance!

解决方案

If it is a helper method, make it private. In Grails 2.0+ public controller methods are assumed to be actions, and arguments are assumed to be bindable. That means they need to be number types, boolean, String, etc., or a command object class.

Command object classes are automatically made validateable if they're defined in the controller class file, and if they're defined elsewhere they need to be annotated as @Validateable.

Since this is a helper method and not an action, just make it private (especially since it can't be called from another class anyway):

private mySimpleFunction(Map myMap) {
   // Function logic here...
}

这篇关于Groovy/Grails使用映射作为函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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