Results 1 to 2 of 2

Thread: Representing Map<String,String> using form taglib

  1. #1
    Join Date
    Oct 2012
    Posts
    2

    Default 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. #2
    Join Date
    Oct 2012
    Posts
    2

    Default 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>

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •