PageModel类
package com.wepull.bookSys.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
public class PageModel {
//查询的起始位置 limit ?,? 中的第一个参数
private int offset;
//一页显示记录条数 limit ?,? 中的第二参数
private int lineSize=2; //默认值 显示3条数据
//记录总数
private int totalRecords;
//存放结果集
private List results;
//请求URI
private String url;
//存放查询参数
private Map<String, String> searchParam=new HashMap<String, String>();
//总页数
private int totalPage;
//当前页
private int pageNo;
//生成的分页表单
private String pageList;
/**
* @return 返回当前页数
*/
public int getPageNo() {
return pageNo;
}
/**
* @return 返回总页数
*/
public int getTotalPage() {
return (totalRecords+lineSize-1)/lineSize;
}
public int getOffset() {
if(this.pageNo==0) {
return 0;
}
return (this.pageNo-1)*this.lineSize;
}
public void setOffset(int offset) {
this.offset = offset;
}
public int getLineSize() {
return lineSize;
}
public void setLineSize(int lineSize) {
this.lineSize = lineSize;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public void setTotal(int total) {
this.totalRecords = total;
}
public List getResults() {
return results;
}
public void setResults(List results) {
this.results = results;
}
public Map<String, String> getSearchParam() {
return searchParam;
}
public void setSearchParam(Map<String, String> searchParam) {
this.searchParam = searchParam;
}
public String getPageList() {
int pageCount = getTotalPage(); //计算总页数
//
//拼写要输出到页面的HTML文本
StringBuilder sb = new StringBuilder();
sb.append("<style type=\"text/css\">");
sb.append(".pagination {padding: 5px;float:right;font-size:12px;}");
sb.append(".pagination a, .pagination a:link, .pagination a:visited {padding:2px 5px;margin:2px;border:1px solid #aaaadd;text-decoration:none;color:#006699;}");
sb.append(".pagination a:hover, .pagination a:active {border: 1px solid #ff0000;color: #000;text-decoration: none;}");
sb.append(".pagination span.current {padding: 2px 5px;margin: 2px;border: 1px solid #B1D5F0;font-weight: bold;background-color: #B1D5F0;color: #FFF0;}");
sb.append(".pagination span.disabled {padding: 2px 5px;margin: 2px;border: 1px solid #eee; color: #ddd;}");
sb.append("</style>\r\n");
sb.append("<div class=\"pagination\">\r\n");
if(getTotalPage() == 0){
sb.append("<strong>没有可显示的项目</strong>\r\n");
}else{
//页号越界处理
if(pageNo > pageCount){ pageNo = pageCount; }
if(pageNo < 1){ pageNo = 1; }
sb.append("<form method=\"post\" action=\"").append(this.url)
.append("\" name=\"qPagerForm\" id=\"qPagerForm1\" >\r\n");
//获取请求中的所有参数
Set<Map.Entry<String, String>> set =this.searchParam.entrySet();
Iterator<Map.Entry<String, String>> it = set.iterator();
String name = null; //参数名
String value = null; //参数值
//把请求中的所有参数当作隐藏表单域
while (it.hasNext()) {
Map.Entry<String, String> entry= it.next();
name = entry.getKey();
value = entry.getValue();
// 去除页号
if (name.equals("pageNo")) {
if (null != value && !"".equals(value)) {
pageNo = Integer.parseInt(value);
}
continue;
}
sb.append("<input type=\"hidden\" name=\"")
.append("pm.searchParam."+name)
.append("\" value=\"")
.append(value)
.append("\"/>\r\n");
}
// 把当前页号设置成请求参数
sb.append("<input type=\"hidden\" name=\"").append("pm.pageNo")
.append("\" value=\"").append(pageNo).append("\" id='pageNo1' />\r\n");
// 把总页数设置成请求参数
sb.append("<input type=\"hidden\" name=\"").append("pm.totalRecords")
.append("\" value=\"").append(totalRecords).append("\" />\r\n");
// 输出统计数据
sb.append(" 共<strong>").append(this.totalRecords)
.append("</strong>项")
.append(",<strong>")
.append(pageCount)
.append("</strong>页: \r\n");
//上一页处理
if (pageNo == 1) {
sb.append("<span class=\"disabled\">« 上一页")
.append("</span>\r\n");
} else {
sb.append("<a href=\"javascript:turnOverPage(")
.append((pageNo - 1))
.append(")\">« 上一页</a>\r\n");
}
//如果前面页数过多,显示"..."
int start = 1;
if(this.pageNo > 4){
start = this.pageNo - 1;
sb.append("<a href=\"javascript:turnOverPage(1)\">1</a>\r\n");
sb.append("<a href=\"javascript:turnOverPage(2)\">2</a>\r\n");
sb.append("…\r\n");
}
//显示当前页附近的页
int end = this.pageNo + 1;
if(end > pageCount){
end = pageCount;
}
for(int i = start; i <= end; i++){
if(pageNo == i){ //当前页号不需要超链接
sb.append("<span class=\"current\">")
.append(i)
.append("</span>\r\n");
}else{
sb.append("<a href=\"javascript:turnOverPage(")
.append(i)
.append(")\">")
.append(i)
.append("</a>\r\n");
}
}
//如果后面页数过多,显示"..."
if(end < pageCount - 2){
sb.append("…\r\n");
}
if(end < pageCount - 1){
sb.append("<a href=\"javascript:turnOverPage(")
.append(pageCount - 1)
.append(")\">")
.append(pageCount - 1)
.append("</a>\r\n");
}
if(end < pageCount){
sb.append("<a href=\"javascript:turnOverPage(")
.append(pageCount)
.append(")\">")
.append(pageCount)
.append("</a>\r\n");
}
//下一页处理
if (pageNo == pageCount) {
sb.append("<span class=\"disabled\">下一页 »")
.append("</span>\r\n");
} else {
sb.append("<a href=\"javascript:turnOverPage(")
.append((pageNo + 1))
.append(")\">下一页 »</a>\r\n");
}
sb.append("</form>\r\n");
// 生成提交表单的JS
sb.append("<script language=\"javascript\">\r\n");
sb.append(" function turnOverPage(no){\r\n");
sb.append(" if(no>").append(pageCount).append("){");
sb.append(" no=").append(pageCount).append(";}\r\n");
sb.append(" if(no<1){no=1;}\r\n");
sb.append(" document.getElementById('pageNo1').value=no;\r\n");
sb.append(" document.getElementById('qPagerForm1').submit();\r\n");
sb.append(" }\r\n");
sb.append("</script>\r\n");
}
sb.append("</div>\r\n");
//
System.out.println(sb.toString());
return sb.toString();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
}
BookAction中使用
package com.wepull.bookSys.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.mysql.jdbc.util.ServerController;
import com.opensymphony.xwork2.ActionSupport;
import com.wepull.bookSys.biz.BookBiz;
import com.wepull.bookSys.biz.TypeBiz;
import com.wepull.bookSys.dto.BookDto;
import com.wepull.bookSys.dto.TypeDto;
import com.wepull.bookSys.entity.Book;
import com.wepull.bookSys.util.PageModel;
public class BookAction extends ActionSupport {
private BookBiz bookBiz;
private TypeBiz typeBiz;
private BookDto bookDto;
private List<BookDto> bookList ;
private List<TypeDto> typeList;
private PageModel pm = new PageModel();
private PageControllerTag pc = new PageControllerTag();
public List<TypeDto> getTypeList() {
return typeList;
}
public String list(){
HttpServletRequest request = ServletActionContext.getRequest();
String url = (request.getRequestURL()).toString()+"?"+request.getQueryString();
System.out.println(url);
pm = bookBiz.list(pm);
bookList = pm.getResults();
pm.setUrl(url);
return SUCCESS;
}
/
public void setBookBiz(BookBiz bookBiz) {
this.bookBiz = bookBiz;
}
public void setBookDto(BookDto bookDto) {
this.bookDto = bookDto;
}
public BookDto getBookDto() {
return bookDto;
}
public List<BookDto> getBookList() {
return bookList;
}
public void setTypeBiz(TypeBiz typeBiz) {
this.typeBiz = typeBiz;
}
public PageModel getPm() {
return pm;
}
public void setPm(PageModel pm) {
this.pm = pm;
}
public void setBookList(List<BookDto> bookList) {
this.bookList = bookList;
}
}
BookBiz中使用
package com.wepull.bookSys.biz;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import com.wepull.bookSys.dao.BookDao;
import com.wepull.bookSys.dao.TypeDao;
import com.wepull.bookSys.dto.BookDto;
import com.wepull.bookSys.dto.TypeDto;
import com.wepull.bookSys.entity.Book;
import com.wepull.bookSys.entity.Type;
import com.wepull.bookSys.util.PageModel;
public class BookBizImpl implements BookBiz {
private BookDao bookDao;
private TypeDao typeDao;
public PageModel list(PageModel pm) {
System.out.println("biz层--list()");
List<BookDto> listDto = new ArrayList<BookDto>();
List<Book> listEntity = bookDao.list(pm).getResults();
for (Book book : listEntity) {
BookDto bookDto = new BookDto();
System.out.println(book.getType().getName());
BeanUtils.copyProperties(book, bookDto);
//实体类book与bookDto中不是同名属性的需要手动 处理
Type type = book.getType();
TypeDto typeDto = new TypeDto();
BeanUtils.copyProperties(type, typeDto);
bookDto.setTypeDto(typeDto);
listDto.add(bookDto);
}
pm.setResults(listDto);
return pm;
}
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public void setTypeDao(TypeDao typeDao) {
this.typeDao = typeDao;
}
public Book test(int id) {
Book book = bookDao.test(id);
return book;
}
}
BookDao中使用
package com.wepull.bookSys.dao;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.wepull.bookSys.entity.Book;
import com.wepull.bookSys.util.PageModel;
public class BookDaoImpl extends EntityDao implements BookDao {
public void add(Book book) {
this.getHibernateTemplate().save(book);
}
public void delete(int id) {
Book book = findById(id);
this.getHibernateTemplate().delete(book);
// Session session = sessionFactory.getCurrentSession();
// Query q = session.createQuery("delete from Book b where b.id = :id");
// q.setInteger("id", id).executeUpdate();
}
public Book findById(int id) {
return(Book) this.getHibernateTemplate().get(Book.class, id);
}
public PageModel list(PageModel pm) {
Map<String, String> map = pm.getSearchParam();
String bookName = map.get("bookName"); //"查询参数bookName
String typeName = map.get("typeName"); //查询参数typeName
bookName=(bookName==null?"":bookName);
Session session = getSession(); //获取HibernateDaoSupport 中的session
Criteria c = session.createCriteria(Book.class)
.add(Restrictions.like("name", "%"+bookName+"%"));
if(typeName!=null&&!"".equalsIgnoreCase(typeName)){//如果前台传来 typeName 的查询参数 ,则进行2张表关联查询
c = c.createCriteria("type")
.add(Restrictions.eq("name", typeName));
}
List<Book> list =c.setFirstResult(pm.getOffset())
.setMaxResults(pm.getLineSize())
.list();
if(pm.getTotalRecords()==0){
pm.setTotalRecords(this.getTotalRecords(c)) ;
}
System.out.println("总记录数:"+pm.getTotalRecords());
pm.setResults(list);
/*
List<Book> list = session.createCriteria(Book.class)
.setFirstResult(pm.getOffset())
.setMaxResults(pm.getLineSize())
.list();
pm.setTotalRecords(getTotalRecords());
pm.setResults(list);*/
return pm;
}
public PageModel listHQL(PageModel pm) {
System.out.println("dao层--list()");
Map<String, String> map = pm.getSearchParam();
String bookName = map.get("bookName"); //"查询参数bookName
String typeName = map.get("typeName"); //查询参数typeName
bookName=(bookName==null?"":bookName);
Session session = getSession(); //获取HibernateDaoSupport 中的session
Criteria c = session.createCriteria(Book.class)
.add(Restrictions.like("name", "%"+bookName+"%"));
if(typeName!=null&&!"".equalsIgnoreCase(typeName)){//如果前台传来 typeName 的查询参数 ,则进行2张表关联查询
c = c.createCriteria("type")
.add(Restrictions.eq("name", typeName));
}
if(pm.getTotalRecords()==0){
pm.setTotalRecords(this.getTotalRecords(c)) ;
}
List<Book> list =c.setFirstResult(pm.getOffset())
.setMaxResults(pm.getLineSize())
.list();
System.out.println("总记录数:"+pm.getTotalRecords());
pm.setResults(list);
/*
List<Book> list = session.createCriteria(Book.class)
.setFirstResult(pm.getOffset())
.setMaxResults(pm.getLineSize())
.list();
pm.setTotalRecords(getTotalRecords());
pm.setResults(list);*/
return pm;
}
public void update(Book book) {
getHibernateTemplate().update(book);
}
public Book test(int id) {
return (Book) getHibernateTemplate().load(Book.class, id);
}
}
生成的前台表单
<style type="text/css">
.pagination {padding: 5px;float:right;font-size:12px;}.pagination a, .pagination a:link, .pagination a:visited {padding:2px 5px;margin:2px;border:1px solid #aaaadd;text-decoration:none;color:#006699;}.pagination a:hover, .pagination a:active {border: 1px solid #ff0000;color: #000;text-decoration: none;}.pagination span.current {padding: 2px 5px;margin: 2px;border: 1px solid #B1D5F0;font-weight: bold;background-color: #B1D5F0;color: #FFF0;}.pagination span.disabled {padding: 2px 5px;margin: 2px;border: 1px solid #eee; color: #ddd;}
</style>
<div class="pagination">
<form method="post" action="http://192.168.167.110:8080/1102_SSH__BookSys__3.0/book/Book-list?null" name="qPagerForm" id="qPagerForm1" >
<input type="hidden" name="pm.searchParam.typeName" value="武侠类"/>
<input type="hidden" name="pm.searchParam.bookName" value="2"/>
<input type="hidden" name="pm.pageNo" value="1" id='pageNo1' />
<input type="hidden" name="pm.totalRecords" value="3" />
共<strong>3</strong>项,<strong>2</strong>页: <span class="disabled">« 上一页</span> <span class="current">1</span> <a href="javascript:turnOverPage(2)">2</a> <a href="javascript:turnOverPage(2)">下一页 »</a>
</form>
<script language="javascript">
function turnOverPage(no){
if(no>2){ no=2;}
if(no<1){no=1;}
document.getElementById('pageNo1').value=no;
document.getElementById('qPagerForm1').submit();
}
</script>
</div>