Announcement Announcement Module
Collapse
No announcement yet.
HELP!!! newbie attempting to populate a dropdown!!! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HELP!!! newbie attempting to populate a dropdown!!!

    Hello there,
    I am not sure my problem is directly related to spring but as I am using it along with jstl i thought this could be a good forum to post my problem.
    I've been the last 2 days trying to fill a dropdown with countries. The way I am doing this is as follow:

    movingplanner-servlet.xml:
    Code:
    <beans>
    
        <bean name="country1" class="com.movingplanner.backend.model.Country">
        	<property name="id"><value>1</value></property>
        	<property name="name"><value>Canada</value></property>
        	<property name="code"><value>CA</value></property>
        </bean>
        
        <bean name="country2" class="com.movingplanner.backend.model.Country">
        	<property name="id"><value>2</value></property>
        	<property name="name"><value>United States</value></property>
        	<property name="code"><value>US</value></property>
        </bean>
        
    	<bean name="searchCriteria" class="com.movingplanner.backend.commons.impl.SearchCriteriaImpl" singleton="false"></bean>
    	<bean name="movingplannerController" class="com.movingplanner.ui.MovingplannerController"/>
    	
    	<bean name="propertySearchController" class="com.movingplanner.ui.PropertySearchController">
    		<property name="countries">
    			<list>
    				<ref bean="country1"/>
    				<ref bean="country2"/>
    			</list>
    		</property>
    	</bean>
    	
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/home.htm">movingplannerController</prop>
                    <prop key="/propertySearch.htm">propertySearchController</prop>
                </props>
            </property>
        </bean>
        
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
            <property name="prefix"><value>/WEB-INF/jsp/</value></property>
            <property name="suffix"><value>.jsp</value></property>
        </bean>
    
    </beans>
    Here is the relevant chunk of code from my .jsp page:

    Code:
    	
    <tr>
       <td align="left">
    	<select style="WIDTH&#58; 143px" name="countrySelect">
    		<option value="0" 
                    <c&#58;if test="$status.value==0">selected="selected"</c&#58;if>>
                    -- Select Country --
                    </option>
    		<c&#58;forEach items="$&#123;countries&#125;" var="country">
    			<option value="<c&#58;out value="$&#123;country.id&#125;"/>" 							    <c&#58;if test="$status.value==country.id">
                                    selected="selected"
                                </c&#58;if>>
    	                    <c&#58;out value="$&#123;country.name&#125;"/>
    			</option>
    		</c&#58;forEach>
    	</select>
       </td>
    </tr>
    Here goes my controller class:
    Code:
    public class PropertySearchController implements Controller &#123;
    
    	private List countries;
    
    	public List getCountries&#40;&#41; &#123;
    		return countries;
    	&#125;
    
    	public void setCountries&#40;List countries&#41; &#123;
    		this.countries = countries;
    	&#125;
    
    	public ModelAndView handleRequest&#40;HttpServletRequest arg0,                       HttpServletResponse arg1&#41; throws Exception &#123;
    		
    		logger.info&#40;"-- Entering handleRequest --"&#41;;
    		
    		HashMap model = new HashMap&#40;&#41;;
    		model.put&#40;"countries", getCountries&#40;&#41;&#41;;
    		
    		return new ModelAndView&#40;"propertySearch", "model", model&#41;;
    	&#125;
    
    &#125;
    I am deplying my app to tomcat 5.
    The result I am getting is that the country dropdown ionly shows the -- Select Country -- option and nothing more.
    I have been trying to figure out the problem but have been unsuccessfull so far.
    any help will be appreciated.

    Thanks.

  • #2
    Re: HELP!!! newbie attempting to populate a dropdown!!!

    here is a simplifed sample loop. i hope i works you may want to do a

    ${countries}

    in your page to make your collection has values.

    <select type="text" name="${status.expression}" value="${status.value}">
    <option value="-1">-- Select --</option>
    <c:forEach items="${states}" var="state">
    <option value="${state.value}"${state.value == status.value ? ' selected' : ''}>${state.label}</option>
    </c:forEach>
    </select>

    hope it helps.

    ATTA

    Originally posted by edovale
    Hello there,
    I am not sure my problem is directly related to spring but as I am using it along with jstl i thought this could be a good forum to post my problem.
    I've been the last 2 days trying to fill a dropdown with countries. The way I am doing this is as follow:

    movingplanner-servlet.xml:
    Code:
    <beans>
    
        <bean name="country1" class="com.movingplanner.backend.model.Country">
        	<property name="id"><value>1</value></property>
        	<property name="name"><value>Canada</value></property>
        	<property name="code"><value>CA</value></property>
        </bean>
        
        <bean name="country2" class="com.movingplanner.backend.model.Country">
        	<property name="id"><value>2</value></property>
        	<property name="name"><value>United States</value></property>
        	<property name="code"><value>US</value></property>
        </bean>
        
    	<bean name="searchCriteria" class="com.movingplanner.backend.commons.impl.SearchCriteriaImpl" singleton="false"></bean>
    	<bean name="movingplannerController" class="com.movingplanner.ui.MovingplannerController"/>
    	
    	<bean name="propertySearchController" class="com.movingplanner.ui.PropertySearchController">
    		<property name="countries">
    			<list>
    				<ref bean="country1"/>
    				<ref bean="country2"/>
    			</list>
    		</property>
    	</bean>
    	
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/home.htm">movingplannerController</prop>
                    <prop key="/propertySearch.htm">propertySearchController</prop>
                </props>
            </property>
        </bean>
        
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
            <property name="prefix"><value>/WEB-INF/jsp/</value></property>
            <property name="suffix"><value>.jsp</value></property>
        </bean>
    
    </beans>
    Here is the relevant chunk of code from my .jsp page:

    Code:
    	
    <tr>
       <td align="left">
    	<select style="WIDTH&#58; 143px" name="countrySelect">
    		<option value="0" 
                    <c&#58;if test="$status.value==0">selected="selected"</c&#58;if>>
                    -- Select Country --
                    </option>
    		<c&#58;forEach items="$&#123;countries&#125;" var="country">
    			<option value="<c&#58;out value="$&#123;country.id&#125;"/>" 							    <c&#58;if test="$status.value==country.id">
                                    selected="selected"
                                </c&#58;if>>
    	                    <c&#58;out value="$&#123;country.name&#125;"/>
    			</option>
    		</c&#58;forEach>
    	</select>
       </td>
    </tr>
    Here goes my controller class:
    Code:
    public class PropertySearchController implements Controller &#123;
    
    	private List countries;
    
    	public List getCountries&#40;&#41; &#123;
    		return countries;
    	&#125;
    
    	public void setCountries&#40;List countries&#41; &#123;
    		this.countries = countries;
    	&#125;
    
    	public ModelAndView handleRequest&#40;HttpServletRequest arg0,                       HttpServletResponse arg1&#41; throws Exception &#123;
    		
    		logger.info&#40;"-- Entering handleRequest --"&#41;;
    		
    		HashMap model = new HashMap&#40;&#41;;
    		model.put&#40;"countries", getCountries&#40;&#41;&#41;;
    		
    		return new ModelAndView&#40;"propertySearch", "model", model&#41;;
    	&#125;
    
    &#125;
    I am deplying my app to tomcat 5.
    The result I am getting is that the country dropdown ionly shows the -- Select Country -- option and nothing more.
    I have been trying to figure out the problem but have been unsuccessfull so far.
    any help will be appreciated.

    Thanks.

    Comment


    • #3
      public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {

      logger.info("-- Entering handleRequest --");

      HashMap model = new HashMap();
      model.put("countries", getCountries());

      return new ModelAndView("propertySearch", "model", model);
      }
      I think your problem may be in the constructor you use to return your ModelAndView.
      The three argument constructor you use will add a single model item, the Map model, under the name "model".
      On the other hand if you use a two argument constructor,
      Code:
      return new ModelAndView&#40;"propertySearch", model&#41;;
      the model will consist of all the obejcts in your map, i.e. your countries list under the name 'countries', and you can access it as countries. See the ModelAndView javadoc for details.

      Hope this helps,

      Comment


      • #4
        Thanks cmgharris,
        That was it. Problem solved.

        cheers,

        Erick.

        Comment

        Working...
        X