nebula
nebula

Reputation: 4002

Jstl iterate over a List

I am trying to iterate over a list and trying to print the value as:

<jsp:useBean class="com.lftechnology.db.EmployeeDaoImpl" id="empImpl"></jsp:useBean>
<jsp:useBean class="com.lftechnology.employee.Employee" id="employee"></jsp:useBean>

<%
List<com.lftechnology.employee.Employee> empList = null; 
empList = empImpl.getAllEmployee();
out.println(empList.size());
%>


<c:forEach items="${empList}" var="element"> 
  <tr>
    <td>${element.name}</td>
    <td><c:out value="${element.name}" /></td>
     </tr>
</c:forEach>

However, only the size of the list is printed not the name as defined inside jstl. Any help? I want to print the all the attributes of employee object.

Upvotes: 2

Views: 4223

Answers (1)

home
home

Reputation: 12538

Why do you use the scriptlet at all? Something like this should work (I could not test it):

<jsp:useBean class="com.lftechnology.db.EmployeeDaoImpl" id="empImpl"></jsp:useBean>

<c:forEach items="${empImpl.allEmployee}" var="element"> 
  <tr>
    <td>${element.name}</td>
    <td><c:out value="${element.name}" /></td>
  </tr>
</c:forEach>

Some background

Just declaring a variable in a scriptlet does not automatically make it available to the JSTL runtime. In fact JSTL works on the pageContext injected into each JSP at runtime by the container. So if you really need to declare a variable in a scriptlet and want to make it available to JSTL you have to do this explicitly:

<%
List<String> stuff = new ArrayList<String>();
request.setAttribute("mystuff", stuff);
%>

<c:out value="${mystuff" />

Upvotes: 3

Related Questions