Data binding 多端关联的数据绑定
假设我想将HTTP参数数据绑定到Data binding 多端关联的数据绑定,data-binding,grails,spring-mvc,groovy,propertyeditor,Data Binding,Grails,Spring Mvc,Groovy,Propertyeditor,假设我想将HTTP参数数据绑定到 class Continent { Integer id String name Country country } 其中,Country类类似于: class Country { Integer id String name Currency currency // other properties } 如果要将大陆.country绑定到已存在且可使用以下方法检索的国家的实例: interface CountryService
class Continent {
Integer id
String name
Country country
}
其中,Country
类类似于:
class Country {
Integer id
String name
Currency currency
// other properties
}
如果要将大陆.country
绑定到已存在且可使用以下方法检索的国家的实例
:
interface CountryService {
Country get(Integer countryId)
}
一种简单的方法是定义一个属性编辑器
,它可以将国家ID转换为相应的国家
实例,例如
public class ProductTypeEditor extends PropertyEditorSupport {
CountryService countryService // set this via dependency injection
void setAsText(String paramValue) {
if (paramValue)
value = countryService.get(paramValue.toInteger())
}
public String getAsText() {
value?.id.toString()
}
}
相反,如果我想数据绑定
class Continent {
Integer id
String name
Collection<Country> countries
}
类大陆{
整数id
字符串名
收集国
}
国家的ID以HTTP(数组参数)的形式发送。是否有任何简单的方法可以绑定
集合
,例如通过定义另一个属性编辑器
?属性编辑器只是字符串对象周围的包装器。您将不得不自行对数据进行封送和解封。就像你为国家所做的那样
我将创建一个这样做的服务
Collection getCountries(int[]id)
然后使用PropertyEditor拆分并使用您的服务。我认为你不会找到更好的解决办法。你可以这样做
void setAsText(String paramValue) {
ids = param.split(",")
// make each id an int
value = service.getCountries(ids)
}
为什么你要绑定到集合而不是大陆?我想你会想绑定到大陆id而不是国家列表?对吗?在本例中,您可以假设用户可以创建新大陆,但大陆可能只包含已创建的国家。您不能用逗号分隔国家代码并加载每个国家代码吗?如果每一个都被缓存,或者您使用了一个漂亮的单行SQL语句,那么您应该能够在一个请求中加载所有内容?或者您在哪里寻找更简单的方法?理想情况下,我希望编写一个
PropertyEditor
,它可以为我完成这项工作,就像它可以绑定单端关联一样