7.2过滤器filter

    技术2024-11-14  21

    分页 a、pageinfo list size totalcount totalpage currentPage b、dao findAll(start) get totalcount c、service new pageinfo 对象 给pageinfo赋值 d、controller 在modelandview里面添加pageinfo对象 e、页面 在页面展示数据,在页码拼接当前页

    过滤器filter a、新建loginfilter implements Filter 在filter里面判断session里面是否有user,如果没有user并且当前的请求不是login.do,跳转到登录页面,否则继续执行 b、到Controller里面的login方法,登陆成功后把用户信息放到session里面 PageInfo

    package com.zhongruan.bean; 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 + '}'; } }

    LoginFilter

    package com.zhongruan.filter; import com.zhongruan.bean.User; import javax.servlet.*; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; 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() { } }

    UserServic

    @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); } //0 5 10 int start=(pageInfo.getCurrentPage()-1)*5; List<User> userList=userDao.findAll(start); pageInfo.setList(userList); return pageInfo; }

    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>

    UserController

    @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; }
    Processed: 0.012, SQL: 9