1

我有这个困境,不知道如何解决它。

我有一个 ArrayList 从我的数据库中获取信息并在 html 表上“绘制”它。问题是信息越大,页面越长。如果它有 2000 条记录,那么表格就会变得那么长,并且用户向下滚动会很痛苦。

我想制作一个 NEXT 和 PREVIOUS 按钮,但我不知道该怎么做。我所拥有的,抱歉是葡萄牙语...

ArrayList<ArrayRegistos> listaRegistos = (ArrayList<ArrayRegistos>) request.getAttribute("listaRegistos");

if (listaRegistos.size() == 0) {

    out.println("<h2>No records</h2>");
    %><%
} else {%><%
            //out.println("<br/>");

            //out.println("<span id=\"cor_tabela\" style=\"color: black;\">");
            out.println("<table id=\"myTable\" border=3 width=\"1100\"height=\"1\">");
            out.println("<thead>");
            out.println("<tr>");
            out.println("<center><td>Estado</td><td>Data Início</td><td>Hora Início</td><td>Técnico</td><td>Serviço</td><td>Descricao</td><td>Data Fim</td><td>Hora Fim</td></center>");
            //out.println("</span>");
            out.println("</tr>");
            out.println("</thead>");
            out.println("<tbody>");
            for (ArrayRegistos i : listaRegistos) {
                out.println("<input type=\"hidden\" name=\"teste\" value=\""+i.getId()+"\"> ");
            //  out.println("<tr>");
            //  out.println("<td>");
                if(i.getEstado().equals("Pendente")){
                    out.println("<tr class=\"pendente\">");
                    out.println("<td>");
                    out.println(String.format("<center><a href=\"DetalheRegisto?detalhe="+i.getId()+"\"><span id=\"cor\" style=\"color: red ;\">"+i.getEstado()+"</span></a></center>"));
                }else if(i.getEstado().equals("Concluído")){
                    out.println("<tr class=\"concluido\">");
                    out.println("<td>");
                    out.println(String.format("<center><a href=\"DetalheRegisto?detalhe="+i.getId()+"\"><span id=\"cor\" style=\"color: green ;\">"+i.getEstado()+"</span></a></center>"));
                }else if(i.getEstado().equals("Validado")){
                    out.println("<tr class=\"validado\">");
                    out.println("<td>");
                    out.println(String.format("<center><a href=\"DetalheRegisto?detalhe="+i.getId()+"\"><span id=\"cor\" style=\"color: blue ;\">"+i.getEstado()+"</span></a></center>"));
                }
                //out.println(String.format("<center>"+i.getEstado()+"</center>"));
                out.println("</td>");
                out.println("<td>");
                out.println(String.format("<center>"+i.getDataInicio()+"</center>"));
                out.println("</td>");               
                out.println("<td>");
                out.println(String.format("<center>"+i.getHoraInicio()+"</center>"));
                out.println("</td>");
                out.println("<td>");
                out.println(String.format("<center>"+i.getTecnico()+"</center>"));
                out.println("</td>");
                out.println("<td>");
                out.println(String.format("<center>"+i.getServico()+"</center>"));
                out.println("</td>");
                out.println("<td>");    
                String descricao = i.getDescricao();
                if (descricao!=null && descricao.length()> 60){

                    out.println("</span>");
                    descricao=descricao.substring(0,40);
                    out.println("<center><span class=\"dropt\" title=\"\">"+descricao+"...</center>");
                    out.println("<span style=\"color: black ;\">"+i.getDescricao()+"</span>");
                    //out.println(String.format("<center>"+descricao+"...</center>"));
                    //System.out.println(descricao);
                }else if(descricao==null){
                    descricao="Sem descrição";
                    out.println(String.format("<center>"+descricao+"</center>"));
                }else if(descricao.isEmpty()){
                    descricao="Sem descrição";
                    out.println(String.format("<center>"+descricao+"</center>"));

                }else{          
                    out.println(String.format("<center>"+i.getDescricao()+"</center>"));
                }
                //out.println(String.format("<center>"+i.getDescricao()+"</center>"));
                out.println("</td>");
                out.println("<td>");
                out.println(String.format("<center>"+i.getDataFim()+"</center>"));
                out.println("</td>");
                out.println("<td>");
                out.println(String.format("<center>"+i.getHoraFim()+"</center>"));
                out.println("</td>");
                out.println("</tr>");



            out.println("</tbody>");
            out.println("</table>");



        }

%></center>
4

1 回答 1

1

我知道有些人认为使用 scriptlet 是一种罪过。但这里有一个演示页面。

<%@ page import="java.util.*" %>
<%
   ArrayList<String> list = new ArrayList<String>();
   for(int i = 0; i < 35; i++){
       list.add("item" + i);
   }
   int totalCount = 0;
   int perPage = 10;
   int pageStart = 0; 
   String start = request.getParameter("start");
   if(start != null)pageStart = Integer.parseInt(start);
   if(pageStart < 0)pageStart = 0;
   totalCount = list.size();
   if(pageStart >= totalCount)pageStart = pageStart - perPage;
%>
<a href="<%=request.getRequestURL()%>?start=<%=pageStart - 10 %>">Previous</a>
<%=pageStart + 1 %> - <%=pageStart + 10 %> 
<a href="<%=request.getRequestURL()%>?start=<%=pageStart + 10 %>">Next</a><br/>
<%
   for(int x = pageStart; x < pageStart + perPage && x < list.size(); x++){
       out.print((String)list.get(x) + "<br/>");
   }
%>

如果可以使用 JSTL,那么我们就可以避免使用 scriptlet。

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="total" scope="session" value="${fn:length(list)}"/>
<c:set var="perPage" scope="session"  value="10"/>
<c:set var="pageStart" value="${param.start}"/>
<c:if test="${empty pageStart or pageStart < 0}">
    <c:set var="pageStart" value="0"/>
</c:if>
<c:if test="${total <= pageStart}">
    <c:set var="pageStart" value="${pageStart - perPage}"/>
</c:if>
<a href="?start=${pageStart - perPage}">Previous</a>${pageStart +1} - ${pageStart + perPage} 
<a  href="?start=${pageStart + perPage}">Next</a><br/>
<c:forEach var="item" items="${list}" begin="${pageStart}" end="${pageStart + perPage - 1}" >
        ${item}<br/>
</c:forEach>   
于 2012-09-03T18:00:58.710 回答