Results 1 to 7 of 7

Thread: How to bind a java.util.Set

  1. #1
    Join Date
    Feb 2005
    Posts
    2

    Default 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. #2

    Default

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

  3. #3
    Join Date
    Feb 2005
    Posts
    2

    Default

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

  4. #4
    Join Date
    Jan 2005
    Posts
    17

    Default

    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.

  5. #5
    Join Date
    Apr 2005
    Posts
    7

    Default

    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?

  6. #6
    Join Date
    Jan 2005
    Posts
    17

    Default

    Quote 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...

  7. #7
    Join Date
    Apr 2005
    Posts
    7

    Default

    Quote 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.

Similar Threads

  1. bind set of checkbox values
    By JimN in forum Web
    Replies: 2
    Last Post: Aug 11th, 2005, 08:29 PM
  2. Replies: 8
    Last Post: Jul 29th, 2005, 06:40 PM
  3. Newbie bind question: bind an entire form?
    By kendelong in forum Web
    Replies: 0
    Last Post: Feb 4th, 2005, 04:02 PM
  4. Indexed properties and java.util.Set
    By karaznie in forum Web
    Replies: 1
    Last Post: Sep 20th, 2004, 03:55 AM
  5. Named Bind Variables
    By james.estes in forum Data
    Replies: 4
    Last Post: Sep 6th, 2004, 10:11 AM

Posting Permissions

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