您可以使用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)
一個巨大的警告:如果分別綁定父對象和容器對象,容器對象上產生的錯誤不會得到波及到父母。 – 2011-04-28 17:22:17