@Entity
@Table(name = "sys_user")
@NamedQuery(name = "SysUser.findAll", query = "SELECT s FROM SysUser s")
public class SysUser implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "password")
private String password;
@Id
@Column(name = "user_id")
private String userId;
@Column(name = "office_id")
private String officeId;
//private SysOffice office;
@Column(name = "user_addr")
private String userAddr;
@Column(name = "user_age")
private int userAge;
@Column(name = "user_nm")
server---------------------------------------
public void testJpaSpeciationExecutor(){
int pageNo=0;
int pageSize=5;
PageRequest pageRequest=new PageRequest(pageNo, pageSize);
//通常使用Specification 的匿名内部类,
Specification<SysUser> specification=new Specification<SysUser>() {
@Override
public Predicate toPredicate(Root<SysUser> root,
CriteriaQuery<?> query, CriteriaBuilder cb) {
// Path path=root.get("id");
// Predicate predicate=cb.gt(path, 5);
Predicate p1=cb.like(root.get("id").as(String.class), "%"+"1"+"%");
Predicate p2=cb.equal(root.get("lastName").as(String.class), "sd");
Predicate p3=cb.like(root.get("email").as(String.class), "%s%");
// 构建组合的Predicate示例:
Predicate p = cb.and(p3,cb.or(p1,p2));
return p;
}
};
Page<SysUser> page= userRepository.findAll(specification,pageRequest);
System.out.println("总记录数"+page.getTotalElements());
System.out.println("当前第几页"+page.getNumber());
System.out.println("总页数"+page.getTotalPages());
System.out.println("当前页面的List"+page.getContent());
System.out.println("当前页面的记录数"+page.getNumberOfElements());
}
controller----------------------------------------------------------
@RequestMapping(value = {"list", ""})//把叫号机类型和负责人显示在网页 int pageNo, int pageSize,
public String list(HttpServletRequest request, HttpServletResponse response, Model model) {
String pageNo = request.getParameter("pageNo");
String pageSize = request.getParameter("pageSize");
System.out.println("pageNo " + pageNo + " pageSize " + pageSize);
if(pageNo == null || pageSize == null)
model.addAttribute("page", sysUserService.findAllSysUser(0, 5));
else
model.addAttribute("page", sysUserService.findAllSysUser(Integer.parseInt(pageNo), Integer.parseInt(pageSize)));
// String frontPageInfo = "<li><a href=\"javascript:\" οnclick=\"page(1,30,'');\"> 上一页</a></li>";
// String pageInfo =
return "userList";
}
前台:----------------------------------------------
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery/jquery-1.8.3.min.js"></script>
<!-- <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js "></script> -->
<html>
<head>
<title>用户管理</title>
<script type="text/javascript">
$(document).ready(function() {
});
function page(n,s){
$("#pageNo").val(n);
$("#pageSize").val($("#inputPageSize").val());
// $("#currentPageNo").val(currentPageNo);
alert("n = " + n + " s = " + $("#inputPageSize").val());
$("#searchForm").submit();
return false;
}
</script>
</head>
<body>
<form:form id="searchForm" modelAttribute="SysUser" action="${pageContext.request.contextPath}/user/list" method="post" class="breadcrumb form-search">
<input id="pageNo" name="pageNo" type="hidden" value="0"/>
<input id="pageSize" name="pageSize" type="hidden" value="${page.getNumberOfElements()}"/>
<ul class="ul-form">
<li class="btns"><input id="btnSubmit" class="btn btn-primary" type="submit" value="查询"/></li>
<li class="clearfix"></li>
</ul>
</form:form>
<%-- <sys:message content="${message}"/> --%>
<table id="contentTable" class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th>用户ID</th>
<th>部门ID</th>
<th>用户地址ID</th>
<th>用户姓名</th>
<th>用户年龄</th>
<%-- <shiro:hasPermission name="brchmng:brchMng:edit"><th>操作</th></shiro:hasPermission> --%>
</tr>
</thead>
<tbody>
<c:forEach items="${page.content}" var="user">
<tr>
<td><a href="${ctx}/brchmng/brchMng/form?id=${brchMng.id}">
${user.userId}
</a></td>
<td>
${user.officeId}
</td>
<td>
${user.userAddr}
</td>
<td>
${user.userNm}
</td>
<td>
${user.userAge}
</td>
</tr>
</c:forEach>
</tbody>
</table>
<div>
第${page.getNumber() + 1}页/共${page.getTotalPages()}页 每页<input id="inputPageSize" name="inputPageSize"
οnkeypress="if(event.keyCode==13) {page(0, 5);return false;}" value="${page.getNumberOfElements()}"/>条 <a
href="javascript:" οnclick="page(0,${page.getNumberOfElements()});">首页</a><span>
</span>
<c:choose>
<c:when test="${page.getNumber() + 1 == 1}">
上一页
</c:when>
<c:otherwise>
<a
href="javascript:" οnclick="page(${page.getNumber() - 1},${page.getNumberOfElements()});">上一页</a>
</c:otherwise>
</c:choose>
<%--计算begin和end --%>
<c:choose>
<%--如果总页数不足10,那么就把所有的页都显示出来 begin和end是用来显示在前台的页码--%>
<c:when test="${page.getTotalPages() <= 10}">
<c:set var="begin" value="1" />
<c:set var="end" value="${page.getTotalPages() - 1}" />
<c:choose>
<c:when test="${page.getTotalPages() == 0}"><!-- 如果是空表 -->
<c:set var="begin" value="0" />
<c:set var="end" value="0" />
</c:when>
</c:choose>
</c:when>
<c:otherwise>
<%--如果总页数大于10,通过公式计算出begin和end --%>
<c:set var="begin" value="${page.getNumber() + 1 - 5}" />
<c:set var="end" value="${page.getNumber() + 1 + 4 }" />
<%--头溢出 --%>
<c:if test="${begin<=1}">
<c:set var="begin" value="1"></c:set>
<c:set var="end" value="10"></c:set>
</c:if>
<%--尾溢出 --%>
<c:if test="${end > page.getTotalPages()}">
<c:set var="begin" value="${page.getNumber() + 1 - 9 }"></c:set>
<c:set var="end" value="${page.getTotalPages()}"></c:set>
</c:if>
</c:otherwise>
</c:choose>
<%--循环显示页码列表 --%>
<c:forEach var="i" begin="${begin}" end="${end}">
<c:choose>
<c:when test="${i == page.getNumber() + 1}">
[${i}]
</c:when>
<c:otherwise>
<a
href="javascript:" οnclick="page(${i - 1}, ${page.getNumberOfElements()});">[${i}]</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:choose>
<c:when test="${page.getNumber() + 1 == page.getTotalPages()}">
下一页
</c:when>
<c:otherwise>
<a
href="javascript:" οnclick="page(${page.getNumber() + 1},${page.getNumberOfElements()});">
下一页</a>
</c:otherwise>
</c:choose>
<span> </span><a
href="javascript:" οnclick="page(${page.getTotalPages()} - 1,${page.getNumberOfElements()});">尾页</a>
</div>
</body>
</html>
@Table(name = "sys_user")
@NamedQuery(name = "SysUser.findAll", query = "SELECT s FROM SysUser s")
public class SysUser implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "password")
private String password;
@Id
@Column(name = "user_id")
private String userId;
@Column(name = "office_id")
private String officeId;
//private SysOffice office;
@Column(name = "user_addr")
private String userAddr;
@Column(name = "user_age")
private int userAge;
@Column(name = "user_nm")
private String userNm;
}
server---------------------------------------
public void testJpaSpeciationExecutor(){
int pageNo=0;
int pageSize=5;
PageRequest pageRequest=new PageRequest(pageNo, pageSize);
//通常使用Specification 的匿名内部类,
Specification<SysUser> specification=new Specification<SysUser>() {
@Override
public Predicate toPredicate(Root<SysUser> root,
CriteriaQuery<?> query, CriteriaBuilder cb) {
// Path path=root.get("id");
// Predicate predicate=cb.gt(path, 5);
Predicate p1=cb.like(root.get("id").as(String.class), "%"+"1"+"%");
Predicate p2=cb.equal(root.get("lastName").as(String.class), "sd");
Predicate p3=cb.like(root.get("email").as(String.class), "%s%");
// 构建组合的Predicate示例:
Predicate p = cb.and(p3,cb.or(p1,p2));
return p;
}
};
Page<SysUser> page= userRepository.findAll(specification,pageRequest);
System.out.println("总记录数"+page.getTotalElements());
System.out.println("当前第几页"+page.getNumber());
System.out.println("总页数"+page.getTotalPages());
System.out.println("当前页面的List"+page.getContent());
System.out.println("当前页面的记录数"+page.getNumberOfElements());
}
controller----------------------------------------------------------
@RequestMapping(value = {"list", ""})//把叫号机类型和负责人显示在网页 int pageNo, int pageSize,
public String list(HttpServletRequest request, HttpServletResponse response, Model model) {
String pageNo = request.getParameter("pageNo");
String pageSize = request.getParameter("pageSize");
System.out.println("pageNo " + pageNo + " pageSize " + pageSize);
if(pageNo == null || pageSize == null)
model.addAttribute("page", sysUserService.findAllSysUser(0, 5));
else
model.addAttribute("page", sysUserService.findAllSysUser(Integer.parseInt(pageNo), Integer.parseInt(pageSize)));
// String frontPageInfo = "<li><a href=\"javascript:\" οnclick=\"page(1,30,'');\"> 上一页</a></li>";
// String pageInfo =
return "userList";
}
前台:----------------------------------------------
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery/jquery-1.8.3.min.js"></script>
<!-- <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js "></script> -->
<html>
<head>
<title>用户管理</title>
<script type="text/javascript">
$(document).ready(function() {
});
function page(n,s){
$("#pageNo").val(n);
$("#pageSize").val($("#inputPageSize").val());
// $("#currentPageNo").val(currentPageNo);
alert("n = " + n + " s = " + $("#inputPageSize").val());
$("#searchForm").submit();
return false;
}
</script>
</head>
<body>
<form:form id="searchForm" modelAttribute="SysUser" action="${pageContext.request.contextPath}/user/list" method="post" class="breadcrumb form-search">
<input id="pageNo" name="pageNo" type="hidden" value="0"/>
<input id="pageSize" name="pageSize" type="hidden" value="${page.getNumberOfElements()}"/>
<ul class="ul-form">
<li class="btns"><input id="btnSubmit" class="btn btn-primary" type="submit" value="查询"/></li>
<li class="clearfix"></li>
</ul>
</form:form>
<%-- <sys:message content="${message}"/> --%>
<table id="contentTable" class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th>用户ID</th>
<th>部门ID</th>
<th>用户地址ID</th>
<th>用户姓名</th>
<th>用户年龄</th>
<%-- <shiro:hasPermission name="brchmng:brchMng:edit"><th>操作</th></shiro:hasPermission> --%>
</tr>
</thead>
<tbody>
<c:forEach items="${page.content}" var="user">
<tr>
<td><a href="${ctx}/brchmng/brchMng/form?id=${brchMng.id}">
${user.userId}
</a></td>
<td>
${user.officeId}
</td>
<td>
${user.userAddr}
</td>
<td>
${user.userNm}
</td>
<td>
${user.userAge}
</td>
</tr>
</c:forEach>
</tbody>
</table>
<div>
第${page.getNumber() + 1}页/共${page.getTotalPages()}页 每页<input id="inputPageSize" name="inputPageSize"
οnkeypress="if(event.keyCode==13) {page(0, 5);return false;}" value="${page.getNumberOfElements()}"/>条 <a
href="javascript:" οnclick="page(0,${page.getNumberOfElements()});">首页</a><span>
</span>
<c:choose>
<c:when test="${page.getNumber() + 1 == 1}">
上一页
</c:when>
<c:otherwise>
<a
href="javascript:" οnclick="page(${page.getNumber() - 1},${page.getNumberOfElements()});">上一页</a>
</c:otherwise>
</c:choose>
<%--计算begin和end --%>
<c:choose>
<%--如果总页数不足10,那么就把所有的页都显示出来 begin和end是用来显示在前台的页码--%>
<c:when test="${page.getTotalPages() <= 10}">
<c:set var="begin" value="1" />
<c:set var="end" value="${page.getTotalPages() - 1}" />
<c:choose>
<c:when test="${page.getTotalPages() == 0}"><!-- 如果是空表 -->
<c:set var="begin" value="0" />
<c:set var="end" value="0" />
</c:when>
</c:choose>
</c:when>
<c:otherwise>
<%--如果总页数大于10,通过公式计算出begin和end --%>
<c:set var="begin" value="${page.getNumber() + 1 - 5}" />
<c:set var="end" value="${page.getNumber() + 1 + 4 }" />
<%--头溢出 --%>
<c:if test="${begin<=1}">
<c:set var="begin" value="1"></c:set>
<c:set var="end" value="10"></c:set>
</c:if>
<%--尾溢出 --%>
<c:if test="${end > page.getTotalPages()}">
<c:set var="begin" value="${page.getNumber() + 1 - 9 }"></c:set>
<c:set var="end" value="${page.getTotalPages()}"></c:set>
</c:if>
</c:otherwise>
</c:choose>
<%--循环显示页码列表 --%>
<c:forEach var="i" begin="${begin}" end="${end}">
<c:choose>
<c:when test="${i == page.getNumber() + 1}">
[${i}]
</c:when>
<c:otherwise>
<a
href="javascript:" οnclick="page(${i - 1}, ${page.getNumberOfElements()});">[${i}]</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:choose>
<c:when test="${page.getNumber() + 1 == page.getTotalPages()}">
下一页
</c:when>
<c:otherwise>
<a
href="javascript:" οnclick="page(${page.getNumber() + 1},${page.getNumberOfElements()});">
下一页</a>
</c:otherwise>
</c:choose>
<span> </span><a
href="javascript:" οnclick="page(${page.getTotalPages()} - 1,${page.getNumberOfElements()});">尾页</a>
</div>
</body>
</html>