Data binding Grails从URL数据绑定到映射属性。自定义数据绑定器从未被调用
我正在开发一个简单的购物车应用程序,它需要能够在URL中传递特定产品的数量。我设想通过在命令对象上使用map属性来保持数据绑定的简单性。但是,当我用一个参数点击我的操作时,我会得到以下错误:Data binding Grails从URL数据绑定到映射属性。自定义数据绑定器从未被调用,data-binding,grails,Data Binding,Grails,我正在开发一个简单的购物车应用程序,它需要能够在URL中传递特定产品的数量。我设想通过在命令对象上使用map属性来保持数据绑定的简单性。但是,当我用一个参数点击我的操作时,我会得到以下错误: ERROR errors.GrailsExceptionResolver - Exception occurred when processing request: [GET] /mygrailsapp/action itemQty[123].id: 5 java.lang.NullPointerExce
ERROR errors.GrailsExceptionResolver - Exception occurred when processing request: [GET] /mygrailsapp/action
itemQty[123].id: 5
java.lang.NullPointerException
at grails.plugin.springcache.web.GrailsFragmentCachingFilter.doFilter(GrailsFragmentCachingFilter.groovy:66)
at net.sf.ehcache.constructs.web.filter.Filter.doFilter(Filter.java:86)
at com.infusionsoft.cam.security.filter.BlackListIpAddressFilter.doFilter(BlackListIpAddressFilter.java:78)
at java.lang.Thread.run(Thread.java:662)
命令对象如下所示:
class MyCommand {
Map itemQty
static constraints = {
itemQty(nullable: true, blank: true)
}
}
def action = {MyCommand myCommand ->
// some code
}
控制器操作如下所示:
class MyCommand {
Map itemQty
static constraints = {
itemQty(nullable: true, blank: true)
}
}
def action = {MyCommand myCommand ->
// some code
}
我点击的一个示例url是/mygrailsapp/action?itemQty[123]=5
我遵循Grails文档中的示例将数据绑定到地图,唯一的区别是我尝试从URL而不是像文档中的post那样进行绑定。我正在使用Grails1.3.7。我尝试过对括号-[和]-进行编码,但我得到了相同的错误
任何帮助都将不胜感激。谢谢
编辑:我发现这是因为spring希望在设置值之前映射中包含键,并且不会插入新条目
我现在尝试使用自定义绑定器来填充映射,但从未调用属性编辑器类方法
这是我的属性编辑器(我调用super只是为了验证控件是否正在访问该方法):
以下是注册主任:
class ItemQuantityPropertyEditorRegistrar implements PropertyEditorRegistrar {
void registerCustomEditors(PropertyEditorRegistry propertyEditorRegistry) {
propertyEditorRegistry.registerCustomEditor(MyCommand, "itemQty", new ItemQuantityPropertyEditor())
}
}
以下是我在resources.groovy中的条目:
beans = {
itemQuantityPropertyEditorRegistrar(ItemQuantityPropertyEditorRegistrar)
}
尝试使用公用集合惰性映射初始化映射:
import org.apache.commons.collections.MapUtils
import org.apache.commons.collections.FactoryUtils
class MyCommand {
Map itemQty = MapUtils.lazyMap([:], FactoryUtils.constantFactory(''))
static constraints = {
itemQty(nullable: true, blank: true)
}
}
您不应该需要自定义绑定属性编辑器之类的东西…尝试使用commons collections惰性映射初始化映射:
import org.apache.commons.collections.MapUtils
import org.apache.commons.collections.FactoryUtils
class MyCommand {
Map itemQty = MapUtils.lazyMap([:], FactoryUtils.constantFactory(''))
static constraints = {
itemQty(nullable: true, blank: true)
}
}
您不应该需要自定义绑定属性编辑器之类的东西