用于分页目的的 Java taglib

发布于 2024-12-04 06:35:49 字数 807 浏览 0 评论 0 原文

我正在尝试使用 JSTL 在现有的 JSP 代码中创建分页,但我不确定我在示例之一中看到的以下语法。

    <sql:setDataSource var="dataSrc"
     url="jdbc:oracle:thin:@127.0.0.1:1521:database_name"
     driver="oracle.jdbc.driver.OracleDriver"
     user="user_name" password="pass_word"/>

然后运行查询:

    <sql:query var="queryResults" dataSource="${dataSrc}">
    select system_id, employeename from employees
    </sql:query>

然后,在网页上显示结果:

    <table>
    <tr>
    <th>ID</th>....

我的问题是 这是我必须使用的标准语法还是我该输入什么在这个 query var= ? 以及 dataSource ="{dataSrc}" 中,这是标准代码还是我必须修改它?

如果有人可以指导我如何在每页上显示限制为 10 的表格或行的源示例,我将非常感激。

I'm trying to use JSTL to create a pagination in my existing JSP code but I am not sure about the following syntax which I've seen in one of the examples.

    <sql:setDataSource var="dataSrc"
     url="jdbc:oracle:thin:@127.0.0.1:1521:database_name"
     driver="oracle.jdbc.driver.OracleDriver"
     user="user_name" password="pass_word"/>

Then you run a query:

    <sql:query var="queryResults" dataSource="${dataSrc}">
    select system_id, employeename from employees
    </sql:query>

Then, you display the results on the web page:

    <table>
    <tr>
    <th>ID</th>....

My question is <sql: query var="queryResults" is this a standard syntax I must use or what do I put inside this query var= ? and also the dataSource ="{dataSrc}" is this a standard code or must I modify this?

If someone can direct me to a source example of how to display my tables or rows limited to 10 on each page I'd be very grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗酒趁年少 2024-12-11 06:35:49

我以前写过自己的分页标签 - 我真希望我没有写过。

我建议您查看 displaytag。它是一个易于使用的开源标签库,几乎可以满足您所有的分页要求。它生成的 HTML 是干净的并且符合标准,最重要的是,它已经被编写了 :-)

对于正确的分页,用法可以像这样简单:

<display:table name="${paginatedList}" partialList="true" pageSize="10" size="${totalNumberOfItems}" />

${paginationList} 是结果集的第 1 到 10 项(您将需要在 DAO 中编写代码以从数据库中分块检索项目,大多数 ORM 库都可以让您轻松完成此操作,或者您也可以使用 SQL 来完成此操作 - 请参阅下面的示例)。

${totalNumberOfItems} 是如果您没有将结果数量限制为页面大小 10,则查询将返回的项目总数。同样,大多数 ORM 框架允许您相当轻松地执行此操作,或者您也可以这样做使用 SQL(例如 select count(*)...) - 见下文...

有关 Hibernate 中分页的示例,请查看 这个。有关 JDBC 示例,请查看

I've written my own pagination tag before - I really wish I hadn't.

I would suggest you take a look at displaytag. It's an easy to use open source tag library that should pretty much cover all your pagination requirements. The HTML it produces is clean and standards compliant and best of all, it's already been written :-)

For proper pagination, usage can be as simple as this:

<display:table name="${paginatedList}" partialList="true" pageSize="10" size="${totalNumberOfItems}" />

${paginatedList} is e.g. items 1 to 10 of your resultset (you'll need to write the code in your DAO to retrieve items from your database in chunks. Most ORM libraries allow you to do this pretty easily, or you can do it with SQL - see below for examples).

${totalNumberOfItems} is the total number of items your query would return if you didn't limit the number of results to a page size of 10. Again, most ORM frameworks allow you to do this fairly easily, or again you can do it with SQL (e.g. select count(*)...) - see below...

For an example of pagination in Hibernate, take a look at this. For a JDBC example, have a look at this.

疏忽 2024-12-11 06:35:49

有了一点额外的知识,您就可以使用

下面的参考指南中的 Struts 2.0 分页 JSP 代码 spinet,

<pg:pager
items="<%= searchResultPagerSize %>"
index="center"
url=""
maxPageItems="<%= numberOfItemsPerPage %>"
maxIndexPages="<%= maxNumberOfPagesToShow %>"
isOffset="<%= true %>"
export="offset,currentPageNumber=pageNumber"
scope="request">

<%-- START: the visual element of the pager --%>
<%--
START: You can take any of the pages in the pagertags war file in the folder /WEB-INF/JSP and put them in here instead
--%>
<pg:index export="totalItems=itemCount">
    <pg:page export="firstItem, lastItem">
        <div class="resultInfo">
            Displaying results <strong><%= firstItem%>-<%= lastItem%></strong> of <strong><%= totalItems%></strong> found
        </div>
    </pg:page>

    <div class="rnav">
        <span class="rnavLabel">Results:</span> 
        <pg:prev export="pageUrl">
            <a href="<%= pageUrl%>" class="rnavLink">« Prev</a> 
        </pg:prev>
        <pg:pages export="pageUrl,pageNumber,firstItem,lastItem">
            <% if (pageNumber == currentPageNumber) {%>
             <span class="rnavCurr"><%= firstItem%>-<%= lastItem%></span>
            <% } else {%>
             <a href="<%= pageUrl%>" class="rnavLink"><%= firstItem%>-<%= lastItem%></a>
            <% }%>
        </pg:pages>
        <pg:next export="pageUrl">
              <a href="<%= pageUrl%>" class="rnavLink">Next »</a>
        </pg:next>
    </div>
</pg:index>
<%--
END: You can take any of the pages in the pagertags war file in the folder /WEB-INF/JSP and put them in here instead
--%>
<%-- END: the visual element of the pager --%>

请参阅 此处获取完整参考。我认为这正是您所需要的。

With a little bit of extra knowledge you could have used Struts 2.0 pagination

JSP code spinet from below reference guide

<pg:pager
items="<%= searchResultPagerSize %>"
index="center"
url=""
maxPageItems="<%= numberOfItemsPerPage %>"
maxIndexPages="<%= maxNumberOfPagesToShow %>"
isOffset="<%= true %>"
export="offset,currentPageNumber=pageNumber"
scope="request">

<%-- START: the visual element of the pager --%>
<%--
START: You can take any of the pages in the pagertags war file in the folder /WEB-INF/JSP and put them in here instead
--%>
<pg:index export="totalItems=itemCount">
    <pg:page export="firstItem, lastItem">
        <div class="resultInfo">
            Displaying results <strong><%= firstItem%>-<%= lastItem%></strong> of <strong><%= totalItems%></strong> found
        </div>
    </pg:page>

    <div class="rnav">
        <span class="rnavLabel">Results:</span> 
        <pg:prev export="pageUrl">
            <a href="<%= pageUrl%>" class="rnavLink">« Prev</a> 
        </pg:prev>
        <pg:pages export="pageUrl,pageNumber,firstItem,lastItem">
            <% if (pageNumber == currentPageNumber) {%>
             <span class="rnavCurr"><%= firstItem%>-<%= lastItem%></span>
            <% } else {%>
             <a href="<%= pageUrl%>" class="rnavLink"><%= firstItem%>-<%= lastItem%></a>
            <% }%>
        </pg:pages>
        <pg:next export="pageUrl">
              <a href="<%= pageUrl%>" class="rnavLink">Next »</a>
        </pg:next>
    </div>
</pg:index>
<%--
END: You can take any of the pages in the pagertags war file in the folder /WEB-INF/JSP and put them in here instead
--%>
<%-- END: the visual element of the pager --%>

see here for the complete reference. I think this is what you exactly need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文