基於SSH2分頁處理

在這個功能中我將分頁的功能都封裝到一個工具類PageTool類中,先看下代碼

action代碼:

package cn.watermellon.action.UI.admin;


import javax.annotation.Resource;


import org.springframework.stereotype.Controller;


import com.opensymphony.xwork2.ActionContext;

import cn.watermellon.service.CustomerService;
import cn.watermellon.tools.PageTool;


@Controller
public class CustomerManager {

@Resource CustomerService customerService;


private int next_page = 1;               //分頁的頁數


public int getNext_page() {
return next_page;
}
public void setNext_page(int next_page) {
this.next_page = next_page == 0 ? 1 : next_page;
}
public String allCustomer(){
ActionContext.getContext().put("page", customerService.allCustomer(next_page));
return "list";
}
}

業務層代碼:

package cn.watermellon.service.impl;


import javax.annotation.Resource;


import org.springframework.stereotype.Service;


import cn.watermellon.bean.Customer;
import cn.watermellon.dao.BaseDAO;
import cn.watermellon.service.CustomerService;
import cn.watermellon.tools.PageTool;
@Service
public class CustomerServiceBean implements CustomerService{
@Resource BaseDAO daoService;


@SuppressWarnings("rawtypes")
public PageTool allCustomer(int next_page) {
PageTool<Customer> page = new PageTool<Customer>(10);             //首先先實例化一個PageTool對象,並且賦值每頁顯示數10
page.setResults(daoService.findAll(Customer.class, "Customer"));    //接着設置結果集,調用findAll方法返回需要展現的所有數據的集合
page.setPage_current(next_page);                                                              //設置當前頁,該值可以由頁面傳值過來
page.final_results();                                                                                         //最後設置最終要顯示在當前頁的所有數據
return page;
}
}

最後是最重要的PageTool類代碼:

package cn.watermellon.tools;


import java.util.List;


public class PageTool <T>{


/** 開始索引 **/
private int star_index;
/** 結束索引 **/
private int end_index;
/** 總記錄數 **/
private int count;
/** 頁面顯示數 **/
private int page_display_count;
/** 結果集 **/
private List<T> results;
/** 當前頁面值 **/
private int page_current;
/** 總頁數 **/
private int page_count;
/** 顯示在頁面的結果集 **/
private List<T> page_results;

public PageTool(){}
public PageTool(int page_display_count) {
this.page_display_count = page_display_count;
}
public int getStar_index() {
return star_index;
}
public void setStar_index(int star_index) {
this.star_index = star_index;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public List<T> getResults() {
return results;
}
public void setResults(List<T> result) {                 //設置結果集的同時設置結果集的大小,以及總頁數
this.results = result;
setCount(result.size());
setPage_count(getCount()%getPage_display_count()==0? getCount()/getPage_display_count() : getCount()/getPage_display_count()+1);
}
public int getPage_display_count() {
return page_display_count;
}
public void setPage_display_count(int page_display_count) {
this.page_display_count = page_display_count;
}
public int getPage_current() {
return page_current;
}
public void setPage_current(int page_current) {                //設置當前頁的同時設置在當前頁顯示數據的起始索引和結束索引
this.page_current = page_current;
setStar_index((page_current-1)*getPage_display_count());
this.end_index = getPage_current()<getPage_count()? getStar_index()+getPage_display_count()-1 : getCount()-1;
}
public int getEnd_index() {
return end_index;
}
public void setEnd_index(int end_index) {
this.end_index = end_index;
}
public int getPage_count() {
return page_count;
}
public void setPage_count(int page_count) {
this.page_count = page_count;
}
public List<T> getPage_results() {
return page_results;
}
public void setPage_results(List<T> page_results) {
this.page_results = page_results;
}
public void final_results(){                                    //根據設置好的當前頁的兩個索引值重新獲取到要顯示在當前頁面的數據集
setPage_results(results.subList(star_index, end_index+1));
}
}

頁面的代碼就不貼出來了,大家有不懂的可以留言我們一起討論

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章