绑定弹簧:提交时枚举的复选框会导致错误

Jar*_*red 7 java model-view-controller spring jstl

简而言之,我正在使用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>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器启动器:

@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;
         }
     });
Run Code Online (Sandbox Code Playgroud)

在控制器中,我传递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]
Run Code Online (Sandbox Code Playgroud)

我有一种感觉,我必须修改InitBinder以使表单提交工作.有任何想法吗??

谢谢!

dma*_*a_k 5

坦率地说,我很难想象这个想法会如何起作用:EnumSet集合旨在存储枚举的值,但目前它的构造需要知道枚举中元素的数量(=宇宙的大小,它的术语) ).

CustomCollectionEditor传递一个集合类作为它的构造函数参数,因此它需要创建此集合,并且由于上述原因它将失败.更在CustomCollectionEditor仅支持目标集合的有限数量(ArrayList,TreeSet,LinkedHashSet,见CustomCollectionEditor#createCollection()).

为了不使事情过于复杂,我建议你使用通用集合,而不是EnumSet.否则你需要编写自己的属性编辑器.实施并不困难,例如:

binder.registerCustomEditor(EnumSet.class, "enumSet",
    new PropertyEditorSupport() {
        @Override
        public void setValue(Object value) {
            EnumSet<EnumInnerObject> set = EnumSet.noneOf(EnumInnerObject.class);

            for (String val: (String[]) value) {
                set.add(EnumInnerObject.valueOf(val));
            }

            super.setValue(set);
        }
    });
Run Code Online (Sandbox Code Playgroud)