java-SSH2实现数据库和界面的分页

时间:2021-07-30 00:21:29

分页应该是在我们开发web应用时经常要做的工作,能够比较简洁的实现数据库和视图层的分页十分重要。

在数据库层利用hibernate进行数据库的分页,将从数据库中查询出的数据封装为javabean;在视图层就可以方便的实现分页。

创建PageBean

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.fishing.common.bean;
 
import java.util.List;
@SuppressWarnings("unchecked")
public class PageBean {
   
  private List list; // 要返回的某一页的记录列表
 
  private int allRow; // 总记录数
  private int totalPage; // 总页数
  private int currentPage; // 当前页
  private int pageSize; // 每页记录数
 
   
  private boolean isFirstPage; // 是否为第一页
  private boolean isLastPage; // 是否为最后一页
  private boolean hasPreviousPage; // 是否有前一页
  private boolean hasNextPage; // 是否有下一页
 
  public List getList() {
    return list;
  }
 
  public void setList(List list) {
    this.list = list;
  }
 
  public int getAllRow() {
    return allRow;
  }
 
  public void setAllRow(int allRow) {
    this.allRow = allRow;
  }
 
  public int getTotalPage() {
    return totalPage;
  }
 
  public void setTotalPage(int totalPage) {
    this.totalPage = totalPage;
  }
 
  public int getCurrentPage() {
    return currentPage;
  }
 
  public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
  }
 
  public int getPageSize() {
    return pageSize;
  }
 
  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }
 
  /** */
  /**
   * 初始化分页信息
   */
  public void init() {
    this.isFirstPage = isFirstPage();
    this.isLastPage = isLastPage();
    this.hasPreviousPage = isHasPreviousPage();
    this.hasNextPage = isHasNextPage();
  }
 
  /** */
  /**
   * 以下判断页的信息,只需getter方法(is方法)即可
   *
   * @return
   */
 
  public boolean isFirstPage() {  
    return (currentPage == 1);// 如是当前页是第1页  
  }  
  public boolean isLastPage() {  
 return currentPage == totalPage; //如果当前页是最后一页  
}  
  public boolean isHasPreviousPage() {  
 return currentPage != 1; //只要当前页不是第1页  
}  
  public boolean isHasNextPage() {  
 return currentPage != totalPage; //只要当前页不是最后1页  
}  
  /** */
  /**
   * 计算总页数,静态方法,供外部直接通过类名调用
   *
   * @param pageSize
   *      每页记录数
   * @param allRow
   *      总记录数
   * @return 总页数
   */
  public static int countTotalPage(final int pageSize, final int allRow) {
    int totalPage = allRow % pageSize == 0 ? allRow / pageSize : allRow
        / pageSize + 1;
    return totalPage;
  }
 
  /** */
  /**
   * 计算当前页开始记录
   *
   * @param pageSize
   *      每页记录数
   * @param currentPage
   *      当前第几页
   * @return 当前页开始记录号
   */
  public static int countOffset(final int pageSize, final int currentPage) {
    final int offset = pageSize * (currentPage - 1);
    return offset;
  }
 
  /** */
  /**
   * 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替
   *
   * @param page
   *      传入的参数(可能为空,即0,则返回1)
   * @return 当前页
   */
  public static int countCurrentPage(int page) {
    final int curPage = (page == 0 ? 1 : page);
    return curPage;
  }
}

在Dao的抽象接口BaseDao中添加方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public List queryForPage(final String hql, final int offset,
      final int length);
在Dao的实现类JianSheDWDaoImpl中实现方法
public List queryForPage(final String hql, final int offset,
      final int length) {
    List list = getHibernateTemplate().executeFind(new HibernateCallback() {
 
      public Object doInHibernate(Session session)
          throws HibernateException, SQLException {
        Query query = session.createQuery(hql);
        query.setFirstResult(offset);
        query.setMaxResults(length);
        List list = query.list();
        return list;
 
      }
    });
 
    return list;
  }

在service抽象层接口JianSheDWService中添加方法:

?
1
public PageBean queryForPage(int pageSize,int currentPage); 

在service实现类中实现方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public PageBean queryForPage(int pageSize, int page) {
    final String hql = "from JianSheDWBean"; // 查询语句
    int allRow = this.baseDao.getAllRowCount(hql); // 总记录数
    int totalPage = PageBean.countTotalPage(pageSize, allRow); // 总页数
    final int offset = PageBean.countOffset(pageSize, page); // 当前页开始记录
    final int length = pageSize; // 每页记录数
    final int currentPage = PageBean.countCurrentPage(page);
    List<JianSheDWBean> list = this.baseDao.queryForPage(hql, offset, length); // "一页"的记录
 
    // 把分页信息保存到Bean中
    PageBean pageBean = new PageBean();
    pageBean.setPageSize(pageSize);
    pageBean.setCurrentPage(currentPage);
    pageBean.setAllRow(allRow);
    pageBean.setTotalPage(totalPage);
    pageBean.setList(list);
    pageBean.init();
    return pageBean;
 
  }

在视图层action中建立分页模型

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.fishing.action.lcq; 
import com.fishing.common.bean.JianSheDWBean;
import com.fishing.common.bean.PageBean;
import com.fishing.service.lcq.JianSheDWService;
import com.opensymphony.xwork2.ActionSupport;
 
@SuppressWarnings("serial")
public class GetInfoJSDWListAction extends ActionSupport {
 
  private int page; // 第几页
 
  private PageBean pageBean; // 包含分布信息的bean
 
  private JianSheDWBean jianSheDWBean;
  // private PageBean page;
  private JianSheDWService jianSheDWService;
 
  public int getPage() {
    return page;
  }
 
  public void setPage(int page) {
    this.page = page;
  }
 
  public PageBean getPageBean() {
    return pageBean;
  }
 
  public void setPageBean(PageBean pageBean) {
    this.pageBean = pageBean;
  }
 
  public JianSheDWBean getJianSheDWBean() {
    return jianSheDWBean;
  }
 
  public void setJianSheDWBean(JianSheDWBean jianSheDWBean) {
    this.jianSheDWBean = jianSheDWBean;
  }
 
  public JianSheDWService getJianSheDWService() {
    return jianSheDWService;
  }
 
  public void setJianSheDWService(JianSheDWService jianSheDWService) {
    this.jianSheDWService = jianSheDWService;
  }
 
  @Override  
   public String execute() throws Exception {  
     
   //分页的pageBean,参数pageSize表示每页显示记录数,page为当前页  
   this.pageBean = jianSheDWService.queryForPage(10, page);  
   return SUCCESS;
  }
}

在jsp中编写分页

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<tr class="odd">
                          <td>
                              
                          </td>
                          <td>
                            <s:if test="%{pageBean.currentPage == 1}">  
                           首页   上一页
                          </s:if>
                            <s:else>
                              <a href="jianguan/getJSDWInfos.action?page=1">首页</a>
                              <a
                                href="jianguan/getJSDWInfos.action?page=<s:property value="%{pageBean.currentPage-1}"/>" />上一页</a>
 
                            </s:else>
                          </td>
                          <td>
                            <s:if test="%{pageBean.currentPage != pageBean.totalPage}">
 
                              <a
                                href="jianguan/getJSDWInfos.action?page=<s:property value="%{pageBean.currentPage+1}"/>">下一页</a>
                              <a
                                href="jianguan/getJSDWInfos.action?page=<s:property value="pageBean.totalPage"/>">尾页
                              </a>
                            </s:if>
                            <s:else
                           下一页 尾页 
                          </s:else>
                          </td>
 
                          <td>
                            <div align="center">
                              页次
                              <s:property value="pageBean.currentPage" />
                              /
                              <s:property value="pageBean.totalPage" />
                                
                              <s:property value="pageBean.allRow" />
                              记录
                            </div>
                            <div align="center"></div>
                          </td>
                        </tr>

 上面只是代码的实现,没有说明配置文件的配置,读者根据情况配置。

希望本文所述对你有所帮助,SSH2实现数据库和界面的分页内容就给大家介绍到这里了。希望大家继续关注我们的网站!想要学习java可以继续关注本站。