如何一次打印一行值,如何在JSTL中迭代列表

时间:2014-03-03 09:17:34

标签: java jsp collections jstl

我正在开发web应用程序。在jsp页面中,我使用Scriptlets标签迭代列表。但我想在JSTL中执行相同的结果。我该怎么办?

此处列表包含数据库的列名。

这里eList包含表数据(表的值)

以下是我的JSP代码:

  <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代码

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);
    }
}

我怎么能用JSTL标签来做那件事。非常感谢你

4 个答案:

答案 0 :(得分:1)

list1在请求范围内,因此您可以像您一样进行迭代,以打印SR.No.你可以使用LoopTagStatus

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

记住

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

另见

答案 1 :(得分: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">......

..

答案 2 :(得分:0)

请参阅以下代码。

<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>

答案 3 :(得分:0)

喜欢下面

 <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>