附源码下载(底)。实现了登录,查询分页,文件上传等基本功能
page.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>测试</title>
</head>
<table>
<c:forEach items="${page.data}" var="us" varStatus="status">
<tr>
<td><input type="checkbox" name="id" value="${user.id}" /></td>
<td>${us.id}</td>
<td>${us.username}</td>
<td>${us.password}</td>
<td>${us.age}</td>
</tr>
</c:forEach>
</table>
<div>
共<span >${page.count}</span>个记录
当前第<span >${page.pageNow}</span>页
<a href="${page.firtpage}" >首页</a>
<a href="${page.prevpage}" >上一页</a>
<a href="${page.nextpage}">下一页</a>
<a href="${page.lastpage}" >尾页</a>
</div>
<body>
</body>
</html>
控制器PageController.java。测试可以把user对象数据写在内存中。不用service
package com.cn.uuu.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.cn.uuu.pojo.Pages;
import com.cn.uuu.pojo.User;
import com.cn.uuu.service.UserService;
/**
* @author chuck
*
*/
@Controller
@RequestMapping("/page")
public class PageController {
@Resource(name="userServiceImpl")
private UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
@RequestMapping(value="/listpage.do", method = RequestMethod.GET)
public String toIndex(HttpServletRequest request,Model model,Pages page){
String queryString = request.getQueryString();
System.out.println(queryString);
List data=userService.selectAll();
int count=userService.selectCount();
//(pageNow - 1) * pageSize------------pageNow * pageSize 分页的始末
page.setCount(count);
page.setPageSize(3);
int index1=page.pageNow*3-3;//6
int index2=index1+3;//9
if (index2>count) {
index2=count;
}
if (index1<0) {
index1=0;
}
if (index1>=0 && index1< count ) {
page.setData(data.subList(index1, index2));
}
// page.setData(data.subList(5, 6));
page.setNextpage("http://localhost:8080/page/listpage.do?pageNow="+(page.getPageNow()+1));
page.setPrevpage("http://localhost:8080/page/listpage.do?pageNow="+(page.getPageNow()-1));
System.out.println(page.getData().get(0).toString());
model.addAttribute("page", page);
return "page";
}
}
model
package com.cn.uuu.pojo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Pages {
public static String ListPAGE = "listpage";
public int pageNow = 1;
protected int pageSize = 10;
private int step = 5;
protected Integer count= 0;//总共多少条数据。
protected List data = new ArrayList();//存放数据。
protected int pageCount;//总页数
private int maxPage = 100;
protected String firtpage="";//首页
protected String lastpage="";//尾页
protected String prevpage="";//上一页
protected String nextpage="";//下一页
protected String topage;//跳转
protected List<Map<String, Object>> listPages = new ArrayList<Map<String, Object>>();
public static String getListPAGE() {
return ListPAGE;
}
public static void setListPAGE(String listPAGE) {
ListPAGE = listPAGE;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getStep() {
return step;
}
public void setStep(int step) {
this.step = step;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public List getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getMaxPage() {
return maxPage;
}
public void setMaxPage(int maxPage) {
this.maxPage = maxPage;
}
public String getFirtpage() {
return firtpage;
}
public void setFirtpage(String firtpage) {
this.firtpage = firtpage;
}
public String getLastpage() {
return lastpage;
}
public void setLastpage(String lastpage) {
this.lastpage = lastpage;
}
public String getPrevpage() {
return prevpage;
}
public void setPrevpage(String prevpage) {
this.prevpage = prevpage;
}
public String getNextpage() {
return nextpage;
}
public void setNextpage(String nextpage) {
this.nextpage = nextpage;
}
public String getTopage() {
return topage;
}
public void setTopage(String topage) {
this.topage = topage;
}
public List<Map<String, Object>> getListPages() {
return listPages;
}
public void setListPages(List<Map<String, Object>> listPages) {
this.listPages = listPages;
}
}
源码在gitHub上免费下载,clone
https://github.com/chenyyyang/spring-SpringMVC-Mybtis-maven