• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2321
  • Last Modified:

JSTL forEach Tag

I have the following in the JSP. Tag libs are all configured properly. The array list object I want to iterate through is also fine, ie., the sys.out prints out values in the array list. But I get an empty drop-down list. What is wrong? How do I fix it? Please help ASAP.

<% ArrayList projects = DBUtil.getList(DBUtil.PROJECT);
     if(projects == null ) {
          System.out.println("PROJECT IS NULL");
     } else {
          System.out.println("PROJECT IS "+projects);    
     }
%>
<SELECT>
    <c:forEach var="project" items="${projects}">
     <OPTION value=<c:out value="${project}"/>><c:out value="${project}"/></OPTION>
    </c:forEach>
</SELECT>

Thanks,
Padma.
0
vpadma
Asked:
vpadma
  • 2
1 Solution
 
TimYatesCommented:
Does:

---------------------

<OPTION value='<c:out value="${project}"/>'><c:out value="${project}"/></OPTION>

---------------------

(with single quotes) work any better?

What if you replace it with:

---------------------

<%
  if( projects != null )
  {
%>
Size equals <%= projects.size() %>
<SELECT>
<%
  for( java.util.Iterator i = projects.iterator() ; i.hasNext() ; )
  {
    String str = (String)i.next()
%>
    <OPTION value='<%= str %>'><%= str %></OPTION>
<%
  }
%>
</SELECT>
<%
  }
%>

---------------------

Does it fill then?  Guess so...but it will let you check that you are getting data back :-)
0
 
rrzCommented:
You have set "projects" as a scripting variable.
try adding a line in the scriptlet.
<% ArrayList projects = DBUtil.getList(DBUtil.PROJECT);
    if(projects == null ) {
         System.out.println("PROJECT IS NULL");

    } else {
         System.out.println("PROJECT IS "+projects);  
         pageContext.setAttribute("mylistarray",projects);
    }
%>
then access in tags ${mylistarray}
0
 
rrzCommented:
>${projects}
this expression will return value of the page-scoped variable named "projects" if one had existed. But "projects" is your scripting variable.
Please use the JSP topic area for JSTL questions.
0
 
vpadmaAuthor Commented:
Perfect. Although I found out yeterday and modified it as
<c_rt:set var='projects' value='<%= DBUtil.getList(DBUtil.PROJECT) %>'/>

that was the reason my code was not working earlie,  I appreciate your help.

Padma.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now