自定义分页标签结合spring mvc、bootstrap、mybatis、mysql的使用

news/2024/7/7 13:04:54

pager.tld

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
	version="2.0">
	<description>Pager</description>
	<tlib-version>1.0</tlib-version>
	<short-name>page</short-name>
	<uri></uri>
	<tag>
		<name>createPager</name>
		<tag-class>com.test.utils.Pager</tag-class>
		<body-content>JSP</body-content>
		<attribute>
			<name>curPage</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>java.lang.Integer</type>
		</attribute>
		<attribute>
			<name>totalPage</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>java.lang.Integer</type>
		</attribute>
		<attribute>
			<name>pageSize</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>java.lang.Integer</type>
		</attribute>
		<attribute>
			<name>totalCount</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>java.lang.Integer</type>
		</attribute>
		<attribute>
			<name>formId</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>java.lang.String</type>
		</attribute>
	</tag>
</taglib>

 Pager.java

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/***
 * 分页控件
 * @author 花非花雾非雾
 */
public class Pager extends TagSupport {

	private static final long serialVersionUID = 1L;

	private Integer curPage;
	private Integer totalPage;
	private Integer pageSize = TestConstance.pageSize;
	private Integer totalCount = 0;
	private String formId;

	public void setCurPage(Integer curPage) {
		this.curPage = curPage;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	public void setFormId(String formId) {
		this.formId = formId;
	}
	
	public Integer getTotalCount() {
		return totalCount;
	}

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

	public int doStartTag() throws JspException {
		
		JspWriter out = pageContext.getOut();

		int pageNumber = 0;
		if (totalPage%pageSize==0) {
			pageNumber = totalPage/pageSize;
		} else {
			pageNumber = (totalPage/pageSize)+1;
		}
		if (curPage < 1) {
			curPage = 1;
		}

		try {
			if (pageNumber > 0) {
				out.print("<script type='text/javascript'>" + 
						  		"function go(pageNum){" + 
						  			"var f = document.getElementById('" + formId + "');"+
						  			"f.action = f.action + '?pageNum=' + pageNum + '&pageSize="+pageSize+"';"+
						  			"f.submit();"+
		  						"}" + 
						  "</script>");
				
				out.print("<div class='pagination'><ul>");
				int start = 1;
				int end = totalPage;
				for(int i=4;i>=1;i--){
					if((curPage-i)>=1){
						start = curPage-i;
						break;
					}
				}
				for(int i=4;i>=1;i--){
					if((curPage+i)<=totalPage){
						end = curPage+i;
						break;
					}
				}
				//如果小于9则右侧补齐
				if(end-start+1<=9){
					Integer padLen = 9-(end-start+1);
					for(int i=padLen;i>=1;i--){
						if((end+i)<=totalPage){
							end = end+i;
							break;
						}
					}
				}
				
				//如果还小于9左侧补齐
				if(end-start+1<=9){
					Integer padLen = 9-(end-start+1);
					for(int i=padLen;i>=1;i--){
						if((start-i)>=1){
							start = start-i;
							break;
						}
					}
				}
				
				if(curPage>1){
					if(start>1){
						out.print("<li><a href='javascript:go(1)'>首页</a></li>");
					}
					out.print("<li><a href='javascript:go("+(curPage-1)+")'>上一页</a></li>");
				}
				
				for(int i=start;i<=end;i++){
					if(i==curPage){
						out.print("<li class='active'><a href='#'>" + i + "</a></li>");
					}else{
						out.print("<li><a href='javascript:go("+i+")'>" + i + "</a></li>");
					}
				}
				if(curPage<totalPage){
					out.print("<li><a href='javascript:go("+(curPage+1)+")'>下一页</a></li>");
					if(end<totalPage){
						out.print("<li><a href='javascript:go("+totalPage+")'>尾页</a></li>");
					}
				}
				out.print("<li><a href='javascript:void(0)'>共" + totalPage + "页" + this.totalCount + "条</a></li>");
				out.print("</ul>");
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

		return super.doStartTag();

	}
	
	public static Integer getStartIndex(Integer pageNum, Integer pageSize){
		Integer res = 0;
		if(pageNum>0){
			res = (pageNum-1)*pageSize;
		}
		return res;
	}

}

 BaseController

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;

public class BaseController {

	//初始化分页相关信息
	protected void initPage(Map<String,Object> map, Integer pageNum, Integer pageSize, Integer totalCount){
		if(null==pageSize || pageSize.equals("")){
			pageSize = FundTestConstance.pageSize;
		}
		if(pageSize>50){
			pageSize = 50;
		}
		Integer totalPage = (totalCount+pageSize-1)/pageSize;
		if(null==pageNum){
			pageNum = 1;
		}else if(pageNum>totalPage){
			pageNum = totalPage;
		}
		map.put("startIndex", Pager.getStartIndex(pageNum, pageSize));
		map.put("pageNum", pageNum);
		map.put("totalPage", totalPage);
		map.put("pageSize", pageSize);
		map.put("totalCount", totalCount);
		
	}
	
	//将相关数据放入model
	protected void initResult(Model model, List<Object> list, Map<String,Object> map){
		model.addAttribute("list", list);
		Iterator it = map.entrySet().iterator(); 
		while(it.hasNext()){ 
			Map.Entry m = (Map.Entry)it.next(); 
			model.addAttribute(m.getKey().toString(), m.getValue());
	   } 
	}
	
}

 TestController.java

@Controller
public class TestController extends BaseController{
	
	@Autowired
	private TestService testService;
	
	public TestService getTestService() {
		return testService;
	}

	public void setTestService(TestService testService) {
		this.testService = testService;
	}

	@RequestMapping("/test")
	public String test(Model model, @RequestParam(required=false) String type, @RequestParam(required=false) Integer pageNum, 
			@RequestParam(required=false) Integer pageSize) {	
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("type", type);
		Integer totalCount = this.testService.getTestsCount(map);
		
		this.initPage(map, pageNum, pageSize, totalCount);
		
		List list = this.testService.getTests(map);
		this.initResult(model, list, map);
		
		return "test";
	}
	
	@RequestMapping("/test/add")
	public String testAdd(@RequestParam String type,Model model) {
		Test test = new Test();
		test.setType(type);
		this.testService.addTest(test);
		return this.test(model,null,null,null);
	}
	
	@RequestMapping("/test/del")
	public String testDel(@RequestParam(required=true) Integer id, @RequestParam(required=false) Integer pageNum, 
			@RequestParam(required=false) Integer pageSize, Model model) {
		this.testService.delTest(id);
		return this.test(model, null, pageNum, pageSize);
	}
	
	@RequestMapping("/test/toEdit")
	public String testToEdit(@RequestParam(required=true) Integer id, Model model) {
		Test test = this.testService.getTestById(id);
		model.addAttribute("test", test);
		return "testEdit";
	}
	
	@RequestMapping("/test/edit")
	public String testedit(@RequestParam(required=true) Integer id,
						  @RequestParam String type,
					      @RequestParam(required=false) Integer pageNum, 
						  @RequestParam(required=false) Integer pageSize,
						  Model model) {
		Test test = new Test();
		test.setType(type);
		this.testService.editTest(test);
		return this.test(model, null, pageNum, pageNum);
	}
	
}

 TestMapper.xml

<select id="getTests"  resultMap="baseResultMap" parameterType="map">
		SELECT id, name, type, create_date, update_date
		FROM test
		WHERE 1=1
			<if test="type!=null and type!=''">
				AND type = #{type}
			</if>
		limit #{startIndex},#{pageSize}
</select>
	
<select id="getTestsCount" resultType="java.lang.Integer" parameterType="map">
		SELECT COUNT(1)
		FROM test
		WHERE 1=1
		<if test="type!=null and type!=''">
			AND type = #{type}
		</if>
</select>

 


http://www.niftyadmin.cn/n/4413326.html

相关文章

Qt Https通信: TLS initialization failed 解决方法

Qt Https通信&#xff1a; TLS initialization failed 解决方法&#xff0c;Window端使用Qt 做开发请求Https资源时&#xff0c;会经常遇到 TLS initialization failed。 原因分析&#xff1a; 在Qt中并未包含 SSL所包含的库&#xff0c;因此需要开发者&#xff0c;自己将库拷贝…

CXF 添加拦截器+整合Spring

1.自定义拦截器 package com.cxf.interceptor;import java.util.List; import javax.xml.soap.SOAPException; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.headers.Header; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phas…

Oracle中查看游标和缓存

1、Oracle查看当前打开的游标数目 SQL> select count(*) from v$open_cursor; COUNT(*) 17494 2、修改Oracle最大游标数 SQL> alter system set open_cursors1000 scopeboth; 系统已更改。 SQL> show parameter open_cursors; NAME TYPE VALUE open_cursors …

五种开源协议比较

本文&#xff0c;我们来看5种最常用的开源协议及它们的适用范围&#xff0c;供那些准备开源或者使用开源产品的开发人员/厂家参考。 BSD开源协议 BSD开源协议是一个给于使用者很大自由的协议。基本上使用者可以”为所欲为”&#xff0c;可以自由的使用&#xff0c;修改源代码&a…

axis2命令行生产客户端

wsdl2java 用于根据WSDL生成相应的服务端和客户端代码的生成工具。命令行格式为&#xff1a;WSDL2Java [options] -uri <url or path> : A url or path to a WSDL 例如&#xff1a; wsdl2java -uri http://localhost:8080/cxfService_0617/services/Hellows?wsdl -s -o …

axis 生成客户端client stub文件

一、Axis环境的安装 1、安装环境 J2SE SDK 1.4&#xff0c;Tomcat 5.0&#xff0c;eclipse 3.2。 2、到 http://xml.apache.org 网站下载Axis安装包。 3、将Axis相关包文件放在WEB-INF/lib目录下。 4、Axis可选的包&#xff1a;activation.jar&#xff1b; mail.jar&#xff1b…

如何让搜索引擎抓取AJAX内容

这种做法的好处是用户体验好、节省流量&#xff0c;缺点是AJAX内容无法被搜索引擎抓取。举例来说&#xff0c;你有一个网站。 http://example.com 用户通过井号结构的URL&#xff0c;看到不同的内容。 http://example.com#1http://example.com#2http://example.com#3 但是&am…

@WebService

JAX-WS 注释 “基于 XML 的 Web Service 的 Java API”&#xff08;JAX-WS&#xff09;通过使用注释来指定与 Web Service 实现相关联的元数据以及简化 Web Service 的开发。注释描述如何将服务器端的服务实现作为 Web Service 来访问或者客户端的 Java 类如何访问 Web Service…