【发布时间】:2011-11-11 14:15:49
【问题描述】:
请注意,我正在将 Java 和 Spring 用于 Web 应用程序。
我有一个对象 (objectBean),它包含一个 EnumInnerObject 类型的 EnumSet (enumSet) 作为属性。我将此对象作为 bean 从我的控制器传递到我的 .jsp 视图。我使用以下 .jsp 代码来绑定复选框:
<form:form commandName="objectBean" name="whatever" action="./save.htm" method="post">
<form:checkboxes items="${allOptions}" path="enumSet" />
</form:form>
这是我的控制器 initbinder:
@InitBinder
protected void initBinder(WebDataBinder binder) throws Exception{
binder.registerCustomEditor(EnumSet.class, "enumSet", new CustomCollectionEditor(Collection.class){
protected Object convertElement(Object element){
if(element instanceof String){
EnumInnerObject enumInnerObject= EnumInnerObject.valueOf((String)element);
return enumInnerObject;
}
return null;
}
});
在控制器中,我传递了 allOptions(与我的 bean 分开),它包含所有 EnumInnerObject 选项,因此显示了所有复选框。 “enumSet”是包含适当值的 EnumSet 属性(如果该值包含在 EnumSet 中,则它会自动检查“allOptions”中的正确框)。所有这些都有效,并且 .jsp 正确显示了正确的复选框。但是,问题是当我提交要保存的页面时。我收到以下错误:
java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String[]] to required type [java.util.EnumSet] for property 'enumSet': PropertyEditor [com.example.controller.MyController$1] returned inappropriate value]
我觉得我必须修改 InitBinder 才能让表单提交工作。有什么想法吗??
谢谢!
【问题讨论】:
标签: java spring model-view-controller jstl