Conversion of for loop from JSP to JSTL

Discussions

Web tier: servlets, JSP, Web frameworks: Conversion of for loop from JSP to JSTL

  1. Conversion of for loop from JSP to JSTL (2 messages)

    Hi, I got a tricky problem, when I convert my JSP for loop into JSTL for loop, I got two String variables in my JSP for-loop, obviously which get iterate every time when my loop get executed and new values are coming.
    But when I try to convert it into JSTL for loop( forEach), I am not getting the desired result which I am looking.
    I am looking for help, if anyone knows plz help me. Its urgent.

    Here is my code:-


    JSP for loop


    <% try {
    for(int i=0; i<alCompanyID.size(); i++)
    {
    String sCompanyName = (String)alCompanyName.get(i);
    String sCompanyID = ((Integer)alCompanyID.get(i)).toString();
    %>
    <option value="<%= sCompanyID %>"><%= sCompanyName %></option>
    <%}}catch(Exception e){}



    JSTL forEcah loop



    <c:catch var="exception">
    <c_rt:forEach begin="0" step="1" end="<%=alCompanyID.size()%>">
    <c_rt:set var="CompanyName" scope="page" value="${alCompanyName}" />
    <c_rt:set var="CompanyID" scope="page" value="${alCompanyID}" />
    <option value="<c_rt:out value="${pageScope.CompanyID}"/>"><c_rt:out value="${pageScope.CompanyName}" /></option>
    </c_rt:forEach>
    </c:catch>



    Thanks
    RD
  2. You're using page scope in the JSTL code. Try using request scope.
  3. Looping using JSTL[ Go to top ]

    The JSP code below might help you.

    <%@ page contentType="text/html; charset=ISO-8859-1" language="java"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

    <%
    java.util.List allCompanyID = new java.util.LinkedList();
    allCompanyID.add("Sun");
    allCompanyID.add("IBM");
    allCompanyID.add("Novel");

    request.setAttribute("allCompanyID", allCompanyID);
    %>

    My Companies

    <SELECT NAME="Test">
        <c:forEach var="id" items="${allCompanyID}" varStatus="loopStatus">
            <option value="${loopStatus.index}">${allCompanyID[loopStatus.index]}</option>
        </c:forEach>
    </SELECT>