国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

Struts2+Hibernate實(shí)現(xiàn)數(shù)據(jù)分頁(yè)的方法

2019-11-26 14:28:57
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Struts2+Hibernate實(shí)現(xiàn)數(shù)據(jù)分頁(yè)的方法。分享給大家供大家參考,具體如下:

1.用Hibernate實(shí)現(xiàn)分頁(yè)技術(shù):

/*** 使用hql語句進(jìn)行分頁(yè)查詢* @param hql 需要查詢的hql語句* @param offset 第一條記錄索引* @param pageSize 每頁(yè)需要顯示的記錄數(shù)* @return 當(dāng)前頁(yè)的所有記錄*/@SuppressWarnings("unchecked")public List findByPage(final String hql,final int offset, final int pageSize){    //通過一個(gè)HibernateCallback對(duì)象來執(zhí)行查詢    List list = getHibernateTemplate()      .executeFind(new HibernateCallback()    {      //實(shí)現(xiàn)HibernateCallback接口必須實(shí)現(xiàn)的方法      public Object doInHibernate(Session session)        throws HibernateException, SQLException      {        //執(zhí)行Hibernate分頁(yè)查詢        List result = session.createQuery(hql)          .setFirstResult(offset)          .setMaxResults(pageSize)          .list();        return result;      }    });    return list;}// 獲取總記錄數(shù)public int getRows(String hql) {    return getHibernateTemplate().find(hql).size();}

2.在Action里調(diào)用Hibernate實(shí)現(xiàn)分頁(yè)技術(shù)的方法,并跳轉(zhuǎn)到顯示界面:

// 分頁(yè)@SuppressWarnings("unchecked")public String paging() {    String hql = "from Income"; // 分頁(yè)的數(shù)據(jù)表    int pageSize = 3; // 每頁(yè)顯示記錄的條數(shù)    int allRows = service.getRows(hql); // 記錄總數(shù)    int allPage = 0; // 總頁(yè)數(shù)    int offset = getPage() + 1; // 第一條記錄的索引    /*if (rows % size != 0) {      pageSize = rows / size + 1;    } else {      pageSize = rows / size;    }*/    allPage = (allRows - 1) / pageSize + 1; // 計(jì)算總頁(yè)數(shù)    List<Income> income = service.findByPage(hql, (offset-1)*pageSize, pageSize);    request.setAttribute("allPage", allPage);    request.setAttribute("offset", offset);    request.setAttribute("income", income);    return "paging";}

3.struts.xml配置:

<action name="income"    class="com.xqh.action.IncomeAction">    <!-- 為兩個(gè)邏輯視圖配置視圖頁(yè)面 -->    <result name="error">/error.jsp</result>    <result name="paging">/income/income_list.jsp</result>    <result name="update">/income/income_edit.jsp</result></action>

4.顯示界面income_list.jsp

<%@ page language="java" pageEncoding="GBK"%><%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><head>  <title>收入列表</title>  <meta http-equiv="pragma" content="no-cache">  <meta http-equiv="cache-control" content="no-cache">  <meta http-equiv="expires" content="0">  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  <meta http-equiv="description" content="This is my page">  <link rel="stylesheet" type="text/css" href="../images/styles.css"></head><body>  <div class="div1">    <table width="100%" cellpadding="0" cellspacing="0" border="0"      align="center">      <tr>        <td class="td_title1">          ?當(dāng)前位置:收入管理>>查看收入        </td>      </tr>      <tr>        <td bgcolor="#FFFFFF" height="50">          <br>          <table border="1" align="center" width="700" cellpadding="1"            cellspacing="1" bgcolor="#036500" bordercolor="#FFFFF">            <tr bgcolor="#FFFFFF">              <td class="tb_tl" align="center">                收入編號(hào)              </td>              <td class="tb_tl" align="center">                日期              </td>              <td class="tb_tl" align="center">                方式              </td>              <td class="tb_tl" align="center">                金額              </td>              <td class="tb_tl" align="center">                項(xiàng)目              </td>              <td class="tb_tl" align="center">                來源              </td>              <td class="tb_tl" align="center">                人員              </td>              <td class="tb_tl" align="center">                備注              </td>              <td class="tb_tl" align="center">                操作              </td>            </tr>            <s:iterator value="#request.income">              <tr bgcolor="#FFFFFF">                <td align="center"><s:property value="id"/></td>                <td align="center"><s:date name="date" format="yyyy-MM-dd"/></td>                <td align="center"><s:property value="style"/></td>                <td align="center"><s:property value="money"/></td>                <td align="center"><s:property value="project"/></td>                <td align="center"><s:property value="source"/></td>                <td align="center"><s:property value="personnel"/></td>                <td align="center"><s:property value="remarks"/></td>                <td align="center">                  <a href="javascript:if(confirm('確定要?jiǎng)h除${id}嗎?'))location='income!del?id=${id}'">刪除</a>                  <a href="javascript:if(confirm('確定要修改${id}嗎?'))location='income!updateTo?id=${id}'">修改</a>                </td>              </tr>            </s:iterator>          </table>          <center>              總共有${allPage}頁(yè),   當(dāng)前是第${offset}頁(yè)              <a href="income!paging?page=0"><font size="2" color="blue">首頁(yè)</font></a>              <a href="javascript:if(${offset}>1)location='income!paging?page=${page-1}'"><font size="2" color="red">上一頁(yè)</font></a>              <a href="javascript:if(${offset}<${allPage})location='income!paging?page=${page+1}'"><font size="2" color="red">下一頁(yè)</font></a>              <a href="income!paging?page=${allPage-1}"><font size="2" color="blue">末頁(yè)</font></a>          </center>        </td>      </tr>    </table>  </div></body>

5.分頁(yè)結(jié)果:

本文章未提供底層數(shù)據(jù)庫(kù)中的實(shí)現(xiàn),但只要掌握分頁(yè)原理,相信這問題不大。具體分頁(yè)原理可參照前面一篇:《Hibernate框架數(shù)據(jù)分頁(yè)技術(shù)實(shí)例分析

希望本文所述對(duì)大家基于Hibernate框架的Java程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 增城市| 台南县| 健康| 兴隆县| 霍林郭勒市| 静海县| 永胜县| 肇源县| 诏安县| 福安市| 南木林县| 嘉义市| 石河子市| 徐汇区| 徐州市| 新龙县| 大田县| 邢台市| 始兴县| 岑巩县| 锡林郭勒盟| 镇坪县| 延津县| 多伦县| 苏州市| 神池县| 齐齐哈尔市| 武夷山市| 宜阳县| 唐河县| 精河县| 翁牛特旗| 徐州市| 泾阳县| 息烽县| 息烽县| 鄢陵县| 齐齐哈尔市| 孟津县| 故城县| 彭山县|