目录
步骤
1、建立pageinfo类
2、修改userController
3、修改userService
4、修改user-list.jsp
5、getTotalCount的实现
6、拦截器
pageinfo
import java.util.List;
public class PageInfo<T> {
private List<T> list;
private int totalPage;
private int size;
private int totalCount;
private int currentPage;
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
@Override
public String toString() {
return "PageInfo{" +
"list=" + list +
", totalPage=" + totalPage +
", size=" + size +
", totalCount=" + totalCount +
", currentPage=" + currentPage +
'}';
}
}
userController
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/login.do")
public ModelAndView login(User user, HttpSession session) {
boolean flag = userService.login(user.getUsername(), user.getPassword());
ModelAndView modelAndView = new ModelAndView();
if (flag) {
session.setAttribute("user",user);
modelAndView.setViewName("main");
} else {
modelAndView.setViewName("../failer");
}
return modelAndView;
}
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1") int currentPage) {
PageInfo<User> pageInfo = userService.findAll(currentPage);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("pageInfo", pageInfo);
modelAndView.setViewName("user-list");
return modelAndView;
}
@RequestMapping("/deleteById.do")
public String delete(int id) {
userService.deleteById(id);
return "redirect:findAll.do";
}
@RequestMapping("/add.do")
public String add(User user) {
userService.add(user);
return "redirect:findAll.do";
}
@RequestMapping("/toUpdate.do")
public ModelAndView toUpdate(int id) {
User user = userService.selectUserById(id);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("user-update");
modelAndView.addObject("user", user);
return modelAndView;
}
@RequestMapping("/update.do")
public String update(User user) {
userService.update(user);
return "redirect:findAll.do";
}
}
userService
@Service
public class UserService implements IUserService {
@Autowired
private UserDao userDao;
@Override
public boolean login(String username, String password) {
User user=userDao.findUserByUserName(username);
if (user!=null && user.getPassword().equals(password)){
return true;
}
return false;
}
@Override
public PageInfo<User> findAll(int currentPage) {
PageInfo<User> pageInfo=new PageInfo<>();
pageInfo.setSize(5);
int tc=userDao.getTotalCount();
pageInfo.setTotalCount(tc);
int tp=(int)Math.ceil(tc/5.0);
pageInfo.setTotalPage(tp);
if (currentPage<1){
pageInfo.setCurrentPage(1);
}else if (currentPage>tp){
pageInfo.setCurrentPage(tp);
}else {
pageInfo.setCurrentPage(currentPage);
}
int start=(pageInfo.getCurrentPage()-1)*5;
List<User> userList=userDao.findAll(start);
pageInfo.setList(userList);
return pageInfo;
}
// @Override
// public List<User> findAll() {
// return userDao.findAll();
// }
@Override
public void deleteById(int id) {
userDao.deleteById(id);
}
@Override
public void add(User user) {
userDao.add(user);
}
@Override
public User selectUserById(int id) {
return userDao.selectById(id);
}
@Override
public void update(User user) {
userDao.update(user);
}
}
user-list.jsp
<tbody>
<c:forEach items="${pageInfo.list}" var="user">
<tr>
<td><input name="ids" type="checkbox"></td>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td class="text-center">
<a href="${pageContext.request.contextPath}/user/toUpdate.do?id=${user.id}" class="btn bg-olive btn-xs">更新</a>
<a href="${pageContext.request.contextPath}/user/deleteById.do?id=${user.id}" class="btn bg-olive btn-xs">删除</a>
<a href="#" class="btn bg-olive btn-xs">添加角色</a>
</td>
</tr>
</c:forEach>
</tbody>
<div class="box-tools pull-right">
<ul class="pagination">
<li><a href="${pageContext.request.contextPath}/user/findAll.do" aria-label="Previous">首页</a></li>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?currentPage=${pageInfo.currentPage-1}">上一页</a></li>
<c:forEach begin="1" end="${pageInfo.totalPage}" var="pageNum">
<li><a href="${pageContext.request.contextPath}/user/findAll.do?currentPage=${pageNum}">${pageNum}</a></li>
</c:forEach>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?currentPage=${pageInfo.currentPage+1}">下一页</a></li>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?currentPage=${pageInfo.totalPage}" aria-label="Next">尾页</a></li>
</ul>
</div>
getTotalCount
int getTotalCount();
拦截器
web.xml配置
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.zhongruan.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
LoginFilter
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest) servletRequest;
HttpServletResponse response= (HttpServletResponse) servletResponse;
HttpSession session=request.getSession();
User user=(User) session.getAttribute("user");
String uri=request.getRequestURI();
if (user==null && uri.indexOf("login.do")==-1){
response.sendRedirect(request.getContextPath()+"../login.jsp");
}else {
filterChain.doFilter(request,response);
}
}
@Override()
public void destroy() {
}
}
controller
@RequestMapping("/login.do")
public ModelAndView login(User user, HttpSession session) {
boolean flag = userService.login(user.getUsername(), user.getPassword());
ModelAndView modelAndView = new ModelAndView();
if (flag) {
session.setAttribute("user",user);
modelAndView.setViewName("main");
} else {
modelAndView.setViewName("../failer");
}
return modelAndView;
}
分页结果截图
分别是第一页 第二页 第三页
拦截器就不截图了···