Javaweb查询客户&分页部分代码
Posted JokerQ-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Javaweb查询客户&分页部分代码相关的知识,希望对你有一定的参考价值。
pageBean工具类代码(分页工具)
package com.home.domain; import java.util.List; /** * 分页的JavaBean * @author Administrator */ public class PageBean<T> { // 当前页 private int pageCode; // 总页数 // private int totalPage; // 总记录数 private int totalCount; // 每页显示的记录条数 private int pageSize; // 每页显示的数据 private List<T> beanList; public int getPageCode() { return pageCode; } public void setPageCode(int pageCode) { this.pageCode = pageCode; } /** * 调用getTotalPage() 获取到总页数 * JavaBean的属性规定:totalPage是JavaBean是属性 ${pageBean.totalPage} * @return */ public int getTotalPage() { // 计算 int totalPage = totalCount / pageSize; // 说明整除 if(totalCount % pageSize == 0){ return totalPage; }else{ return totalPage + 1; } } /*public void setTotalPage(int totalPage) { this.totalPage = totalPage; }*/ public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public List<T> getBeanList() { return beanList; } public void setBeanList(List<T> beanList) { this.beanList = beanList; } }
WEB层
public String findByPage(){ //调用Service DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class); //查询 PageBean<Customer> page = customerService.findByPage(pageCode,pageSize,criteria); //压栈 ValueStack vs = ActionContext.getContext().getValueStack(); //栈顶 map集合 vs.set("page", page); return "page"; }
service层
//分页查询 @Override public PageBean<Customer> findByPage(Integer pageCode, Integer pageSize, DetachedCriteria criteria) { return CustomerDao.findByPage(pageCode,pageSize,criteria); }
Dao层
//分页查询 @Override public PageBean<Customer> findByPage(Integer pageCode, Integer pageSize, DetachedCriteria criteria) { PageBean<Customer> page = new PageBean<>(); page.setPageCode(pageCode); page.setPageSize(pageSize); //先查询总记录数 criteria.setProjection(Projections.rowCount()); List<Number> list = (List<Number>) this.getHibernateTemplate().findByCriteria(criteria); if (list!=null&&list.size()>0) { int totalCount = list.get(0).intValue(); page.setTotalCount(totalCount); } //强调 把select count(*)先清空 ,变成select *.... criteria.setProjection(null); //分页查询数据,每页显示的数据 //Hib..提供的分页查询 List<Customer> beanList = (List<Customer>)this.getHibernateTemplate().findByCriteria(criteria, (pageCode-1)*pageSize, pageSize); page.setBeanList(beanList); return page; }
前台设置客户数据
<c:forEach items="${page.beanList }" var="customer"> <TR style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none"> <TD>${customer.cust_name }</TD> <TD>${customer.level.dict_item_name }</TD> <TD>${customer.source.dict_item_name }</TD> <TD>${customer.cust_linkman }</TD> <TD>${customer.cust_phone }</TD> <TD>${customer.cust_mobile }</TD> <TD> <a href="${pageContext.request.contextPath }/customerServlet?method=edit&custId=${customer.cust_id}">修改</a> <a href="${pageContext.request.contextPath }/customerServlet?method=delete&custId=${customer.cust_id}">删除</a> </TD> </TR>
</c:forEach>
前台设置分页
<TR> <TD><SPAN id=pagelink> <DIV style="LINE-HEIGHT: 20px; HEIGHT: 20px; TEXT-ALIGN: right"> 共[<B>${page.totalCount}</B>]条记录,共[<B>${page.totalPage}</B>]页 ,每页显示 <select name="pageSize"> <option value="2" <c:if test="${page.pageSize==2 }">selected</c:if>>2</option> <option value="3" <c:if test="${page.pageSize==3 }">selected</c:if>>3</option> </select> 条 <c:if test="${page.pageCode>1 }"> [<A href="javascript:to_page(${page.pageCode-1})">前一页</A>] </c:if> <B>${page.pageCode}</B> <c:if test="${page.pageCode<page.totalPage }"> [<A href="javascript:to_page(${page.pageCode+1})">后一页</A>] </c:if> 到 <input type="text" size="3" id="page" name="pageCode" /> 页 <input type="button" value="Go" onclick="to_page()"/> </DIV> </SPAN></TD> </TR>
to_page()点击事件(Form表单提交到客户Action)
<SCRIPT language=javascript> //提交分页 function to_page(page){ if(page){ $("#page").val(page); } document.customerForm.submit(); } </SCRIPT>
以上是关于Javaweb查询客户&分页部分代码的主要内容,如果未能解决你的问题,请参考以下文章