分页标签的使用
1.commons.tld(自定义文件,名字无所谓)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>2.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>common</short-name>
<uri>http://heima/commons/</uri>
<tag>
<name>page</name>
<tag-class>com.heima.crm.util.NavigationTag</tag-class>
<body-content>JSP</body-content>
<description>create navigation for paging</description>
<attribute>
<name>bean</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>url</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
2.page.java (控制分页的页数,页码,查询列表)
public class Page<T> {
private int total;
private int page;
private int size;
private List<T> rows;
public Page() {
super();
}
/**
*
* @param total查询数据的总条数
* @param currentpage 当前页码
* @param size 每页展示数据的条数
* @param rows查询的结果集
*/
public Page(int total, int page, int size, List<T> rows) {
super();
this.total = total;
this.page = page;
this.size = size;
this.rows = rows;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public List<T> getRows() {
return rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
}
}
3.navigationTag.java
package com.heima.crm.util;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 自定义标签的使用
* 显示格式:上一页 1,2,3,4,5 下一页
* @author lenovo
*
*/
public class NavigationTag extends TagSupport {
/**
* request 中用于保存Page<E> 对象的变量名,默认为“page”
*/
private String bean="page";
/**
* 分页跳转的url 地址此属性必须
*/
private String url=null;
/**
* 显示页码数量
*/
private int number =5;
@Override
public int doStartTag() throws JspException {
JspWriter writer = super.pageContext.getOut();
HttpServletRequest request=(HttpServletRequest) super.pageContext.getRequest();
Page page=(Page) request.getAttribute(bean);
if(page ==null)
return SKIP_BODY;
url=resolveUrl(url,pageContext);
try {
//计算总页数
int pageCount=page.getTotal()/page.getSize();
if(page.getTotal()%page.getSize()>0){
pageCount++;
}
writer.print("<nav><ul class=\"pagination\">");
//显示上一页 按钮
if(page.getPage()>1){
String preUrl=append(url, "page", page.getPage()-1);
append(preUrl, "rows", page.getSize());
writer.print("<li><a href=\""+preUrl+"\">上一页</a></li>");
}else{
writer.print("<li class=\"disabled\"><a href=\"#\">上一页</a></li>");
}
int indexPage=(page.getPage()-2>0)?page.getPage()-2:1;
for(int i =1; i<=number &&indexPage<=pageCount;indexPage++,i++){
if(indexPage==page.getPage()){
writer.print("<li class=\"active\"><a href=\"#\">"+indexPage+
"<span class=\"sr-only\">(current)</span></a></li>");
continue;
}
String pageUrl = append(url, "page", indexPage);
pageUrl=append(pageUrl, "rows", page.getSize());
writer.print("<li><a href=\""+pageUrl+"\">"+indexPage+"</a></li>");
}
//显示下一页
if(page.getPage()<pageCount){
String nextUrl=append(url, "page", page.getPage()-1);
nextUrl=append(nextUrl, "rows", page.getSize());
writer.print("<li><a href=\""+nextUrl+"\">下一页</a></li>");
}else{
writer.print("<li class=\"disabled\"><a href=\"#\">下一页</a></li>");
}
writer.print("</nav>");
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
private String append(String url,String key,int value){
return append(url, key, String.valueOf(value));
}
/**
* 为url添加参数对
* @param url
* @param key
* @param value
* @return
*/
private String append(String url,String key,String value){
if(url==null||url.trim().length()==0){
return "";
}
if(url.indexOf("?")==-1){
url=url+"?"+key+"="+value;
}else{
if(url.endsWith("?")){
url=url+key+"="+value;
}else{
url=url+"&"+key+"="+value;
}
}
return url;
}
/**
* 为url添加翻页的请求参数
* @param url
* @param pageContext
* @return
* @throws JspException
*/
private String resolveUrl(String url,javax.servlet.jsp.PageContext pageContext) throws JspException{
Map params = pageContext.getRequest().getParameterMap();
for (Object key : params.keySet()) {
if("page".equals(key)||"rows".equals(key))
continue;
Object value = params.get(key);
try {
if(value==null){
continue;
}
if(value.getClass().isArray()){
value=((String[]) value)[0];
url=append(url, key.toString(),value.toString());
}else if(value instanceof String){
value=(String)value;
url=append(url,key.toString(),value.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
return url;
}
/**
* @return the bean
*/
public String getBean() {
return bean;
}
/**
* @param bean the bean to set
*/
public void setBean(String bean) {
this.bean = bean;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
public void setNumber(int number) {
this.number = number;
}
}
4.页面jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib prefix="myTag" uri="http://heima/commons/" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>商品分类</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/bootstrap.css" />
<!--需要引入JQUERY-->
<script src="${pageContext.request.contextPath}/js/jquery-3.4.1.js" type="text/javascript" charset="utf-8"></script>
<!-- 最新的 Bootstrap核心 JavaScript 文件 -->
<script src="${pageContext.request.contextPath}/js/bootstrap.js" type="text/javascript" charset="utf-8"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
font{
font-family: "楷体";
font-size: 15;
}
</style>
</head>
<body>
<h3 align="center">客户列表</h3>
<form action="${pageContext.request.contextPath}/customer/list.action" id="Form" name="Form" method="post">
<table style="margin-top: 50px">
<tr>
<td><font>客户名称:</font></td>
<td><input class="textbox" name="custName" style="width: 100px" value="${custName}"/></td>
<td><font>客户来源:</font></td>
<td>
<select id="custSource" name="custSource">
<option value="">--请选择--</option>
<c:forEach items="${formType}" var="item">
<option value="${item.dict_id}"<c:if test="${item.dict_id==custSource}">selected</c:if>>${item.dict_item_name}</option>
</c:forEach>
</select>
</td>
<td><font>客户级别:</font></td>
<td>
<select id="custLevel" name="custLevel">
<option value="">--请选择--</option>
<c:forEach items="${levelType}" var="item">
<option value="${item.dict_id}"<c:if test="${item.dict_id==custLevel}">selected</c:if>>${item.dict_item_name}</option>
</c:forEach>
</select>
</td>
<td><font>客户行业:</font></td>
<td>
<select id="custIndustry" name="custIndustry">
<option value="">--请选择--</option>
<c:forEach items="${industryType}" var="item">
<option value="${item.dict_id}"<c:if test="${item.dict_id==custIndustry}">selected</c:if>>${item.dict_item_name}</option>
</c:forEach>
</select>
</td>
<td>
<font><input class="button" type="submit" value="搜索"></font>
</td>
</tr>
</table>
<table class="table table-condensed">
<tbody>
<thead>
<tr>
<th>ID</th>
<th>客户名字</th>
<th>客户级别</th>
<th>客户来源</th>
<th>客户行业</th>
<th>固定电话</th>
<th>移动电话</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${page.rows}" var="row">
<tr>
<td>${row.cust_id}</td>
<td>${row.cust_name}</td>
<td>${row.cust_level}</td>
<td>${row.cust_source}</td>
<td>${row.cust_industry}</td>
<td>${row.cust_phone}</td>
<td>${row.cust_mobile}</td>
<td>
<a href="${pageContext.request.contextPath}/customer_edit.action?cust_id="><span class="glyphicon glyphicon-edit">编辑</span></a>
<a href="${pageContext.request.contextPath}/customer_delete.action?cust_id="><span class="glyphicon glyphicon-trash">删除</span></a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<div class="col-md-12 text-right">
<myTag:page url="${pageContext.request.contextPath}/customer/list.action"/>
</div>
</form>
</body>
</html>
5.dao层customerMapper.java
package com.heima.crm.mapper;
import java.util.List;
import com.heima.crm.pojo.Customer;
import com.heima.crm.pojo.QueryVo;
/**
* 客户信息持久化接口
* @author lenovo
*
*/
public interface CustomerMapper {
/**
*根据查询条件分页查询用户列表
* @param vo
* @return
*/
List<Customer> getCustomerByQueryVo(QueryVo vo);
/**
* 根据查询条件查询总记录数
* @param vo
* @return
*/
Integer getCountByQueryVo(QueryVo vo);
}
6.customerMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.heima.crm.mapper.CustomerMapper">
<sql id="customer_where">
<where>
<if test="custName!=null and custName !=''">
and c.`cust_name` LIKE "%"#{custName}"%"
</if>
<if test="custSource !=null and custSource !=''">
AND c.`cust_source`=#{custSource}
</if>
<if test="custIndustry !=null and custIndustry !=''">
AND c.`cust_industry`=#{custIndustry}
</if>
<if test="custLevel !=null and custLevel !=''">
AND c.`cust_level`=#{custLevel}
</if>
</where>
</sql>
<select id="getCustomerByQueryVo" parameterType="queryvo" resultType="customer">
SELECT
c.`cust_id`,
c.`cust_name`,
s.`dict_item_name` `cust_source`,
i.`dict_item_name` `cust_industry`,
l.`dict_item_name` `cust_level`,
c.`cust_phone`,
c.`cust_mobile`,
c.`cust_imge`
FROM `cat_customer` c
LEFT JOIN base_dict s ON c.`cust_source`=s.`dict_id`
LEFT JOIN base_dict i ON c.`cust_industry`=i.`dict_id`
LEFT JOIN base_dict l ON c.`cust_level`=l.`dict_id`
<include refid="customer_where"></include>
LIMIT #{start}, #{rows};
</select>
<select id="getCountByQueryVo" parameterType="queryvo" resultType="int" >
SELECT count(1)
FROM `cat_customer` c
LEFT JOIN base_dict s ON c.`cust_source`=s.`dict_id`
LEFT JOIN base_dict i ON c.`cust_industry`=i.`dict_id`
LEFT JOIN base_dict l ON c.`cust_level`=l.`dict_id`
<include refid="customer_where"></include>
</select>
</mapper>
7.业务逻辑层接口customerService.java
package com.heima.crm.service;
import com.heima.crm.pojo.Customer;
import com.heima.crm.pojo.QueryVo;
import com.heima.crm.util.Page;
/**
* 客户信息业务逻辑接口
* @author lenovo
*
*/
public interface CustomerService {
/**
*根据查询条件分页查询用户列表
* @param vo
* @return
*/
Page<Customer> getCustomerByQueryVo(QueryVo vo);
}
8.业务逻辑层实现类customerServiceImpl.java
package com.heima.crm.service.Impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.heima.crm.mapper.CustomerMapper;
import com.heima.crm.pojo.Customer;
import com.heima.crm.pojo.QueryVo;
import com.heima.crm.service.CustomerService;
import com.heima.crm.util.Page;
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerMapper mapper;
@Override
public Page<Customer> getCustomerByQueryVo(QueryVo vo) {
//计算分页查询从那条记录开始
vo.setStart((vo.getPage()-1)*vo.getRows());
//查询总记录数
Integer total = mapper.getCountByQueryVo(vo);
//根据条件查询每页的数据列表
List<Customer> list = mapper.getCustomerByQueryVo(vo);
//包装分页数据
Page<Customer> page =new Page<Customer>(total, vo.getPage(), vo.getRows(), list);
return page;
}
}
9.控制层customerController.java
package com.heima.crm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.heima.crm.pojo.BaseDict;
import com.heima.crm.pojo.Customer;
import com.heima.crm.pojo.QueryVo;
import com.heima.crm.service.BaseDictService;
import com.heima.crm.service.CustomerService;
import com.heima.crm.util.Page;
/**
* 客户信息请求处理控制类
* @author lenovo
*
*/
@Controller
@RequestMapping("customer")
public class CustomerController {
@Autowired
private BaseDictService baseDictService;
@Autowired
private CustomerService customerService;
@Value("${customer_form_type}")
private String customer_form_type;
@Value("${customer_industry_type}")
private String customer_industry_type;
@Value("${customer_level_type}")
private String customer_level_type;
@RequestMapping("list")
public String list(Model model ,QueryVo vo){
//查询来源
List<BaseDict> formType = baseDictService.getBaseDictByCode(customer_form_type);
for (BaseDict baseDict : formType) {
System.out.println(baseDict.getDict_item_name().toString());
}
//查询行业
List<BaseDict> industryType = baseDictService.getBaseDictByCode(customer_industry_type);
//查询级别
List<BaseDict> levelType = baseDictService.getBaseDictByCode(customer_level_type);
//设置数据模型返回
model.addAttribute("formType", formType);
model.addAttribute("industryType", industryType);
model.addAttribute("levelType", levelType);
//根据查询条件分页查询数据列表
Page<Customer> page = customerService.getCustomerByQueryVo(vo);
//设置分页数据返回
model.addAttribute("page", page);
//数据回显
model.addAttribute("vo",vo);
return "customer/list";
}
@RequestMapping("myTag")
public String myTag(){
return "customer";
}
}