【问题标题】:How to perform pagination in jsp如何在jsp中进行分页
【发布时间】:2013-07-30 06:07:54
【问题描述】:

我需要分页概念来显示 15 条记录。 这是我的jsp文件。

<%
ArrayList<String> al = new ArrayList();
%>
<%!String s;
int i;%>
<%
al = op.getCountry();
%>
<jsp:scriptlet>
session.setAttribute( "EmpList", al);
</jsp:scriptlet>
<display:table id="domList" pagesize="10" name="sessionScope.EmpList"> 
<table width="100%" border="0" cellpadding="0" cellspacing="0" id="dataTable">

<tr bgcolor="#57e3ff">
    <td><strong>Country</strong></td>
    <td colspan="2" align="center"><strong>Action</strong></td>
</tr>
<% int a = 0, i = 0;
    while (i < al.size()) {
        if ((i + 1) % 2 == 0) {
                                                                                s = "#f3f4f4";
                                                                            } else {
                                                                                s = "#ebebec";
                                                                            }%>

<tr bgcolor="<%=s%>">
    <td><display:column  value="<%=al.get(i)%>" />  </td>
    <td align="center"><div id='basic-modal-cdel'>
            <a href='#' id="<%=al.get(i)%>" onclick="return del();"
               class='basic-cdel'><img src="${pageContext.request.contextPath}/images/delete.png"
                                    alt="Delete" title="Delete" border="0" /></a>
        </div></td>
    <td align="center"><div id='basic-modal-country'>
            <a href='#' id="<%=al.get(i)%>" class='basic-country'
               onclick="validatedit()"><img src="${pageContext.request.contextPath}/images/edit-icon.png"
                                         alt="Update" title="Update" border="0" /></a>
        </div></td>
</tr>

<%
        i++;
        a++;
    }

%>

请帮助我是分页新手。我只想每页显示 10 条记录。我通过显示标签尝试过,但我无法弄清楚。

【问题讨论】:

    标签: jsp jsp-tags


    【解决方案1】:

    Display Tag 库是一个开源库,它提供了分页功能,同时仍然易于使用。

    您可以在 servlet 类的请求范围内设置记录。

    request.setAttribute( "test", new TestList(10, false) );
    

    然后就可以使用显示标签库进行分页显示了。

    <%@taglib uri="http://displaytag.sf.net" prefix="display" %>
    
    
    <display:table name="test" pagesize="15" >
      <display:column property="id" title="ID" />
      <display:column property="name" />
      <display:column property="email" />
      <display:column property="status" />
      <display:column property="description" title="Comments"/>
    </display:table>
    

    你可以找到它的基础教程here


    更新:

    你不需要使用&lt;table&gt;, &lt;tr&gt;, &lt;td&gt;标签,只要&lt;display:table&gt; and &lt;display:column&gt;就足够了。 您可以在&lt;display:column&gt; 的属性标签内直接使用 EmpList 中的字段。

    关注这个tutorial

    【讨论】:

    • 嗨 Ankur 非常感谢 rply。我只是通过它的教程。但是,这对我来说肯定是不够的。我要编辑我的帖子,请查看。
    【解决方案2】:

    您可以使用Display tag library。它为您的 JSP 页面提供分页功能。您只需将对象列表传递给此标记库,它就会为您的页面添加分页。它还支持其他功能,例如排序、分组、导出等。

    【讨论】:

      【解决方案3】:

      我正在使用Display Tag Library,它可以通过飞行完成所有操作。只需将列表传递给 display:table 属性,然后 display 标记将处理其余部分。您也可以使用 EL。它易于使用且易于维护。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-05
        • 2017-01-05
        • 2019-04-23
        • 2019-08-17
        • 2017-10-03
        • 2017-01-20
        相关资源
        最近更新 更多