2

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

4 Answers4

1

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

Community
  • 1
  • 1
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
  • Thank you. While printing ${data} it's printing Object ([Ljava.lang.Object;@89ffb18) –  Mar 03 '14 at 09:37
  • @codegeek : Ok. That means it is not `String`. That is why I asked you before what are the contents of list? – Aniket Kulkarni Mar 03 '14 at 09:56
  • @codegeek : I am interested in column names and their data types. Because that is giving you an object when you iterate the list. – Aniket Kulkarni Mar 03 '14 at 10:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48858/discussion-between-codegeek-and-aniket-kulkarni) –  Mar 03 '14 at 11:02
0
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">......

..

Uppala
  • 13
  • 3
0

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>
Gautam
  • 3,276
  • 4
  • 31
  • 53
  • Thank you very much @Gautam. Here I am doing pagination.I am fetching 10 records from my table each time, If I given ${loop.count} it always showing 1 to 10 records only because it's loop count. So how can I do that one. –  Mar 04 '14 at 12:11
  • glad i could help :). For that you need to set some more Attribute in your request Like "Total no of records" and "Page number" – Gautam Mar 04 '14 at 16:00
0

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>
Prabha
  • 707
  • 2
  • 11
  • 27