Results 1 to 6 of 6

Thread: dynamic <select>/<option> list population

  1. #1
    Join Date
    Sep 2004
    Posts
    11

    Default dynamic
    I have the following scenario and I'm trying to use spring MVC:

    I have form to enter a user address with number, street, city, etc. Also, on the form I have a combobox/<select> holding a list of states as follows:

    Code:
    <spring&#58;bind path="address.state">
    	          <select size="1" name="<c&#58;out value="$&#123;status.expression&#125;"/>" onchange="submit&#40;&#41;">
    				  <c&#58;forEach var="state" items="$&#123;states&#125;">
    					  <option value="<spring&#58;transform value="$&#123;state.state&#125;"/>"
    		<c&#58;if test="$&#123;address.state.state==state.state&#125;">SELECTED</c&#58;if>>
    					  		<c&#58;out value="$&#123;state.stateAbbr&#125;"/>
    					  </option>
    				  </c&#58;forEach>
    			  </select>
              <font color="red"><c&#58;out value="$&#123;status.errorMessage&#125;"/></font>
    	      </spring&#58;bind>
    and a combobox/<select> holding a list of counties for the selected state as follows:
    Code:
    	      <spring&#58;bind path="address.county">
    	          <select size="1" name="<c&#58;out value="$&#123;status.expression&#125;"/>">
    				  <c&#58;forEach var="county" items="$&#123;counties&#125;">
    					  <option value="<spring&#58;transform value="$&#123;county.county&#125;"/>"
    		<c&#58;if test="$&#123;address.county.county==county.county&#125;">SELECTED</c&#58;if>>
    					  		<c&#58;out value="$&#123;county.countyName&#125;"/>
    					  </option>
    				  </c&#58;forEach>
    			  </select>
              <font color="red"><c&#58;out value="$&#123;status.errorMessage&#125;"/></font>
    	      </spring&#58;bind>
    What I would like to happen is that when a user changes the selection of the state that the county combobox/<select> list would be repopulated with the counties of the newly selected state.

    Does anyone have a solution to a situation like this? Can this be done with Spring MVC alone? Is javascript necessary?


  2. #2
    Join Date
    Sep 2004
    Posts
    11

    Default

    The jsp for the state <select> should look as follows, sorry:

    Code:
    <spring&#58;bind path="address.state">
        <select size="1" name="<c&#58;out value="$&#123;status.expression&#125;"/>" >
    	  <c&#58;forEach var="state" items="$&#123;states&#125;">
    	      <option value="<spring&#58;transform value="$&#123;state.state&#125;"/>"
    		<c&#58;if test="$&#123;address.state.state==state.state&#125;">SELECTED</c&#58;if>>
    					  		<c&#58;out value="$&#123;state.stateAbbr&#125;"/>
                  </option>
    	</c&#58;forEach>
        </select>
           <font color="red"><c&#58;out value="$&#123;status.errorMessage&#125;"/></font>
    </spring&#58;bind>

  3. #3
    Join Date
    Aug 2004
    Location
    San Jose, CA
    Posts
    24

    Default JavaScript vs. roundtrip

    Can this be done with Spring MVC alone? Is javascript necessary?
    The answer is almost and probably. In order to take action on the client (browser) after a form element has been changed you'll need some sort of client-side scripting. The server can only respond to page requests and form submissions (a type of request). The onchange event handler attached to the state select can either load new values into the counties combo from pre-populated JavaScript objects or quietly submit the form and get the values from the server. The first method, in my opinion, relies a little too heaviliy on JavaScript where you have less control of behavior. While a little less seamless, a roundtrip to the server that returns just XHTML is a much safer bet.

    - Justin

    P.S. The HTML font element has gone the way of the dodo. Using CSS to decorate error messages will save you time and headaches.

  4. #4
    Join Date
    Sep 2004
    Posts
    11

    Default

    Thanks for the response. I like the 'quietly submitting' idea as I'm trying to stay away from javascript as much as possible. Currently, that is way that I am set up, and the combo box population seems to work just fine. I now need a way to distinguish between a submit from the combo box and the submit from the submit button. Also, when submitting from the combo box onchanged event, I run through a round of validation of the form's fields and get validation errors showing up when the user has only changed the state combo box. I've still got some work to do before this will be 'right.' I'm pretty new to all this. Appreciate the help.

    DJ

    ps. thanks for tip about the font element.

  5. #5
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    To distinguish between form submission and altering model data, override:
    Code:
    isFormSubmission&#40;HttpServletRequest&#41;

  6. #6

    Default

    Also, I have a solution to this using just Spring MVC and Very, Very little JavaScript.

    See http://forum.springframework.org/showthread.php?t=12497 .

    I hope that helps someone else, I'd also like to here about any improvements.

    Take care,
    James Winans
    Last edited by robyn; May 14th, 2006 at 12:31 PM.

Similar Threads

  1. Replies: 3
    Last Post: Mar 9th, 2011, 10:43 AM
  2. Replies: 2
    Last Post: Jul 14th, 2007, 09:05 AM
  3. Replies: 1
    Last Post: Jul 21st, 2005, 05:17 AM
  4. dynamic proxies och hibernate
    By erik_romson in forum Data
    Replies: 3
    Last Post: Dec 18th, 2004, 04:19 AM
  5. Replies: 3
    Last Post: Nov 6th, 2004, 10:10 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
  •