如何在JSP中执行分页 [英] How to perform pagination in jsp

查看:68
本文介绍了如何在JSP中执行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要分页概念来显示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++;
    }

%>

请帮我IAM新手分页.我只想每页显示10条记录.我通过展示广告代码尝试过,但无法弄清楚.

解决方案

显示广告代码 库是一个开放源代码库,它提供分页功能,同时仍易于使用.

您可以在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>

您可以在此处找到它的基本教程.


已更新:

您不必仅使用<display:table> and <display:column>就可以使用<table>, <tr>, <td>标记. 您可以直接在<display:column>的属性标签内使用EmpList中的字段.

遵循此教程.

i need pagination concept to display 15 records. this is my jsp file.

<%
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++;
    }

%>

please help me iam newbie in pagination. i want to show only 10 records per page. i tried it through display tag but i cant figure it out.

解决方案

The Display Tag library is an open source library which provides pagination functionality while still being easy to use.

You can set your records in request scope from your servlet class.

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

and Then you can use display tag library to display it with pagination.

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

You can find it's basic tutorials here.


Updated:

You need not to use <table>, <tr>, <td> tag only <display:table> and <display:column> is enough. You can directly use fields from EmpList inside property tag of <display:column>.

Follow this tutorial.

这篇关于如何在JSP中执行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆