Announcement Announcement Module
Collapse
No announcement yet.
Binding map values with non-string keys? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding map values with non-string keys?

    I need help convincing Spring to bind to a map which has non-String keys. It may not be possible, in which case, some suggestions as to best implement a work-around would be helpful.

    This is the type to which I'm trying to Map:

    Code:
    public class Preferences {
      // <getters/setters omitted for brevity>
      private Map<LookupTableEntry, MyMapEntry> entries;
    &#125;
    
    public class LookupTableEntry &#123;
      // <getters/setters omitted for brevity>
      private int id;
      private String name;
    &#125;
    
    public class MyMapEntry &#123;
      // <getters/setters omitted for brevity>
      private String code;
      private BigDouble cost;
    &#125;
    The relevant JSP looks like this:

    Code:
    <c&#58;forEach items="$&#123;prefs.entries&#125;" var="entry">
      <tr>
        <td>
          $&#123;entry.key.name&#125;
        </td>
        <td>
          <spring&#58;bind path="prefs.entries&#91;$&#123;entry.key&#125;&#93;.code">
            <input type="text" name="$&#123;status.expression&#125;" value="$&#123;status.value&#125;"/> 
          </spring&#58;bind>
          </td>
        <td>
          <spring&#58;bind path="prefs.entries&#91;$&#123;entry.key&#125;&#93;.cost">
            <input type="text" name="$&#123;status.expression&#125;" value="$&#123;status.value&#125;"/>
          </spring&#58;bind>
        </td>
      </tr>
    </c&#58;forEach>
    Thsi fails because it's not clear how to bind from a String to a LookupTableEntry. The exact error is: Invalid property 'entries[LookupTableEntry@1f2412a]' of bean class [Preferences]: Value of nested property 'entries[LookupTableEntry@1f2412a]' is null

    If I change the type for entries to Map<String, MyMapEntry>, everything works as expected. The problem is that I cannot embed the names of the keys in the map because they may change, which is why I'm using a lookup table. And I'm loading my lookup table using Hibernate, which is why I have a class representing an entry in the table.

    I'm trying to find a clean solution, and this really seems like something that should have a straightforward solution. I'm just not seeing it! Maybe there's another approach that doesn't require non-String keys?

    I found this trolling through JIRA, though I don't think it's something that will help me out. http://opensource.atlassian.com/proj...browse/SPR-392

    Thanks!
    Christian
Working...
X