Announcement Announcement Module
Collapse
No announcement yet.
How to bind a java.util.Set Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to bind a java.util.Set

    Is it possible to bind a java.util.Set that holds a collection of complex objects on JSP using <spring:bind> and <c:forEach> tags to be able to get/set object properties? In case it's possible it would be great to see an example or to be pointed where to read about it.

  • #2
    I don't have an answer, but I need that functionality as well.

    Comment


    • #3
      For now, I use java.util.Set to hold data and work with DB using Hinernate, and use java.util.List to pass the data to JSP. In order to do it, I've added getter/setter to work with java.util.Set as with java.util.List.

      public List getObjectList() {
      List list = new ArrayList();
      Iterator iterator = this.object.iterator();
      while (iterator.hasNext())
      list.add(iterator.next());
      return list;
      }

      public void setObjectList(List list) {
      this.object.retainAll(list);
      }

      .....
      <c:forEach varStatus="loop" items="${command.objectList}">
      <tr>
      <spring:bind path="command.objectList[${loop.index}].property1">
      <td>
      <font color="red"><c:out value="${status.errorMessage}"/></font>
      <input type="text" name="<c:out value="${status.expression}" />" value="<c:out value="${status.value}"/>">
      </td>
      </spring:bind>
      <spring:bind path="command.objectList[${loop.index}].property2">
      <td>
      <font color="red"><c:out value="${status.errorMessage}"/></font>
      <input type="text" name="<c:out value="${status.expression}" />" value="<c:out value="${status.value}"/>">
      </td>
      </spring:bind>
      </tr>
      </c:forEach>
      .....

      Comment


      • #4
        I am using Spring 1.1.4 and it binds to a Set just fine right out of the box. My example:

        Command object (named criteria in the JSP page):
        Code:
        // Accessor methods removed for brevity
        public class SearchCriteria &#123;
        	private String searchText;
        	private long country;
        	private Set<Long> metros;
        JSP page:
        Code:
        				<spring&#58;bind path="criteria.metros">
        					<select name="$&#123;status.expression&#125;" id="metro" size="5" multiple>
        						<c&#58;if test="$&#123;criteria.country != 0&#125;"><option value="0"<xx&#58;ifEmptyIdCol col="$&#123;criteria.metros&#125;"> selected</xx&#58;ifEmptyIdCol>>All Locations</option><c&#58;forEach items="$&#123;countryMap&#91;criteria.country&#93;.metros&#125;" var="metro"><option value="$&#123;metro.id&#125;"<xx&#58;ifInIdCol id="$&#123;metro.id&#125;" col="$&#123;criteria.metros&#125;"> selected</xx&#58;ifInIdCol>>$&#123;metro.name&#125;</option></c&#58;forEach></c&#58;if>
        					</select>
        				</spring&#58;bind>
        Spring automatically binds to the collection (Set or List) with no trouble at all. I have noticed that it seems to like inserting String values into the Set, however. Thus, my processors handle either Long or String values in the collection to avoid any problems. There is also another thread going in which we are discussing using a custom PropertyEditor to handle binding of a Set: http://forum.springframework.org/viewtopic.php?t=2258. You may want to keep an eye on that thread, as I'm sure an example will be posted soon.

        The ifInIdCol checks a set/collection for a given ID element, and the ifEmptyIdCol checks if a collection contains no meaningfull values, since I have a placeholder value 0 to indicate All Metros.

        Comment


        • #5
          I tried to do exactly this but I can't get it work. The values from the set are displayed on the input fields of the page but if user changes values on the field and submits the form, the changes are not in the command object.

          When debugging, I notice that method setObjectList() is never called.

          What might be wrong?

          Comment


          • #6
            Originally posted by sjk
            I tried to do exactly this but I can't get it work. The values from the set are displayed on the input fields of the page but if user changes values on the field and submits the form, the changes are not in the command object.

            When debugging, I notice that method setObjectList() is never called.

            What might be wrong?
            Are you using Spring 1.1.4, and have you registered the custom editor for the form? The thread that I link to in my previous post has a complete working example implementation if you haven't gotten your version working...

            Comment


            • #7
              Originally posted by code_kungfu

              Are you using Spring 1.1.4, and have you registered the custom editor for the form? The thread that I link to in my previous post has a complete working example implementation if you haven't gotten your version working...
              I was actually referring to the post form rsi but it would be better to get it to work your way. But I am sorry I don't quite understand your example, though. I also don't want a select but an HTML table with input type=text on each row.

              This countryMap and everything is confusing me. How would you write the JSP if you just had a set called metros and a metro had two field id and name? I would like to see a simple example with an HTML table and input fields.

              I am using Spring 1.1.5.

              Comment

              Working...
              X