Announcement Announcement Module
No announcement yet.
Binding to an empty list Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding to an empty list

    I have a command object that contains a list that may be empty. In fact it is always empty when the object is created, not null but empty. The list will only ever contain one item (this is how the object was modeled and it can't be changed), so the UI treats it like its not in a list. Here is the problem, when I go into the form for a create of one of these objects I attempt to bind like this:

    <spring:bind path="command.categories[0]">
    <!-- My Form Widget Goes Here -->

    This produces an ArrayIndexOutOfBoundsException because there is no 0 element in the list yet. How can I effectively call the equivalent of getCategories().add with a bind expression. My instinct tells me its not possible and that I should probably use PropertyEditor instead, but I thought I check to see if anyone has something better.



  • #2
    can you initialise 'categories' in the formBackingObject method? or within the constructor of the command object?


    • #3
      The categories list is inialized to be an empty list already. I can't put anything in the list because the domain does not require anything to be there.



      • #4
        seems like maybe one of two choices make sense.

        1. put something in the categories list so your JSP page doesn't crash.
        2. put a <c:if> around your <spring:bind> so you don't draw your "form widget" if there's nothing to draw.