pager.tld
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">
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("
"function go(pageNum){" +
"var f = document.getElementById('" + formId + "');"+
"f.action = f.action + '?pageNum=' + pageNum + '&pageSize="+pageSize+"';"+
"f.submit();"+
"}" +
"");
out.print("
- ");
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("
首页");}
out.print("
上一页");}
for(int i=start;i<=end;i++){
if(i==curPage){
out.print("
" + i + "");}else{
out.print("
" + i + "");}
}
if(curPage
out.print("
下一页");if(end
out.print("
尾页");}
}
out.print("
共" + totalPage + "页" + this.totalCount + "条");out.print("
");}
} 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 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());
}
}
}
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
SELECT id, name, type, create_date, update_date
FROM test
WHERE 1=1
AND type = #{type}
limit #{startIndex},#{pageSize}
SELECT COUNT(1)
FROM test
WHERE 1=1
AND type = #{type}