- 浏览: 308016 次
- 性别:
- 来自: 河南开封
文章分类
最新评论
-
keven_niu:
好不错哦。。
jquery dialog对话框插件实例弹层效果 -
我是云:
zjuttsw 写道zjuttsw 写道楼主最后两个例子很好。 ...
Java switch-case语句用法 -
ytt06460105:
问题已解决。
使用JDBC访问DB2的问题:no db2jdbc in java.library.path -
ytt06460105:
楼主:按照你的方法做了,又出现了新的错误:[com.aliba ...
使用JDBC访问DB2的问题:no db2jdbc in java.library.path -
zjuttsw:
zjuttsw 写道楼主最后两个例子很好。学习了 不过第三条好 ...
Java switch-case语句用法
先是一个page的bean:
Java代码
package com.leatherstore.other;
public class Page {
/** 是否有上一页 */
private boolean hasPrePage;
/** 是否有下一页 */
private boolean hasNextPage;
/** 每页的数量 */
private int everyPage;
/** 总页数 */
private int totalPage;
/** 当前页*/
private int currentPage;
/** 起始点 */
private int beginIndex;
/** 总记录数*/
private int totalCount;
/**
* @return totalCount
*/
public int getTotalCount() {
return totalCount;
}
/**
* @param totalCount 要设置的 totalCount
*/
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
/** The default constructor */
public Page(){
}
/** construct the page by everyPage
* @param everyPage
* */
public Page(int everyPage){
this.everyPage = everyPage;
}
/** The whole constructor */
public Page(boolean hasPrePage, boolean hasNextPage,
int everyPage, int totalPage,
int currentPage, int beginIndex,int totalCount) {
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
this.everyPage = everyPage;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.totalCount = totalCount;
}
/**
* @return
* Returns the beginIndex.
*/
public int getBeginIndex() {
return beginIndex;
}
/**
* @param beginIndex
* The beginIndex to set.
*/
public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
}
/**
* @return
* Returns the currentPage.
*/
public int getCurrentPage() {
return currentPage;
}
/**
* @param currentPage
* The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return
* Returns the everyPage.
*/
public int getEveryPage() {
return everyPage;
}
/**
* @param everyPage
* The everyPage to set.
*/
public void setEveryPage(int everyPage) {
this.everyPage = everyPage;
}
/**
* @return
* Returns the hasNextPage.
*/
public boolean getHasNextPage() {
return hasNextPage;
}
/**
* @param hasNextPage
* The hasNextPage to set.
*/
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
/**
* @return
* Returns the hasPrePage.
*/
public boolean getHasPrePage() {
return hasPrePage;
}
/**
* @param hasPrePage
* The hasPrePage to set.
*/
public void setHasPrePage(boolean hasPrePage) {
this.hasPrePage = hasPrePage;
}
/**
* @return Returns the totalPage.
*
*/
public int getTotalPage() {
return totalPage;
}
/**
* @param totalPage
* The totalPage to set.
*/
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
}
然后构建一个page的工厂PageUtil:
package com.leatherstore.other;
public class PageUtil {
/**
* Use the origin page to create a new page
*
* @param page
* @param totalRecords
* @return
*/
public static Page createPage(Page page, int totalRecords) {
return createPage(page.getEveryPage(), page.getCurrentPage(),
totalRecords);
}
/**
* the basic page utils not including exception handler
*
* @param everyPage
* @param currentPage
* @param totalRecords
* @return page
*/
public static Page createPage(int everyPage, int currentPage,
int totalRecords) {
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int beginIndex = getBeginIndex(everyPage, currentPage);
int totalPage = getTotalPage(everyPage, totalRecords);
boolean hasNextPage = hasNextPage(currentPage, totalPage);
boolean hasPrePage = hasPrePage(currentPage);
return new Page(hasPrePage, hasNextPage, everyPage, totalPage,
currentPage, beginIndex, totalRecords);
}
private static int getEveryPage(int everyPage) {
return everyPage == 0 ? 10 : everyPage;
}
private static int getCurrentPage(int currentPage) {
return currentPage == 0 ? 1 : currentPage;
}
private static int getBeginIndex(int everyPage, int currentPage) {
return (currentPage - 1) * everyPage;
}
private static int getTotalPage(int everyPage, int totalRecords) {
int totalPage = 0;
if (totalRecords % everyPage == 0)
totalPage = totalRecords / everyPage;
else
totalPage = totalRecords / everyPage + 1;
return totalPage;
}
private static boolean hasPrePage(int currentPage) {
return currentPage == 1 ? false : true;
}
private static boolean hasNextPage(int currentPage, int totalPage) {
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}
然后建一个专用的bean:
package com.leatherstore.hibernate.domain;
import java.util.List;
import com.leatherstore.other.Page;
public class Result {
private Page page; //分页信息
private List content; //每页显示的集合
/**
* The default constructor
*/
public Result() {
super();
}
/**
* The constructor using fields
*
* @param page
* @param content
*/
public Result(Page page, List content) {
this.page = page;
this.content = content;
}
/**
* @return Returns the content.
*/
public List getContent() {
return content;
}
/**
* @return Returns the page.
*/
public Page getPage() {
return page;
}
/**
* @param content
* The content to set.
*/
public void setContent(List content) {
this.content = content;
}
/**
* @param page
* The page to set.
*/
public void setPage(Page page) {
this.page = page;
}
}
然后在数据访问层写两个方法:
ProductDAO:
public List getProductByPage(Page page);
public int getProductCount(); //返回数据的总数
ProductDAO的接口实现ProductDAOImpl:
//为了在spring里统一编程风格:我用的回调的方法
public List getProductByPage(final Page page) {
return this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query=session.createQuery("from Productinfo");
query.setFirstResult(page.getBeginIndex()); //hibernate分页的精髓 呵呵
query.setMaxResults(page.getEveryPage());
return query.list();
}
});
}
public int getProductCount() {
List list=this.getHibernateTemplate().find("select count(*) from Productinfo");
return ((Integer)list.iterator().next()).intValue();
}
然后是业务层:
IProduct:
public Result listProduct(Page page);
然后IProduct接口的实现:IProductImpl:
private ProductDAO productDAO;
public void setProductDAO(ProductDAO productDAO){
this.productDAO=productDAO;
}
public Result listProduct(Page page) {
int totalRecords = this.productDAO.getProductCount();
page = PageUtil.createPage(page, totalRecords);
List products = this.productDAO.getProductByPage(page);
return new Result(page, products);
}
然后再代理层:
ProductProxy:
IProduct pro=(IProduct)AppContext.getInstance().getappcontext().getBean("productServicewithTran");
public Result productlist(Page page){
try{
return pro.listProduct(page);
}catch(DataAccessException ex){
ex.printStackTrace();
return null;
}
}
呵呵 终于到productAction啦
显示前方法的代码
Page page = new Page(); //实例化一个page对象
page.setEveryPage(10); //设置每页显示的条数
page.setCurrentPage(1); //为第一页
Result result = pdp.productlist(page);
request.setAttribute("page", pageinfo);
request.setAttribute("productlist", list);
return mapping.findForward("showProduct");
接着就是jsp页面了
<logic:iterate id="product" name="productlist">
//中间迭代所要显示的数据
</logic:iterate>
<tr>
<td width="80" height="30"> </td>
<logic:equal value="true" name="page" property="hasPrePage">
<td width="150" height="30"><div align="right"><a href="../product.do?method=showProductByTag&index=first&msg=${msg }">首页</a></div></td>
<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=prew&pageno=${page.currentPage -1}&msg=${msg }">上一页</a></div></td>
</logic:equal>
<logic:notEqual value="true" name="page" property="hasPrePage">
<td width="150" height="30"><div align="right">首页</div></td>
<td width="80" height="30"><div align="center">上一页</div></td>
</logic:notEqual>
<logic:equal value="true" name="page" property="hasNextPage">
<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=next&pageno=${page.currentPage +1 }&msg=${msg }">下一页</a></div></td>
<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=end&pageno=${page.totalPage }&msg=${msg }">尾页</a></div></td>
</logic:equal>
<logic:notEqual value="true" name="page" property="hasNextPage">
<td width="80" height="30"><div align="center">下一页</div></td>
<td width="80" height="30"><div align="center">尾页</div></td>
</logic:notEqual>
<td height="30" colspan="3"><div align="center">页次${page.currentPage }/${page.totalPage } 共${page.totalCount }条记录</div> <div align="center"></div></td>
</tr>
可以显示相应的页面信息
然后productAction里面的showProductByTag代码如下:
Page page = new Page();
page.setEveryPage(10);
String pagemark = request.getParameter("goto");
if (pagemark == null) {
String state = request.getParameter("index");
String pageno = request.getParameter("pageno");
System.out.println("pageno=" + pageno);
if ("first".equals(state)) {
page.setCurrentPage(1);
Result result = pdp.productlist(page);
request.setAttribute("page", result.getPage());
request.setAttribute("productlist", result.getContent());
} else if ("prew".equals(state)) {
page.setCurrentPage(Integer.parseInt(pageno));
Result result = pdp.productlist(page);
request.setAttribute("page", result.getPage());
request.setAttribute("productlist", result.getContent());
} else if ("next".equals(state)) {
page.setCurrentPage(Integer.parseInt(pageno));
Result result = pdp.productlist(page);
request.setAttribute("page", result.getPage());
request.setAttribute("productlist", result.getContent());
} else if ("end".equals(state)) {
page.setCurrentPage(Integer.parseInt(pageno));
Result result = pdp.productlist(page);
request.setAttribute("page", result.getPage());
request.setAttribute("productlist", result.getContent());
}
} else {
page.setCurrentPage(Integer.parseInt(pagemark));
Result result = pdp.productlist(page);
request.setAttribute("page", result.getPage());
request.setAttribute("productlist", result.getContent());
}
return mapping.findForward("showProduct");
-------------------------------------------------------------------
SSH的分页网上有不少的例子,有利用session的,有利用分页组件的。我几个师兄原来搞的SSH项目也有一个成熟的分页插件。
具体业务实现类中的分页方法:
Java代码
public List get*****(int pageNO){
DetachedCriteria dc = DetachedCriteria.forClass(****.class);
List list=*****Dao.getList(dc,pageNO,15);
int a = 0;
if (list.equals(null)){a=1;}
return list;
}
public PageBean getBean(int pageNO)
{
***** jg;
DetachedCriteria dc = DetachedCriteria.forClass(*****.class);
PageBean pb=collegeDao.getPageBean(dc,pageNO,15);
return pb;
}
然后是一个PageBean的工具类,负责创建分页属性和基本逻辑。
然后是页面的bean获取输出信息及分页属性。
我觉得单纯的拷贝,自己用的不是很顺手。于是自己也搞了一个,个人认为可以清晰的分层,实现这个分页。分层还是传统的SSH七层结构。
SSH结构思想参考我的另一篇随笔SSH思想之我见!
下面是分页思想,一个初始化方法和一个分页实现方法:
分页的util类:
package com.sy.util;
import java.util.List;
@SuppressWarnings("unchecked")
public class Page {
private int pageSize;
private int totalPage;
private int rowCount;
private int currentPage;
private int prePage;
private int nextPage;
private boolean hasNextPage;
private boolean hasPreviousPage;
private List list;
public Page() {
this.pageSize=10;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPrePage() {
return prePage;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
分页的数据库操作和逻辑判断我把他单独用一个PageDaoImpl来实现:
/*
* 施杨的分页daoimpl类
* */
package com.sy.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.sy.dao.BaseDao;
import com.sy.util.Page;
@SuppressWarnings("unchecked")
public class PageDaoImpl extends HibernateDaoSupport {
private String hql;
public Page page;
public int start;
public BaseDao dao;
public void setDao(BaseDao dao) {
this.dao = dao;
}
public void init(int start,String tableName){ // ͨ��init����ʵ����ij�ʼ��
page = new Page();
this.hql = "from "+tableName;
this.start = start;
setRowCount();
setTotalPage();
setCurrentPage();
setPrePage();
setNextPage();
setPreOrNextBoolean();
}
public int getRowCount(){
List list = dao.find(hql);
if(list.isEmpty()){
return 0;
}
return list.size();
}
public Page getPage(){
List list = (List)getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(getStartIndex());
query.setMaxResults(page.getPageSize());
return query.list();
}
});
page.setList(list);
return page;
}
public void setPreOrNextBoolean() {
if (page.getCurrentPage() <= 1) {
page.setHasPreviousPage(false);
} else {
page.setHasPreviousPage(true);
}
if (page.getCurrentPage() >= page.getTotalPage()) {
page.setHasNextPage(false);
} else {
page.setHasNextPage(true);
}
}
public void setCurrentPage() {
if (start < 1) {
page.setCurrentPage(1);
}
if (start > page.getTotalPage()) {
page.setCurrentPage(page.getTotalPage());
}
page.setCurrentPage(start);
}
public void setPrePage() {
page.setPrePage(page.getCurrentPage() - 1);
}
public void setNextPage() {
page.setNextPage(page.getCurrentPage() + 1);
}
public void setTotalPage() {
int rowCount = getRowCount();
int pageSize = page.getPageSize();
if (rowCount > pageSize) {
if (rowCount % pageSize == 0) {
page.setTotalPage(rowCount / pageSize);
} else {
page.setTotalPage(1 + (rowCount / pageSize));
}
} else {
page.setTotalPage(1);
}
}
public void setRowCount() {
page.setRowCount(getRowCount());
}
public int getStartIndex() {
int startIndex = 0;
if (start < 0) {
startIndex = 0;
} else {
if (start > page.getTotalPage()) {
startIndex = page.getPageSize() * (page.getTotalPage() - 1);
} else {
startIndex = page.getPageSize() * (start - 1);
}
}
return startIndex;
}
}
然后是业务层接口,举例AdminService.java
//管理员service层接口
package com.sy.service;
import java.util.List;
import com.sy.util.Page;
import com.sy.vo.Admin;
public interface AdminService extends BaseService{
//分页初始化
public void init(int pno);
//分页实现
public Page getPage();
}
实现类AdminServiceImpl.java
//管理员service层实现类
package com.sy.service.impl;
import java.util.List;
import com.sy.dao.impl.AdminDaoImpl;
import com.sy.dao.impl.PageDaoImpl;
import com.sy.service.AdminService;
import com.sy.util.Page;
import com.sy.vo.Admin;
@SuppressWarnings("unchecked")
public class AdminServiceImpl extends BaseServiceImpl implements AdminService {
.
public Page getPage() {
return Pdao.getPage();
}
public void init(int pno) {
String tableName="Admin";
Pdao.init(pno,tableName);
}
}
struts的action层AdminAction.java
//管理员action实现类
package com.sy.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import com.sy.form.AdminForm;
import com.sy.form.LoginForm;
import com.sy.service.AdminService;
import com.sy.util.MD5;
import com.sy.util.Page;
import com.sy.vo.Admin;
@SuppressWarnings("unchecked")
public class AdminAction extends BaseAction {
.
//查看管理员
public ActionForward listAdmins(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AdminService as=(AdminService)super.getBean("AdminService");
int pno = (new Integer(request.getParameter("pno"))).intValue();
as.init(pno);
Page myPage = as.getPage();
List myList = as.getPage().getList();
request.setAttribute("myPage", myPage);
request.setAttribute("AdminList", myList);
return mapping.findForward("show");
}
}
结构清晰,也很实用。
页面lookAdmin.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>施杨SSH新闻发布</title>
</head>
<body>
<center>
<h3>
查看管理员
</h3>
<br>
<a href="./admin/addAdmin.jsp">添加管理员</a>
<br>
<a href="./NewsPage2.do?pno=1">查看新闻</a>
<br>
<table border="2" width="600">
<tr>
<td>
用户名
</td>
<td>
密码
</td>
<td>
操作
</td>
</tr>
<c:forEach items="${requestScope['AdminList']}" var="admin">
<tr align="center">
<td width="20%" height="10"><c:out value="${admin.aname}" /></td>
<td width="20%" height="10"><c:out value="${admin.apassword}" /></td>
<td>
<a href="./editAdmin.do?aid=<c:out value="${admin.aid}"/>">修改</a>
<a href="./deleteAdmin.do?aid=<c:out value="${admin.aid}"/>" onclick="javascript:return confirm('您确定删除吗?')">删除</a>
</td>
</tr>
</c:forEach>
</table>
<table align="center" width="500">
<tr>
<td align="center" colspan="10">
<logic:present name="myPage">
<html:link page="/AdminPage.do?pno=1">首页</html:link>
<logic:equal name="myPage" property="hasPreviousPage" value="false">上一页</logic:equal>
<logic:equal name="myPage" property="hasPreviousPage" value="true">
<a href="./AdminPage.do?pno=<bean:write name="myPage" property="prePage"/>">上一页</a>
</logic:equal>
每页<bean:write name="myPage" property="pageSize"/>条记录
共<bean:write name="myPage" property="rowCount"/>条记录
当前第(<bean:write name="myPage" property="currentPage"/>/<bean:write name="myPage" property="totalPage"/>)页
<logic:equal name="myPage" property="hasNextPage" value="false">下一页</logic:equal>
<logic:equal name="myPage" property="hasNextPage" value="true">
<a href="./AdminPage.do?pno=<bean:write name="myPage" property="nextPage"/>">下一页</a>
</logic:equal>
<a href="./AdminPage.do?pno=<bean:write name='myPage' property='totalPage'/>">末页</a>
</logic:present>
</td>
</tr>
</table>
</center>
</body>
</html>
Java代码
package com.leatherstore.other;
public class Page {
/** 是否有上一页 */
private boolean hasPrePage;
/** 是否有下一页 */
private boolean hasNextPage;
/** 每页的数量 */
private int everyPage;
/** 总页数 */
private int totalPage;
/** 当前页*/
private int currentPage;
/** 起始点 */
private int beginIndex;
/** 总记录数*/
private int totalCount;
/**
* @return totalCount
*/
public int getTotalCount() {
return totalCount;
}
/**
* @param totalCount 要设置的 totalCount
*/
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
/** The default constructor */
public Page(){
}
/** construct the page by everyPage
* @param everyPage
* */
public Page(int everyPage){
this.everyPage = everyPage;
}
/** The whole constructor */
public Page(boolean hasPrePage, boolean hasNextPage,
int everyPage, int totalPage,
int currentPage, int beginIndex,int totalCount) {
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
this.everyPage = everyPage;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.totalCount = totalCount;
}
/**
* @return
* Returns the beginIndex.
*/
public int getBeginIndex() {
return beginIndex;
}
/**
* @param beginIndex
* The beginIndex to set.
*/
public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
}
/**
* @return
* Returns the currentPage.
*/
public int getCurrentPage() {
return currentPage;
}
/**
* @param currentPage
* The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return
* Returns the everyPage.
*/
public int getEveryPage() {
return everyPage;
}
/**
* @param everyPage
* The everyPage to set.
*/
public void setEveryPage(int everyPage) {
this.everyPage = everyPage;
}
/**
* @return
* Returns the hasNextPage.
*/
public boolean getHasNextPage() {
return hasNextPage;
}
/**
* @param hasNextPage
* The hasNextPage to set.
*/
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
/**
* @return
* Returns the hasPrePage.
*/
public boolean getHasPrePage() {
return hasPrePage;
}
/**
* @param hasPrePage
* The hasPrePage to set.
*/
public void setHasPrePage(boolean hasPrePage) {
this.hasPrePage = hasPrePage;
}
/**
* @return Returns the totalPage.
*
*/
public int getTotalPage() {
return totalPage;
}
/**
* @param totalPage
* The totalPage to set.
*/
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
}
然后构建一个page的工厂PageUtil:
package com.leatherstore.other;
public class PageUtil {
/**
* Use the origin page to create a new page
*
* @param page
* @param totalRecords
* @return
*/
public static Page createPage(Page page, int totalRecords) {
return createPage(page.getEveryPage(), page.getCurrentPage(),
totalRecords);
}
/**
* the basic page utils not including exception handler
*
* @param everyPage
* @param currentPage
* @param totalRecords
* @return page
*/
public static Page createPage(int everyPage, int currentPage,
int totalRecords) {
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int beginIndex = getBeginIndex(everyPage, currentPage);
int totalPage = getTotalPage(everyPage, totalRecords);
boolean hasNextPage = hasNextPage(currentPage, totalPage);
boolean hasPrePage = hasPrePage(currentPage);
return new Page(hasPrePage, hasNextPage, everyPage, totalPage,
currentPage, beginIndex, totalRecords);
}
private static int getEveryPage(int everyPage) {
return everyPage == 0 ? 10 : everyPage;
}
private static int getCurrentPage(int currentPage) {
return currentPage == 0 ? 1 : currentPage;
}
private static int getBeginIndex(int everyPage, int currentPage) {
return (currentPage - 1) * everyPage;
}
private static int getTotalPage(int everyPage, int totalRecords) {
int totalPage = 0;
if (totalRecords % everyPage == 0)
totalPage = totalRecords / everyPage;
else
totalPage = totalRecords / everyPage + 1;
return totalPage;
}
private static boolean hasPrePage(int currentPage) {
return currentPage == 1 ? false : true;
}
private static boolean hasNextPage(int currentPage, int totalPage) {
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}
然后建一个专用的bean:
package com.leatherstore.hibernate.domain;
import java.util.List;
import com.leatherstore.other.Page;
public class Result {
private Page page; //分页信息
private List content; //每页显示的集合
/**
* The default constructor
*/
public Result() {
super();
}
/**
* The constructor using fields
*
* @param page
* @param content
*/
public Result(Page page, List content) {
this.page = page;
this.content = content;
}
/**
* @return Returns the content.
*/
public List getContent() {
return content;
}
/**
* @return Returns the page.
*/
public Page getPage() {
return page;
}
/**
* @param content
* The content to set.
*/
public void setContent(List content) {
this.content = content;
}
/**
* @param page
* The page to set.
*/
public void setPage(Page page) {
this.page = page;
}
}
然后在数据访问层写两个方法:
ProductDAO:
public List getProductByPage(Page page);
public int getProductCount(); //返回数据的总数
ProductDAO的接口实现ProductDAOImpl:
//为了在spring里统一编程风格:我用的回调的方法
public List getProductByPage(final Page page) {
return this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query=session.createQuery("from Productinfo");
query.setFirstResult(page.getBeginIndex()); //hibernate分页的精髓 呵呵
query.setMaxResults(page.getEveryPage());
return query.list();
}
});
}
public int getProductCount() {
List list=this.getHibernateTemplate().find("select count(*) from Productinfo");
return ((Integer)list.iterator().next()).intValue();
}
然后是业务层:
IProduct:
public Result listProduct(Page page);
然后IProduct接口的实现:IProductImpl:
private ProductDAO productDAO;
public void setProductDAO(ProductDAO productDAO){
this.productDAO=productDAO;
}
public Result listProduct(Page page) {
int totalRecords = this.productDAO.getProductCount();
page = PageUtil.createPage(page, totalRecords);
List products = this.productDAO.getProductByPage(page);
return new Result(page, products);
}
然后再代理层:
ProductProxy:
IProduct pro=(IProduct)AppContext.getInstance().getappcontext().getBean("productServicewithTran");
public Result productlist(Page page){
try{
return pro.listProduct(page);
}catch(DataAccessException ex){
ex.printStackTrace();
return null;
}
}
呵呵 终于到productAction啦
显示前方法的代码
Page page = new Page(); //实例化一个page对象
page.setEveryPage(10); //设置每页显示的条数
page.setCurrentPage(1); //为第一页
Result result = pdp.productlist(page);
request.setAttribute("page", pageinfo);
request.setAttribute("productlist", list);
return mapping.findForward("showProduct");
接着就是jsp页面了
<logic:iterate id="product" name="productlist">
//中间迭代所要显示的数据
</logic:iterate>
<tr>
<td width="80" height="30"> </td>
<logic:equal value="true" name="page" property="hasPrePage">
<td width="150" height="30"><div align="right"><a href="../product.do?method=showProductByTag&index=first&msg=${msg }">首页</a></div></td>
<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=prew&pageno=${page.currentPage -1}&msg=${msg }">上一页</a></div></td>
</logic:equal>
<logic:notEqual value="true" name="page" property="hasPrePage">
<td width="150" height="30"><div align="right">首页</div></td>
<td width="80" height="30"><div align="center">上一页</div></td>
</logic:notEqual>
<logic:equal value="true" name="page" property="hasNextPage">
<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=next&pageno=${page.currentPage +1 }&msg=${msg }">下一页</a></div></td>
<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=end&pageno=${page.totalPage }&msg=${msg }">尾页</a></div></td>
</logic:equal>
<logic:notEqual value="true" name="page" property="hasNextPage">
<td width="80" height="30"><div align="center">下一页</div></td>
<td width="80" height="30"><div align="center">尾页</div></td>
</logic:notEqual>
<td height="30" colspan="3"><div align="center">页次${page.currentPage }/${page.totalPage } 共${page.totalCount }条记录</div> <div align="center"></div></td>
</tr>
可以显示相应的页面信息
然后productAction里面的showProductByTag代码如下:
Page page = new Page();
page.setEveryPage(10);
String pagemark = request.getParameter("goto");
if (pagemark == null) {
String state = request.getParameter("index");
String pageno = request.getParameter("pageno");
System.out.println("pageno=" + pageno);
if ("first".equals(state)) {
page.setCurrentPage(1);
Result result = pdp.productlist(page);
request.setAttribute("page", result.getPage());
request.setAttribute("productlist", result.getContent());
} else if ("prew".equals(state)) {
page.setCurrentPage(Integer.parseInt(pageno));
Result result = pdp.productlist(page);
request.setAttribute("page", result.getPage());
request.setAttribute("productlist", result.getContent());
} else if ("next".equals(state)) {
page.setCurrentPage(Integer.parseInt(pageno));
Result result = pdp.productlist(page);
request.setAttribute("page", result.getPage());
request.setAttribute("productlist", result.getContent());
} else if ("end".equals(state)) {
page.setCurrentPage(Integer.parseInt(pageno));
Result result = pdp.productlist(page);
request.setAttribute("page", result.getPage());
request.setAttribute("productlist", result.getContent());
}
} else {
page.setCurrentPage(Integer.parseInt(pagemark));
Result result = pdp.productlist(page);
request.setAttribute("page", result.getPage());
request.setAttribute("productlist", result.getContent());
}
return mapping.findForward("showProduct");
-------------------------------------------------------------------
SSH的分页网上有不少的例子,有利用session的,有利用分页组件的。我几个师兄原来搞的SSH项目也有一个成熟的分页插件。
具体业务实现类中的分页方法:
Java代码
public List get*****(int pageNO){
DetachedCriteria dc = DetachedCriteria.forClass(****.class);
List list=*****Dao.getList(dc,pageNO,15);
int a = 0;
if (list.equals(null)){a=1;}
return list;
}
public PageBean getBean(int pageNO)
{
***** jg;
DetachedCriteria dc = DetachedCriteria.forClass(*****.class);
PageBean pb=collegeDao.getPageBean(dc,pageNO,15);
return pb;
}
然后是一个PageBean的工具类,负责创建分页属性和基本逻辑。
然后是页面的bean获取输出信息及分页属性。
我觉得单纯的拷贝,自己用的不是很顺手。于是自己也搞了一个,个人认为可以清晰的分层,实现这个分页。分层还是传统的SSH七层结构。
SSH结构思想参考我的另一篇随笔SSH思想之我见!
下面是分页思想,一个初始化方法和一个分页实现方法:
分页的util类:
package com.sy.util;
import java.util.List;
@SuppressWarnings("unchecked")
public class Page {
private int pageSize;
private int totalPage;
private int rowCount;
private int currentPage;
private int prePage;
private int nextPage;
private boolean hasNextPage;
private boolean hasPreviousPage;
private List list;
public Page() {
this.pageSize=10;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPrePage() {
return prePage;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
分页的数据库操作和逻辑判断我把他单独用一个PageDaoImpl来实现:
/*
* 施杨的分页daoimpl类
* */
package com.sy.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.sy.dao.BaseDao;
import com.sy.util.Page;
@SuppressWarnings("unchecked")
public class PageDaoImpl extends HibernateDaoSupport {
private String hql;
public Page page;
public int start;
public BaseDao dao;
public void setDao(BaseDao dao) {
this.dao = dao;
}
public void init(int start,String tableName){ // ͨ��init����ʵ����ij�ʼ��
page = new Page();
this.hql = "from "+tableName;
this.start = start;
setRowCount();
setTotalPage();
setCurrentPage();
setPrePage();
setNextPage();
setPreOrNextBoolean();
}
public int getRowCount(){
List list = dao.find(hql);
if(list.isEmpty()){
return 0;
}
return list.size();
}
public Page getPage(){
List list = (List)getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(getStartIndex());
query.setMaxResults(page.getPageSize());
return query.list();
}
});
page.setList(list);
return page;
}
public void setPreOrNextBoolean() {
if (page.getCurrentPage() <= 1) {
page.setHasPreviousPage(false);
} else {
page.setHasPreviousPage(true);
}
if (page.getCurrentPage() >= page.getTotalPage()) {
page.setHasNextPage(false);
} else {
page.setHasNextPage(true);
}
}
public void setCurrentPage() {
if (start < 1) {
page.setCurrentPage(1);
}
if (start > page.getTotalPage()) {
page.setCurrentPage(page.getTotalPage());
}
page.setCurrentPage(start);
}
public void setPrePage() {
page.setPrePage(page.getCurrentPage() - 1);
}
public void setNextPage() {
page.setNextPage(page.getCurrentPage() + 1);
}
public void setTotalPage() {
int rowCount = getRowCount();
int pageSize = page.getPageSize();
if (rowCount > pageSize) {
if (rowCount % pageSize == 0) {
page.setTotalPage(rowCount / pageSize);
} else {
page.setTotalPage(1 + (rowCount / pageSize));
}
} else {
page.setTotalPage(1);
}
}
public void setRowCount() {
page.setRowCount(getRowCount());
}
public int getStartIndex() {
int startIndex = 0;
if (start < 0) {
startIndex = 0;
} else {
if (start > page.getTotalPage()) {
startIndex = page.getPageSize() * (page.getTotalPage() - 1);
} else {
startIndex = page.getPageSize() * (start - 1);
}
}
return startIndex;
}
}
然后是业务层接口,举例AdminService.java
//管理员service层接口
package com.sy.service;
import java.util.List;
import com.sy.util.Page;
import com.sy.vo.Admin;
public interface AdminService extends BaseService{
//分页初始化
public void init(int pno);
//分页实现
public Page getPage();
}
实现类AdminServiceImpl.java
//管理员service层实现类
package com.sy.service.impl;
import java.util.List;
import com.sy.dao.impl.AdminDaoImpl;
import com.sy.dao.impl.PageDaoImpl;
import com.sy.service.AdminService;
import com.sy.util.Page;
import com.sy.vo.Admin;
@SuppressWarnings("unchecked")
public class AdminServiceImpl extends BaseServiceImpl implements AdminService {
.
public Page getPage() {
return Pdao.getPage();
}
public void init(int pno) {
String tableName="Admin";
Pdao.init(pno,tableName);
}
}
struts的action层AdminAction.java
//管理员action实现类
package com.sy.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import com.sy.form.AdminForm;
import com.sy.form.LoginForm;
import com.sy.service.AdminService;
import com.sy.util.MD5;
import com.sy.util.Page;
import com.sy.vo.Admin;
@SuppressWarnings("unchecked")
public class AdminAction extends BaseAction {
.
//查看管理员
public ActionForward listAdmins(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AdminService as=(AdminService)super.getBean("AdminService");
int pno = (new Integer(request.getParameter("pno"))).intValue();
as.init(pno);
Page myPage = as.getPage();
List myList = as.getPage().getList();
request.setAttribute("myPage", myPage);
request.setAttribute("AdminList", myList);
return mapping.findForward("show");
}
}
结构清晰,也很实用。
页面lookAdmin.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>施杨SSH新闻发布</title>
</head>
<body>
<center>
<h3>
查看管理员
</h3>
<br>
<a href="./admin/addAdmin.jsp">添加管理员</a>
<br>
<a href="./NewsPage2.do?pno=1">查看新闻</a>
<br>
<table border="2" width="600">
<tr>
<td>
用户名
</td>
<td>
密码
</td>
<td>
操作
</td>
</tr>
<c:forEach items="${requestScope['AdminList']}" var="admin">
<tr align="center">
<td width="20%" height="10"><c:out value="${admin.aname}" /></td>
<td width="20%" height="10"><c:out value="${admin.apassword}" /></td>
<td>
<a href="./editAdmin.do?aid=<c:out value="${admin.aid}"/>">修改</a>
<a href="./deleteAdmin.do?aid=<c:out value="${admin.aid}"/>" onclick="javascript:return confirm('您确定删除吗?')">删除</a>
</td>
</tr>
</c:forEach>
</table>
<table align="center" width="500">
<tr>
<td align="center" colspan="10">
<logic:present name="myPage">
<html:link page="/AdminPage.do?pno=1">首页</html:link>
<logic:equal name="myPage" property="hasPreviousPage" value="false">上一页</logic:equal>
<logic:equal name="myPage" property="hasPreviousPage" value="true">
<a href="./AdminPage.do?pno=<bean:write name="myPage" property="prePage"/>">上一页</a>
</logic:equal>
每页<bean:write name="myPage" property="pageSize"/>条记录
共<bean:write name="myPage" property="rowCount"/>条记录
当前第(<bean:write name="myPage" property="currentPage"/>/<bean:write name="myPage" property="totalPage"/>)页
<logic:equal name="myPage" property="hasNextPage" value="false">下一页</logic:equal>
<logic:equal name="myPage" property="hasNextPage" value="true">
<a href="./AdminPage.do?pno=<bean:write name="myPage" property="nextPage"/>">下一页</a>
</logic:equal>
<a href="./AdminPage.do?pno=<bean:write name='myPage' property='totalPage'/>">末页</a>
</logic:present>
</td>
</tr>
</table>
</center>
</body>
</html>
发表评论
-
Apache+Tomcat集群配置
2013-06-24 09:49 1249Tomcat 本文Apache+Tomcat集群 ... -
FusionCharts参数的详细说明和功能特性
2013-03-13 12:22 1172功能特性animation ... -
Tomcat中关于堆内存与永久区内存的设置
2011-12-12 13:49 27121.在tomcat (免安装版)文件夹下的bin文件夹下找 ... -
Tomcat内存溢出的原因 三个原因
2011-12-12 13:38 1707Tomcat内存溢出的原因 在生产环境中tomcat内存 ... -
xml中大于、小于等特殊字符处理
2011-10-11 09:57 3039可以用下面这种写法转义 < ... -
FreeMarker在struts2,JAVA,web中的应用实例
2011-09-15 10:19 2008FreeMarker概述 * FreeMarker是一 ... -
J2EE里面使用freemark技术
2011-09-15 09:46 1588模板技术在现代的软件 ... -
java乱码问题
2011-09-01 09:44 3304Java中乱码问题很常见,原因也多种多样,这里做一个总结,不求 ... -
ognl表达式入门应用
2011-08-30 17:12 1597package foo.test; public cla ... -
XFire建立webservice服务和客户端
2011-08-15 15:50 1828XFire 是与Axis 2并列的新一代Web Service ... -
字符编码过滤器的一般写法
2011-08-04 16:24 1198今天研究别人的代码的时候突然发现spring的org.spri ... -
struts2的日期date格式化(3种)
2011-08-02 09:27 2691在直接<s:textfield中也要日期格式化,平时使用 ... -
j2ee获取客户端参数集
2011-07-07 18:28 1655Java代码 package javacommon.u ... -
HttpURLConnection发送文件
2011-07-05 10:16 2470Java代码 URL url = new URL( ... -
tomcat部署get请求乱码问题解决方法
2011-06-13 18:06 1922默认情况下,Tomcat对请求采用的默认编码是ISO-8859 ... -
Tomcat:IOException while loading persisted sessions: java.io.EOFException
2011-06-13 15:56 1642一直用tomcat一段时间都一切正常,最近一次启动tomcat ... -
jsp打印问题
2011-04-07 15:58 3136jsp打印问题 回复于 2006-01-06 22:43:45 ... -
分页标签:pager-taglib使用指南
2011-01-26 18:07 1658一简介, Pager-taglib,支持多种风格的分页显示。 ... -
jstl 遍历集合
2011-01-26 16:02 5115<%@ page language="java ... -
使用 HibernateTemplate 实现分页查询
2011-01-24 23:28 20519/*** 使用hql 语句进行操作 * @param hql ...
相关推荐
总的来说,SSH分页是一个涉及前端交互、后端处理和数据库查询的完整过程,需要协调多个组件协同工作。这个例子是一个很好的学习资源,可以帮助开发者掌握在Java Web应用中实现高效分页的关键技术。
这个例子中的`some_command`需要替换为你实际要执行的远程命令。 总的来说,SSH分页和局部刷新是提高SSH使用效率的重要技巧,它们可以帮助我们更好地管理和查看远程服务器的大量输出信息。通过熟练掌握`less`命令的...
在这个“ssh分页例子”中,我们将深入探讨SSH分页的工作原理以及如何在实际操作中使用它。 首先,SSH分页通常通过内置的分页器来实现,如`more`和`less`命令。当命令的输出过多,无法一次性完全显示在终端窗口时,...
在这个“ssh增删改查+分页”的主题中,我们将深入探讨如何使用SSH2来实现数据库操作的基本功能,并结合分页技术进行数据展示。 首先,让我们关注SSH2中的"增"操作,即添加数据到数据库。这通常涉及编写一个Servlet...
综上所述,SSH2实现的分页查询涉及了Struts2的Action、Service、页面展示,以及配置文件等多个方面。通过合理地组织代码和配置,可以轻松实现高效的分页功能。在实际项目中,你还可以根据需求对查询条件、排序方式...
SSH(Secure Shell)是一种网络协议,用于在不安全的网络上提供安全的远程登录和其他服务。在本示例中,我们将深入探讨SSH的一对一单向...希望这个例子对你有所帮助,也欢迎你分享更多关于SSH使用和管理的经验与技巧。
总结,SSH框架下的分页实现涉及了多个层次的协作:JSP页面处理用户界面和数据展示,JavaScript处理用户交互,Action层协调Model和View,Model层封装业务逻辑,DAO层处理数据库操作。了解这些知识点,可以帮助开发者...
在这个例子中,我们关注的是使用SSH2进行Java开发的相关内容,特别是通过jar包实现的简单数据库操作,包括增删改查、分页和排序。 SSH2在Java开发中的应用通常指的是Spring、Struts2和Hibernate这三个框架的组合,...
Service层负责协调DAO层和Controller层的工作,它通常包含对多个DAO层方法的调用。 ```java public class MemberService { private MemberDao memberDao; public void setMemberDao(MemberDao memberDao) { ...
在这个例子中,Bootstrap样式用于美化表格,使其在不同设备上都能有良好的视觉效果。 实现异步表格分页的基本步骤: 1. **前端准备**:创建HTML结构,使用Bootstrap的表格类,以及监听分页事件的JavaScript代码。...
在这个例子中,还包含了`mysql数据脚本`,这意味着项目包含了初始化数据库的脚本,通常用于创建表结构、填充测试数据等。这有助于快速设置开发环境。 **分页显示** 是Web应用中常见的功能,用于处理大量数据的展示...
在这个例子中,`Page`类被用来存储分页的相关信息,例如当前页数、每页记录数、总页数等。 ```java public class Page { private List list; // 要返回的某一页的记录列表 private int allRow; // 总记录数 ...
最近在自学Extjs,做了一个小例子,后台使用SSH,前台是ExtJs,其中包含了很多内容,例如grid,TreeGrid,comboxTree,分页等内容,数据库采用的是mysql,文件中包含了数据库文件,导入mysql数据库即可,希望可以对初学者有一点...
SSH三大框架指的是Spring、Struts和Hibernate,它们是Java企业级开发中常用的技术栈,用于构建高效、可...随着你对每个框架理解的深入,可以尝试添加更多功能,如分页、缓存、国际化等,进一步提高系统性能和用户体验。
前段时间时间当我在学习SSH框架技术的时候,在网上很难找到关于SSH的完整例子,比如最基本的CRUD操作和用的比较多的查询分页技术。为了让SSH初学者更清楚全面的学习,不再像我初学时候迷失方向。特花了2天时间做一个...
前段时间时间当我在学习SSH框架技术的时候,在网上很难找到关于SSH的完整例子,比如最基本的CRUD操作和用的比较多的查询分页技术。为了让SSH初学者更清楚全面的学习,不再像我初学时候迷失方向。特花了2天时间做一个...
功能:使用dwr完成在客户端无刷新的分页排序等 <br>工具:MyEclipse 6.0,数据库SQLSERVER 2000 <br>数据在SQL文件夹下,Jobs是pubs数据库自带的 <br>主要的三个页面,emp是详细的,另两个是简单的复制修改,...
在实际项目中,SSH框架的数据库操作封装还会涉及到错误处理、性能优化(如批处理、缓存机制)、分页查询、连接池配置等多个方面。理解并熟练掌握这些内容,对于开发高效、稳定的Java Web应用至关重要。
例如,如果一个部门有多个员工,删除部门时可以设置级联删除所有关联的员工记录。 5. **分页显示**:在大量数据的场景下,为了提高用户体验,通常会采用分页显示数据。在这个系统中,用户可以查看一部分数据,然后...
分页是将大量数据分为多个较小的、易于管理的部分,每次只加载一部分数据到客户端。这通常通过两个主要部分实现:前端(用户界面)和后端(服务器)的交互。 在Struts框架中,前端分页的实现通常涉及JSP页面。JSP...