`

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

 
阅读更多

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>

 

分享到:
评论
1 楼 北雁南飞33 2016-04-05  
有源码分享吗

相关推荐

Global site tag (gtag.js) - Google Analytics