pageInfo
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 + '}'; } }findAll()
@RequestMapping("/findAll.do") public ModelAndView findAll(@RequestParam(defaultValue = "1") int currentPage,String username,HttpSession session,@RequestParam(defaultValue = "0") int type){ if(type==1){ session.setAttribute("searchname",username); }else{ username=(String) session.getAttribute("searchname"); } PageInfo<User> pageInfo=userService.findAll(currentPage,username); ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("pageInfo",pageInfo); modelAndView.setViewName("user-list"); return modelAndView; }userlist.jsp
<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> </div>UserMapper.xml
<select id="findAll" resultType="com.zhongruan.bean.User"> select *from tb_user <if test="username!=null and username!=''"> WHERE username LIKE concat("%",#{username},"%") </if> limit #{start},5 </select>创建过滤器类LoginFilter,实现Filter接口,并且重写init(),doFilter(),destroy()方法,此处因为是简单使用,只需要在doFilter方法中写入拦截逻辑。 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() { } }