Announcement Announcement Module
No announcement yet.
Returning List from Controller and iterate over it in the JSP Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Returning List from Controller and iterate over it in the JSP

    Hi all.
    I am returning List in ModelAndView from the Controller.
    I want to iterate over this List in the JSP and show the values in a combo box.
    I am using <c:forEach> tag.
    I am able to iterate the List for 'n' times(n= number of elements in the List)
    But I am not able to show the values in the combo box.

    which tag should i use?

    Please guide me.


  • #2
    Could we see a little more of your JSP (the whole section containing the c:forEach loop)?


    • #3
      The method in my Controller is as follows

      protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors) throws Exception{
      		TeamMember teamMember = new TeamMember();
      		List list = new ArrayList();
      		try {
      			list = jdbc.getProjectManager();
      		} catch (Exception e) {
      		Iterator itr = list.iterator();
      			teamMember = (TeamMember);
      		return new ModelAndView("NewMemberEntry","Model",list);
      Here I am returning thr List containing the data.

      In the JSP I am trying to iterate the List as follows:

      <TD align="left">
      					<select name="projectManager">
      					<option value ="0">Select a project manager</option>
      				<c:forEach var="managerList" items="${Model}">
      						<option value ="10"><c:out value="${teamMember.projectManagerName}"/></option>
      Here I am able to get the List size as 3. But unable to display the data in the dropdown.

      What should I do?


      • #4
        You appear to be assigning a variable, managerList, in your c:forEach but then not using it in the code within the loop. Also, you're not doing anything with teamMember in your Java code (you're just looping through them). If you change the name of your var in the JSP, thus, I imagine it will work (assuming that the list in the object called 'Model' is a list of TeamMember objects - if not, make sure it is in your controller code).

        		<c:forEach var="teamMember" items="${Model}">
        						<option value ="10"><c:out value="${teamMember.projectManagerName}"/></option>


        • #5
          Hi jonmor, Thank you very much. It is working.