仅将某些属性绑定到 grails 域对象上?

发布于 2024-11-03 23:11:56 字数 467 浏览 4 评论 0原文

我有一个这样的地图:

['var1':'property1', 'var2':'3']

和这样的类:

class MyClass{
   MyEnum var1
   int var2
   String var3
}
enum MyEnum{
   PROP1( "property1" )
   PROP2( "property2" );

   private final String variable;

   public MyEnum( String variable ){ this.variable = variable }
   public String getVariable(){ return variable }
}

我想尝试将 var1var2 绑定到某个现有对象上以获取验证,但我该如何做到这一点?

I have a map like this:

['var1':'property1',
'var2':'3']

and a class like this:

class MyClass{
   MyEnum var1
   int var2
   String var3
}
enum MyEnum{
   PROP1( "property1" )
   PROP2( "property2" );

   private final String variable;

   public MyEnum( String variable ){ this.variable = variable }
   public String getVariable(){ return variable }
}

I'd like to just try to bind var1 and var2 onto some existing object to get the validations, but how do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

金橙橙 2024-11-10 23:11:56

您可以在控制器内使用bindData。此方法具有可选参数,允许您显式声明绑定应包含或排除哪些属性,例如,

def map = ['var1':'property1', 'var2':'3']
def target = new MyClass()

// using inclusive map
bindData(target, map, [include:['var1', 'var2']])

// using exclusive map
bindData(target, this.params, [exclude:['var2']])

如果您想在控制器外部进行此类绑定,请使用 org.codehaus.groovy 的方法之一。 grails.web.binding.DataBindingUtils,例如,

/**
 * Binds the given source object to the given target object performing type conversion if necessary
 *
 * @param object The object to bind to
 * @param source The source object
 * @param include The list of properties to include
 * @param exclude The list of properties to exclud
 * @param filter The prefix to filter by
 *
 * @return A BindingResult or null if it wasn't successful
 */
public static BindingResult bindObjectToInstance(Object object, Object source, List include, List exclude, String filter) 

下面是使用此方法执行与上面 bindData 的“包含映射”调用相同的绑定的示例

def map = ['var1':'property1', 'var2':'3']
def target = new MyClass()
DataBindingUtils.bindObjectToInstance(target, map, ['var1', 'var2'], [], null)

You can use bindData inside a controller. This method has optional arguments that allow you to explicitly state which properties should be included or excluded by the binding, e.g.

def map = ['var1':'property1', 'var2':'3']
def target = new MyClass()

// using inclusive map
bindData(target, map, [include:['var1', 'var2']])

// using exclusive map
bindData(target, this.params, [exclude:['var2']])

If you want to do this kind of binding outside a controller use one of the methods of org.codehaus.groovy.grails.web.binding.DataBindingUtils, e.g.

/**
 * Binds the given source object to the given target object performing type conversion if necessary
 *
 * @param object The object to bind to
 * @param source The source object
 * @param include The list of properties to include
 * @param exclude The list of properties to exclud
 * @param filter The prefix to filter by
 *
 * @return A BindingResult or null if it wasn't successful
 */
public static BindingResult bindObjectToInstance(Object object, Object source, List include, List exclude, String filter) 

Here's an example of using this method to perform the same binding as the "inclusive map" invocation of bindData above

def map = ['var1':'property1', 'var2':'3']
def target = new MyClass()
DataBindingUtils.bindObjectToInstance(target, map, ['var1', 'var2'], [], null)
守不住的情 2024-11-10 23:11:56

您可以使用 bindData 方法,这样您就可以指定哪些属性您想要绑定哪些或不想绑定哪些。

You can use the bindData method for this, so you can specify which properties you want to bind or which ones you don't.

踏雪无痕 2024-11-10 23:11:56

你也可以尝试

Map myClassMap = ['var1':'property1', 'var2':'3']
//You can use either of the following options
MyClass myClass = new MyClass(myClassMap) 
myClass.properties = myClassMap

You can also try

Map myClassMap = ['var1':'property1', 'var2':'3']
//You can use either of the following options
MyClass myClass = new MyClass(myClassMap) 
myClass.properties = myClassMap
一个人的夜不怕黑 2024-11-10 23:11:56

您还可以使用以下语法:

// update the var1 and var2 properties in the target
target.properties['var1', 'var2'] = params

使用 Grails 进行安全数据绑定

You can also use the following syntax:

// update the var1 and var2 properties in the target
target.properties['var1', 'var2'] = params

As described in Code Listing 4 of Secure Data Binding With Grails

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文