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

最终效果如图:


pager.tld



	Pager
	1.0
	page
	
	
		createPager
		com.test.utils.Pager
		JSP
		
			curPage
			true
			true
			java.lang.Integer
		
		
			totalPage
			true
			true
			java.lang.Integer
		
		
			pageSize
			true
			true
			java.lang.Integer
		
		
			totalCount
			true
			true
			java.lang.Integer
		
		
			formId
			true
			true
			java.lang.String
		
	


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("");
				
				out.print("

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 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 list, Map 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());
	   } 
	}
	
} 
  


test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="page" uri="/WEB-INF/pager.tld"%>
<% String path = request.getContextPath(); %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>



测试分页标签
	
	
	


	
	

${item.name}

${item.content}

2013-06-22 22:37   删除   编辑  


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 map = new HashMap();
		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


	




你可能感兴趣的:(SSH_SSM)