开发者

foreach loop is not working on opened pop up

For each loop is not working in opened popup while the same collection i can see when I write it on page

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>my cart</title>
</head>
<body>开发者_JS百科;
<% ArrayList cart=(ArrayList)session.getAttribute("Cart"); 
out.println(cart);
//this line is working
%>
<table>
      <tr bgcolor="#EAEAFF">       
   <td><b>Product ID</b></td>   

  </tr>
  <c:forEach items="${cart}" var="current">
        <tr>
           <td><c:out value="${current}"/></td>            
        </tr>
      </c:forEach>      
      </table>
</body>
</html>


I see two problems with the code you posted:

1 - the case for cart is wrong. In the following code you must have Cart with a capital C:

<c:forEach items="${Cart}" var="current">

Also, this code

...
<% ArrayList cart=(ArrayList)session.getAttribute("Cart"); 
out.println(cart);
//this line is working
%>
...
<c:forEach items="${cart}" var="current">

does not pick up the cart variable you created with the scriptlet (if that's what you are thinking). cart is a local variable in the servlet generated from your JSP. The tag won't be able to access it).

2 - where is the taglib declaration for your c:forEach tag? Something like:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

If the server does not recognize the tag as what it is, it will output the thing directly into the response. Your browser won't recognize <c:forEach> and will ignore it (i.e. you have it in the source code but it is not displayed on the screen).

Use the following code for your JSP and it should work:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>my cart</title>
</head>
<body>
<table>
  <tr bgcolor="#EAEAFF">       
    <td><b>Product ID</b></td>   
  </tr>
  <c:forEach items="${Cart}" var="current">
    <tr>
     <td><c:out value="${current}" /></td>            
    </tr>
  </c:forEach>      
 </table>
</body>
</html>


<c:forEach items="${sessionScope.cart}" var="current">

This should do the trick, you are trying to get a variable that is probably out of the default scope (page).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜