仅将某些属性绑定到 grails 域对象上?
我有一个这样的地图:
['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 }
}
我想尝试将 var1
和 var2
绑定到某个现有对象上以获取验证,但我该如何做到这一点?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在控制器内使用
bindData
。此方法具有可选参数,允许您显式声明绑定应包含或排除哪些属性,例如,如果您想在控制器外部进行此类绑定,请使用 org.codehaus.groovy 的方法之一。 grails.web.binding.DataBindingUtils,例如,
下面是使用此方法执行与上面
bindData
的“包含映射”调用相同的绑定的示例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.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.Here's an example of using this method to perform the same binding as the "inclusive map" invocation of
bindData
above您可以使用 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.
你也可以尝试
You can also try
您还可以使用以下语法:
如使用 Grails 进行安全数据绑定
You can also use the following syntax:
As described in Code Listing 4 of Secure Data Binding With Grails