java SSH2 implements paging of database and interface
- 2020-05-10 18:16:07
- OfStack
Paging should be a common task when we are developing web applications. It is very important to realize the paging of database and view layer in a concise way.
In the database layer, hibernate is used to paginate the database, and the data queried from the database is encapsulated as javabean. Pagination can be easily implemented at the view layer.
Create PageBean
package com.fishing.common.bean;
import java.util.List;
@SuppressWarnings("unchecked")
public class PageBean {
private List list; // Something to return 1 page
private int allRow; // The total number of records
private int totalPage; // Total number of pages
private int currentPage; // The current page
private int pageSize; // Number of records per page
private boolean isFirstPage; // Whether it is the first 1 page
private boolean isLastPage; // Is it the last 1 page
private boolean hasPreviousPage; // If there is a former 1 page
private boolean hasNextPage; // Whether there is the 1 page
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;
}
/** */
/**
* Initializes the split page information
*/
public void init() {
this.isFirstPage = isFirstPage();
this.isLastPage = isLastPage();
this.hasPreviousPage = isHasPreviousPage();
this.hasNextPage = isHasNextPage();
}
/** */
/**
* Information on the following judgment page , just getter methods (is methods ) Can be
*
* @return
*/
public boolean isFirstPage() {
return (currentPage == 1);// If the current page is first 1 page
}
public boolean isLastPage() {
return currentPage == totalPage; // If the current page is last 1 page
}
public boolean isHasPreviousPage() {
return currentPage != 1; // As long as the current page is not first 1 page
}
public boolean isHasNextPage() {
return currentPage != totalPage; // As long as the current page is not the last 1 page
}
/** */
/**
* Total page count , A static method , Called externally directly through the class name
*
* @param pageSize
* Number of records per page
* @param allRow
* The total number of records
* @return Total number of pages
*/
public static int countTotalPage(final int pageSize, final int allRow) {
int totalPage = allRow % pageSize == 0 ? allRow / pageSize : allRow
/ pageSize + 1;
return totalPage;
}
/** */
/**
* Calculates the current page start record
*
* @param pageSize
* Number of records per page
* @param currentPage
* Current page
* @return Current page start record number
*/
public static int countOffset(final int pageSize, final int currentPage) {
final int offset = pageSize * (currentPage - 1);
return offset;
}
/** */
/**
* Calculate current page , if 0 Or requested URL There is no "?page=", To use 1 Instead of
*
* @param page
* Incoming parameter ( May is empty , namely 0, It returns 1)
* @return The current page
*/
public static int countCurrentPage(int page) {
final int curPage = (page == 0 ? 1 : page);
return curPage;
}
}
Add methods to the abstract interface BaseDao of Dao:
public List queryForPage(final String hql, final int offset,
final int length);
in Dao The implementation of the class JianSheDWDaoImpl Middle implementation method
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;
}
Add a method to the service abstraction layer interface JianSheDWService:
public PageBean queryForPage(int pageSize,int currentPage);
Implement the method in the service implementation class:
public PageBean queryForPage(int pageSize, int page) {
final String hql = "from JianSheDWBean"; // The query
int allRow = this.baseDao.getAllRowCount(hql); // The total number of records
int totalPage = PageBean.countTotalPage(pageSize, allRow); // Total number of pages
final int offset = PageBean.countOffset(pageSize, page); // The current page begins recording
final int length = pageSize; // Number of records per page
final int currentPage = PageBean.countCurrentPage(page);
List<JianSheDWBean> list = this.baseDao.queryForPage(hql, offset, length); // "1 page " The record of
// Save the paging information to Bean In the
PageBean pageBean = new PageBean();
pageBean.setPageSize(pageSize);
pageBean.setCurrentPage(currentPage);
pageBean.setAllRow(allRow);
pageBean.setTotalPage(totalPage);
pageBean.setList(list);
pageBean.init();
return pageBean;
}
Build the paging model in the view layer action
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; // What page
private PageBean pageBean; // Containing distribution information 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 {
// paging pageBean, parameter pageSize Represents the number of records displayed per page ,page For the current page
this.pageBean = jianSheDWService.queryForPage(10, page);
return SUCCESS;
}
}
Write pages in jsp
<tr class="odd">
<td>
</td>
<td>
<s:if test="%{pageBean.currentPage == 1}">
Home page on 1 page
</s:if>
<s:else>
<a href="jianguan/getJSDWInfos.action?page=1"> Home page </a>
<a
href="jianguan/getJSDWInfos.action?page=<s:property value="%{pageBean.currentPage-1}"/>" /> on 1 page </a>
</s:else>
</td>
<td>
<s:if test="%{pageBean.currentPage != pageBean.totalPage}">
<a
href="jianguan/getJSDWInfos.action?page=<s:property value="%{pageBean.currentPage+1}"/>"> Under the 1 page </a>
<a
href="jianguan/getJSDWInfos.action?page=<s:property value="pageBean.totalPage"/>"> back
</a>
</s:if>
<s:else>
Under the 1 page back
</s:else>
</td>
<td>
<div align="center">
Page one
<s:property value="pageBean.currentPage" />
/
<s:property value="pageBean.totalPage" />
A total of
<s:property value="pageBean.allRow" />
record
</div>
<div align="center"></div>
</td>
</tr>
is just the implementation of the code above, which does not explain the configuration of the configuration file, and the reader will configure it according to the situation.
I hope this article has been helpful to you. So much for SSH2 to realize the paging content of the database and the interface. We hope you continue to pay attention to our website! Stay tuned to learn about java.