基础巩固--Jsp Paging Demo

分页功能是在mysql+spring+jsp 下demo

1、效果图


基础巩固--Jsp Paging Demo_第1张图片
2、DAO查询

public Paging getAllCustomer(int pageNumber, int pageSize) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", pageNumber);
		map.put("limit", pageSize);
		Paging paging = new Paging();

		String sql = "select * from customer";
		String countSql = SqlUtils.buildCountQuery(sql);
		paging.setTotalCount(template.queryForInt(countSql, map));

		String querySql = SqlUtils.buildLimitedQuery(sql);
		paging.setCollections(template.query(querySql, ParameterizedBeanPropertyRowMapper.newInstance(Customer.class),map));

		return paging;
	}
public class SqlUtils {
	// static final String DEFAULT_DATABASE = "hsql";
	static final String DEFAULT_DATABASE = "mysql";

	public static String buildLimitedQuery(String sql) {
		if ("hsql".equals(DEFAULT_DATABASE)) {
			return buildHsqldbLimitedQuery(sql, true);
		} else if ("mysql".equals(DEFAULT_DATABASE)) {
			return buildMysqlLimitedQuery(sql, true);
		}
		return "";
	}

	public static String buildHsqldbLimitedQuery(String sql, boolean hasOffset) {
		return new StringBuffer(sql.length() + 10).append(sql).insert(
				sql.toLowerCase().indexOf("select") + 6,
				hasOffset ? " limit :start :limit" : " top :limit").toString();
	}

	public static String buildMysqlLimitedQuery(String sql, boolean hasOffset) {
		return new StringBuffer(sql.length() + 20).append(sql).append(
				hasOffset ? " limit :start, :limit" : " limit :limit")
				.toString();
	}

	public static String buildCountQuery(String sql) {
		return "select count(*) from (" + sql + ") cnt_sub";
	}
}
 
public class Paging {
	private int totalCount;
	private Collection<?> collections;

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public Collection<?> getCollections() {
		return collections;
	}

	public void setCollections(Collection<?> collections) {
		this.collections = collections;
	}
}

 

3、spring action

@RequestMapping("/customer/list.do")
	public String list(String pageNumberStr, Model model) {
		int pageNumber = 1;
		if (pageNumberStr != null && !pageNumberStr.isEmpty()) {
			pageNumber = Integer.parseInt(pageNumberStr);
		}

		int pageSize = 5; //分页大小

		Paging paging = customerManager.getAllCustomer((pageNumber - 1) * pageSize, pageSize);

		int totalPosts = paging.getTotalCount();//总笔数
		int totalPages = totalPosts / pageSize + ((totalPosts % pageSize) > 0 ? 1 : 0);//总页数

		model.addAttribute("pageSize", pageSize);
		model.addAttribute("totalPosts", totalPosts);
		model.addAttribute("pageNumber", pageNumber);//页码
		model.addAttribute("totalPages", totalPages);

		model.addAttribute("results", paging.getCollections());
		return "customer/list";
	}

 

4、list.jsp

 

<%@ page language="java" contentType="text/html; charset=GBK"
	pageEncoding="GBK"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>

<script type="text/javascript">

//第一页
 function first_page(formName) {
 
	if(formName){
	var page = parseInt(formName.pageNumberStr.value);
	if(page > 1) {
		formName.pageNumberStr.value = 1;
		formName.submit();
	}
	}
}

//上一页
function prev_page(formName) {
	var page = parseInt(formName.pageNumberStr.value);
	if(page > 1) {
		formName.pageNumberStr.value = page - 1;
		formName.submit();
	}
}

//下一页
function next_page(formName) {
	var page = parseInt(formName.pageNumberStr.value);
	var pages = parseInt(formName.totalPages.value);
	if(page < pages) {
		formName.pageNumberStr.value = page + 1;
		formName.submit();
	}
}

//末页
function last_page(formName) {
	var page = parseInt(formName.pageNumberStr.value);
	var pages = parseInt(formName.totalPages.value);
	alert('page:'+ page +':pages'+ pages);
	if(page < pages) {
		formName.pageNumberStr.value = formName.totalPages.value;
		formName.submit();
	}
}
 
 //选择某一页
 function gotoSelectedPage(formName,_pageNumber)  
 {
 	alert(_pageNumber);
     formName.pageNumberStr.value=_pageNumber;
     formName.submit();  
 }  
 </script>

<% String rootPath = request.getContextPath(); %>

<h1>Paging Demo</h1>
<hr />

<table>
	<thead>
		<tr align="center">
			<td width="10%">id</td>
			<td width="70%">no</td>
			<td colspan="3">name</td>
		</tr>
	</thead>
	<tbody>
		<c:forEach items="${results}" var="customer">
			<tr align="center">
				<td>${customer.id}</td>
				<td>${customer.customerNo}</td>
				<td>${customer.customerName}</td>
			</tr>
		</c:forEach>
	</tbody>
	<tfoot>
		<tr align="center">
			<td colspan="5">

			<form action="<%=rootPath %>/customer/list.do" method="get" id="navigatorForm">
			<input type="hidden" name="pageNumberStr" id="pageNumberStr" value="${pageNumber}"/>
			<input type="hidden" name="totalPages" id="totalPages" value="${totalPages}"/>
			总共${totalPosts}条记录${totalPages}页,当前第${pageNumber}页
			
			<a href="javascript:first_page(document.getElementById('navigatorForm'))">第一页</a>
			<c:if test="${pageNumber>1}">
				<a href="javascript:prev_page(document.getElementById('navigatorForm'))">上一页</a>
			</c:if>
			 跳转到第 <select name="_pageNumber" id="_pageNumber" onchange="gotoSelectedPage(document.getElementById('navigatorForm'),this.value);">
				<c:forEach begin="1" end="${totalPages}" step="1" var="pageIndex">
					<c:choose>
						<c:when test="${pageIndex eq pageNumber}">
							<option value="${pageIndex}" selected="selected">${pageIndex}</option>
						</c:when>
						<c:otherwise>
							<option value="${pageIndex}">${pageIndex}</option>
						</c:otherwise>
					</c:choose>
				</c:forEach>
			</select>
			页 
			<c:if test="${pageNumber<totalPages}">
				<a href="javascript:next_page(document.getElementById('navigatorForm'))">下一页</a>
			</c:if>
			<a href="javascript:last_page(document.getElementById('navigatorForm'))">末页</a>
			</form>

			</td>
		</tr>
	</tfoot>
</table>

<hr/>
</body>
</html>
  

 

你可能感兴趣的:(JavaScript,sql,c,jsp,C#)