Announcement Announcement Module
Collapse
No announcement yet.
Representing Map<String,String> using form taglib Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Representing Map<String,String> using form taglib

    Hi,

    I am working on my first Spring MVC project, and using the tags/form taglib.

    The data object behind the form has one field which is a Map<String,String>. There are no restrictions on the keys and values in the data model, but for this particular form the set of keys is fixed while the user may change the values.

    In the non-Spring predecessor of this project, this was done as follows:

    Code:
                <c:forEach var="keyName" items="${keyNames}" varStatus="iter">
                  <label for="key-${iter.index}">${keyName}</label>
                  <input type="text" name="key-${keyName}" id="key-${iter.index}" value="${entity.partsMap[keyName]}"/>
                </c:forEach>
    where keyNames is populated in the controller with the keys from the map.

    I would like to replace this with something like the following (which of course does not work!)
    Code:
                 <c:forEach var="keyName" items="${keyNames}" varStatus="iter">
                   <label for="key-${iter.index}">${keyName}</label>
                   <form:input path="partsMap[keyName]"/>
                   <form:errors path="partsMap[keyName]"/>
                 </c:forEach>
    or even better, do away with the need for the keyNames list.
    (and better still, change the form:input for a form:select!)

    Is any of this possible? I will be very grateful to anyone who can point me in the right direction...

    Thanks,

    Alex

  • #2
    Solved

    Figured it out:

    Code:
              <c:forEach items="${entity.partsMap}" var="partsMap" varStatus="iter">
                <label for="partsMap'${partsMap.key}'">${partsMap.key}</label>
                <form:select path="partsMap['${partsMap.key}']">
                  <form:options items="${possibleValues}"/>
                </form:select>
                <form:errors path="partsMap['${partsMap.key}']"/>
              </c:forEach>

    Comment

    Working...
    X