1. 程式人生 > >Javaweb查詢客戶&分頁部分代碼

Javaweb查詢客戶&分頁部分代碼

test -a emp for value select lec null min

技術分享圖片

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查詢客戶&分頁部分代碼