Announcement Announcement Module
Collapse
No announcement yet.
bind problems: list, select Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • bind problems: list, select

    Hi,

    I am having difficulty getting spring:bind to work on a list.

    My object graph is a little complicated, but if I can get this to work, I can use it over many pages, so I think it is worth the effort.

    Here are the relevant objects:

    Code:
    public class ProfileInfo extends AbstractInfo {
        List populatedSelects;
    .... including getters/setters...
    }
    Code:
    public class PopulatedSelect {
        private Select select = new Select();
        private AttributeRating attributeRating = new AttributeRating();
    .... including getters/setters...
    Code:
    public class Select {
        private String name;
        private String title;    
        private String category;  
        private List options = new ArrayList();
        private int selected;
    .... including getters/setters...
    Code:
    public class AttributeRating {
        private String userName;
        private String attributeName;
        private int rating = 0;
    .... including getters/setters...
    features.jsp:
    Code:
    <TABLE> 
    <c&#58;forEach items="$&#123;profileInfo.populatedSelects&#125;"  var="populatedSelect" varStatus="varStatus">
      	<TR><td><c&#58;out value="$&#123;populatedSelect.select.title&#125;"/></td>
      	<TD>
      	       <spring&#58;bind path="profileInfo.populatedSelects&#91;$&#123;varStatus.index&#125;&#93;.attributeRating.rating">
      	       <select name="<c&#58;out value="$&#123;populatedSelect.select.name&#125;"/>" size="1"> 
    		  <c&#58;forEach items="$&#123;populatedSelect.select.options&#125;" var="option">
    			<option <c&#58;if test="$&#123;populatedSelect.attributeRating.rating == option.index&#125;">selected</c&#58;if> value="<c&#58;out value="$&#123;option.index&#125;"/>"><c&#58;out value="$&#123;option.value&#125;"/></option>
    		  </c&#58;forEach>			
      		</select>
      	     </spring&#58;bind>
      	</TD>  	
      	</spring&#58;bind>
      	</TR>
      </c&#58;forEach> 
    </TABLE>
    I have had to use a Select object, because I have many pages using a variable number of selects, and I needed to keep all these properties in an XML file. I need the AttributeRating object because later I want to make it so that is a user doesn't like any of the options offered in a select, he can fill in his own value into a textfield - but I have removed this code for now.

    It all works in the display, except that the value selected in the select options doesn't get bound to the field AttributeRating.rating.

    Adding the line:

    Code:
    rating test = <c&#58;out value="$&#123;profileInfo.populatedSelects&#91;varStatus.index&#93;.attributeRating.rating&#125;"/>
    after the spring:bind tag outputs the correct value for attributeRating.rating for each tag.

    I guess I have something wrong in the spring:bind tag - can anyone help?

    Many thanks,

    John Pedersen

  • #2
    Missing the status in your bind

    Code:
    <spring&#58;bind path="profileInfo.populatedSelects&#91;$&#123;varStatus.index&#125;&#93;.attributeRating.rating">
                <select name="<c&#58;out value="$&#123;populatedSelect.select.name&#125;"/>" size="1">
            <c&#58;forEach items="$&#123;populatedSelect.select.options&#125;" var="option">
             <option <c&#58;if test="$&#123;populatedSelect.attributeRating.rating == option.index&#125;">selected</c&#58;if> value="<c&#58;out value="$&#123;option.index&#125;"/>"><c&#58;out value="$&#123;option.value&#125;"/></option>
            </c&#58;forEach>         
            </select>
              </spring&#58;bind>
    Maybe I've gone bleery eyed but I can't see a ${status} reference in your code. Am I missing it?

    Also you have a 2nd "</spring:bind>" after all of that. Is that supposed to close off one not listed in your code?

    I would also look into upgrading to the "${option.value}" syntax versus the <c:out/> one. Easier on the eyes. Also I would see if writting a custom tag would help you out, especially if you are going to use this code in other pages.

    Comment


    • #3
      Thanks for the reply - I have just got it working now. You were right - it was just the use of 'status' I was missing. This works fine:

      Code:
      <c&#58;forEach items="$&#123;profileInfo.populatedSelects&#125;"  var="populatedSelect" varStatus="varStatus">
        	<TR><td><c&#58;out value="$&#123;populatedSelect.select.title&#125;"/></td>
        	<TD>
        		<spring&#58;bind path="profileInfo.populatedSelects&#91;$&#123;varStatus.index&#125;&#93;.attributeRating.rating">
        			<select name="<c&#58;out value="$&#123;status.expression&#125;"/>" size="1"> 
        			<c&#58;forEach items="$&#123;populatedSelect.select.options&#125;" var="option">
        				<option <c&#58;if test="$&#123;status.value == option.index&#125;">selected</c&#58;if> value="<c&#58;out value="$&#123;option.index&#125;"/>"><c&#58;out value="$&#123;option.value&#125;"/></option>
      			</c&#58;forEach>			
        			</select>
        	     </spring&#58;bind>
        	</TD>
        	<TD><spring&#58;bind path="profileInfo.populatedSelects&#91;$&#123;varStatus.index&#125;&#93;.attributeRating.customRating">
        	<input name="<c&#58;out value="$&#123;status.expression&#125;"/>" type="text" size="40" maxlength="255" value = "$&#123;status.value&#125;">
        	</spring&#58;bind></TD>
        	
        	</TR>
        </c&#58;forEach>  
        </TABLE>
      This lists my selects, and also some textfields that give users the option of filling in their own values if they don't like the choices offered in the select.

      Many thanks,

      John

      Comment

      Working...
      X