Question

I am developing web application.In jsp page I am iterating a list using Scriptlets tags. But I want to execute the same result in JSTL. How Can I do?

Here list contain Column names of Data Base.

Here eList contain Table data (values of a table)

Below is My JSP code:

  <table border="1" cellpadding="5" cellspacing="5">
            <tbody>

                <tr>
                    <th>SNO</th>
                    <c:forEach var="column" items="${list}">
                        <th> ${column} </th>
                    </c:forEach>
                </tr>

 //For above list JSTL exectude successfully but while doing below list is not possible

                <%List eList =(Vector)request.getAttribute("list1");
                    Integer s1 = (Integer) request.getAttribute("sno");
                    Iterator it = eList.iterator();
                    while (it.hasNext()) {
                %>
                <tr> <td><%=++s1%></td><%
                    Object[] row = (Object[]) it.next();
                    for (int k = 0; k < row.length; k++) {
               %> 
                    <td> <%=row[k]%></td>
                    <%    }%>                   
                </tr>
                <%
                    }
                %> 
            </tbody>
        </table>

Servelt code

GetTableData .java:

public class GetTableData extends HttpServlet
{
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        Query query1 = entityManager.createNativeQuery("select * from "
                + schemaName + "." + tableName + "");

        totalNumberOfRecords = query1.getResultList().size();
        query1.setFirstResult(newPageIndex);
        query1.setMaxResults(numberOfRecordsPerPage);

        List list1 = query1.getResultList();
        System.out.println("testszenario" + testszenario.size());
        System.out.println(testszenario);

        request.setAttribute("list1", list1);
        request.getRequestDispatcher("/TableResult.jsp").forward(request,
                response);
    }
}

How can I do that one with JSTL tags.Thanking you very much

Was it helpful?

Solution 2

Do like Below

 <c:forEach var="data" items="${list1}" varStatus="loop">
 <tr>
 <td> ${sno+1} </td>  
 <c:forEach var="innerData" items="${data}">
 <td> ${innerData} </td>                      
 </c:forEach>
 <c:set var="sno" value="${sno+1}"/>
 </tr>
 </c:forEach>

OTHER TIPS

The list1 is in request scope so you can just iterate as you did, for printing SR.No. you can make use of LoopTagStatus

<c:forEach var="data" items="${list1}" varStatus="loop">
      <td> ${loop.count} </td>
      <td> ${data} </td>
</c:forEach>

Remember

${loop.index} starts from 0
${loop.count} starts from 1

See also

Include a jstl jar and include them in jsp page like shown below

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>.............

then by using 
<c:forEach items="${values that are set in any scope}" var="any variable">......

..

Please refer to below code.

<c:forEach var="data" items="${list1}" varStatus="loop">
   <tr>
        <td> ${loop.count} </td>  
        <c:forEach var="innerData" items="${data}">
          <td> ${innerData} </td>
        </c:forEach>
  </tr>
</c:forEach>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top