Announcement Announcement Module
Collapse
No announcement yet.
Multiple select value and spring:bind Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


    • #3
      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 ?

      Comment


      • #4
        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]);
        }
        }

        Comment


        • #5
          Perfect !

          Thanks a lot, again !

          Comment


          • #6
            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

            Comment


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

              Comment


              • #8
                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

                Comment


                • #9
                  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.

                  Comment


                  • #10
                    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

                    Comment


                    • #11
                      In my jsp pages i just put a select without spring.

                      It works fine for me this is the old methode simple request parameters, convert in the bind methode to object,

                      Try it and tell me if it works ;-)

                      Fabien

                      Comment


                      • #12
                        This works, but I prefer to keep the <spring:bind> so I preceded the <input> names with a "_" so Spring does not see them while binding. This is not really elegant, but it works.

                        I think the Spring Team should modify the bind method to pass a string array to a custom editor when multiple form parameters with the same name are posted. Now we could do our stuff in regular propertyEditors.

                        Uze

                        Comment


                        • #13
                          customeditor

                          Agree !

                          I like to make a customeditor in order to avoid a string[] to objects and objects to string[] conversion (or encapsulate this conversion) ?

                          Do u kown a sample ?

                          I going to sleep now it's late... see u soon,

                          Fabien

                          Comment


                          • #14
                            Please ....

                            Hi,

                            Always no way for this ?

                            Thanks,

                            Fabien.

                            Comment


                            • #15
                              Originally posted by uze
                              This works, but I prefer to keep the <spring:bind> so I preceded the <input> names with a "_" so Spring does not see them while binding. This is not really elegant, but it works.

                              I think the Spring Team should modify the bind method to pass a string array to a custom editor when multiple form parameters with the same name are posted. Now we could do our stuff in regular propertyEditors.

                              Uze
                              Check out this JIRA issue:
                              http://opensource.atlassian.com/proj...browse/SPR-552

                              Comment

                              Working...
                              X