Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Multiple select value and spring:bind

  1. #1

    Default Multiple select value and spring:bind

    My question is very simple :

    how can i use a select with multiple value and spring:bind ?

    In fact i have a list of users, every user has N role, and i like to show/choose the list of the role with a select with the property mutiple="true".

    Code:
    					<spring&#58;bind path="utilisateur.roles">
    					  <select name="idRoles" multiple="true" >
    					    <c&#58;forEach var="role" items="$&#123;roles&#125;">
    					    </c&#58;forEach>
    					  </select>
    						<span class="fieldError">$&#123;status.errorMessage&#125;</span>
    					</spring&#58;bind>
    Anyone a sample ?

    Fabien.

  2. #2
    Join Date
    Aug 2004
    Location
    Southern Germany
    Posts
    30

    Default Select box with Spring bind (from mailing list)

    Below is an excerpt from the mailing list I once used for testing (and it worked!).
    You may encounter the problem when working with "multi-select-boxes" that the request only contains the selected items.

    Another reference you can check is AppFuse

    https://appfuse.dev.java.net/

    it contains a multi select box but without using "spring:bind" (but solves the problem of non-selected items).

    Regards,
    Lars

    As this solution not only applies to checkboxes, but also to html <select> fields with multiple values allowed, I would suggest renaming "checkbox" to something else, "field" maybe?

    For example:

    <spring:bind path="recommendation.involvedMemberStates">
    <input type="hidden" name="_checkbox.<c:out
    value="${status.expression}"/>">
    <select name="<c:out value="${status.expression}"/>" size="6"
    multiple>
    <c:forEach var="memberState" items="${status.value}">
    <option value="<c:out
    value="${memberState.memberStateCode}"/>">
    <c:out value="${memberState.name}"/>
    </option>
    </c:forEach>
    </select>
    </spring:bind>

    This renders a 6-line select box where you can select multiple values.
    If none are selected, the parameter is not present in the request, and the "_checkbox" hidden field ensures the bean property is set to null.

    This works fine, but the "checkbox" naming is a bit undesirable...

    Kind regards,
    Tom.

  3. #3

    Default Ok but...

    I'am ok for displaying the selected roles.

    But how can i retreive the roles selected in the list box when i submit the form ?

  4. #4
    Join Date
    Aug 2004
    Location
    Southern Germany
    Posts
    30

    Default

    You can use the request, e.g.:

    String[] userRoles = request.getParameterValues("userRoles");
    if (userRoles != null) {
    for (int i = 0; i < userRoles.length; i++) {
    log.debug(userRoles[i]);
    }
    }

  5. #5

    Default Perfect !

    Thanks a lot, again !

  6. #6
    Join Date
    Aug 2004
    Location
    Montreal, Canada
    Posts
    35

    Default

    I have problem with the same multi-select context. My model object has a property of type List containing objects binded to a list of checkboxes in a form. Now when I submit the form, what is the proper way to set that List property (ie to rebuild/add the List's property objects based on the form's checked boxes)???

    Any help would be appreciated.

    Uze

  7. #7

    Default Give us more information

    Hi uze ! can u send us more information (small peice of your code for example) about your pb in order to solve it ?

    Thanks,

    Fabien.

  8. #8
    Join Date
    Aug 2004
    Location
    Montreal, Canada
    Posts
    35

    Default

    Ok, I'll try to make it short:

    I have a car class with a drivers List property like this:

    Code:
    Class Car&#123;
      public List drivers
      ...
    &#125;
    Car is the command passed thru formBackingObject() and refererenced as myDataModel in the view:

    Code:
    <spring&#58;bind path="myDataModel.drivers">
    	
    	<c&#58;forEach var="driver" items="$&#123;allDrivers&#125;">
    		<INPUT class="checkbox" type="checkbox"
    			name='<c&#58;out value="$&#123;status.expression&#125;.$&#123;driver.id&#125;" />'								
    			<c&#58;forEach var="d" items="$&#123;myDataModel.drivers&#125;">								
    				<c&#58;if test="$&#123;d.Id==driver.Id&#125;">CHECKED</c&#58;if>								
    			</c&#58;forEach>>										
    		<c&#58;out value="$&#123;model.modelName&#125;" />
    		<br/>
    	</c&#58;forEach>
    	
    </spring&#58;bind>
    allDriver being a List of driver passed thru referenceData(). This will generate as much checkboxes as there is driver objects and check the ones that have a matching driver object in myDataModel.

    When I post the form, I want myDataModel.drivers (which is a List) to be populated with the checked items only. Now, I could parse manually the request and create the required driver objects, but I am wondering if we can have something like the propertEditors to do the trick.

    Any ideas???

    Uze

  9. #9

    Default No more idea

    I have the same problem, i did it manually in the controller exactly in the onBind part. I don't find an example for propertyEditor for the moment.

    I hope someone will help us, for this moment the classic solution works well and it can be upgraded later.

    Fabien.

  10. #10
    Join Date
    Aug 2004
    Location
    Montreal, Canada
    Posts
    35

    Default

    Now I really need help. I tried parsing the request in the onBind as Fabien mentionned but Spring trows me
    Failed to convert property value of type [java.lang.String] to required type [java.util.List] for property models
    with this jsp:

    Code:
    <spring&#58;bind path="reoDataModel.models">
    	<c&#58;forEach var="model" items="$&#123;models&#125;">
    		<INPUT class="checkbox" type="checkbox"
    			value='<c&#58;out value="$&#123;model.programId&#125;" />'
    			name='<c&#58;out value="$&#123;status.expression&#125;" />'								
    			<c&#58;forEach var="m" items="$&#123;reoDataModel.models&#125;">								
    				<c&#58;if test="$&#123;m.programId==model.programId&#125;">CHECKED</c&#58;if>								
    			</c&#58;forEach> >								
    		<c&#58;out value="$&#123;model.modelName&#125;" />
    		<br />
    	</c&#58;forEach>
    </spring&#58;bind>
    And this is the code in my form controller:

    Code:
    	protected void onBind&#40;HttpServletRequest request, Object command, BindException errors&#41; &#123;
    
    			ReoDataModel reo = &#40;ReoDataModel&#41; command;	
    			
    			//parse checked models from the request 
    			List models=new LinkedList&#40;&#41;;
    			String&#91;&#93; reqModels = request.getParameterValues&#40;"models"&#41;;
    			if &#40;reqModels != null&#41; &#123;				
    				for &#40;int i = 0; i < reqModels.length; i++&#41; &#123;
    					models.add&#40;Model.findById&#40;reqModels&#91;i&#93;&#41;&#41;;					
    				&#125;	
    			&#125;
    
    			reo.setModels&#40;models&#41;;
    	&#125;
    Do I have to remove the bind from the JSP? How can spring know I've actually handled this property of the command and how can I tell Spring to ignore it?

    Any hint would help!

    Uze

Posting Permissions

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