Announcement Announcement Module
Collapse
No announcement yet.
PropertyEditor fails for a List variable that contains Lists Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PropertyEditor fails for a List variable that contains Lists

    I have a property defined as List<List<String>> and I'm registering a custom property editor called CustomNestedListEditor on it:

    Code:
    public class CustomNestedListEditor extends PropertyEditorSupport &#123;
       private static final Log log = LogFactory.getLog&#40;CustomGroupEditor.class&#41;;
    
       public CustomGroupEditor&#40;&#41; &#123;&#125;
    
       public void setAsText&#40;String text&#41; &#123;
          log.debug&#40;"setAsText - " + text&#41;;
          // split the string up into lines, around the \n char
          List<String> groupLines = StringUtil.getValidLines&#40;text&#41;;
          setValue&#40;groupLines&#41;;
       &#125;
    
       @SuppressWarnings&#40;value = &#123; "unchecked" &#125;&#41;
       public String getAsText&#40;&#41; &#123;
          List<String> value = &#40;List<String>&#41; getValue&#40;&#41;;
          log.debug&#40;"getAsText&#40;&#41; = " + value&#41;; 
          return &#40;value == null ? "" &#58; StringUtil.printCollection&#40;value, "\n"&#41;&#41;;
       &#125;
    
    &#125;
    I'm registering my editor like:
    Code:
    protected void initBinder&#40;HttpServletRequest request, ServletRequestDataBinder binder&#41;
             throws Exception &#123;
          super.initBinder&#40;request, binder&#41;;
          binder.registerCustomEditor&#40;List.class, "groups", new CustomNestedListEditor&#40;&#41;&#41;;
       &#125;
    The javadoc for DataBinder.registerCustomEditor contains:

    Register the given custom property editor for the given type and field, or for all fields of the given type.

    If the field denotes an array or Collection, the PropertyEditor will get applied either to the array/Collection itself (the PropertyEditor has to create an array or Collection value) or to each element (the PropertyEditor has to create the element type), depending on the specified required type.

    Note: Only one single registered custom editor per property path is supported. In case of a Collection/array, do not register an editor for both the Collection/array and each element on the same property.
    The problem I'm having is that Spring is assuming that I am registering my editor as an editor for the whole collection, since the type my editor is setting with setValue() is a List. But in actuality I'm trying to register the editor as being an element-level editor.

    Looking at the logs, when I submit the form, I have several calls to getAsText(), but NO calls to setAsText(). After submitting the form, my List now contains String objects instead of the intended List<String>'s.

    As a workaround, I can force Spring to recognize my editor as element-level if I create a custom wrapper class for each List element in my outer List and modify my editor accordingly.

    What I'm really asking is - Is this a design flaw? Couldn't the problem be fixed by adding a parameter to the registerCustomEditor function that specifies element-level or collection-level? Or am I missing something simple.
Working...
X