中软国际实训日记第四天-7.23

目录

1.分页

2.模糊查询

3.过滤器

正文

1.分页

1.1 bean

package com.zr.bean;

import java.util.List;

public class PageInfo {
    private List list;
    private int totalCount;
    private int size;
    private int totalPage;
    private int currentPage;

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    @Override
    public String toString() {
        return "PageInfo{" +
                "list=" + list +
                ", totalCount=" + totalCount +
                ", size=" + size +
                ", totalPage=" + totalPage +
                ", currentPage=" + currentPage +
                '}';
    }

}

添加pageInfo类记录分页的信息。
1.2 dao
findAll方法以及sql语句的实现

List findAll(@Param("start") int start,@Param("size") int size,@Param("username") String username);

getTotalCount方法以及sql语句实现

 int getTotalCount(@Param("username") String username);

1.3 service
服务层里的findAll方法实现

 @Override
    public PageInfo findAll(int currentPage,String username)
    {
        PageInfo pageInfo=new PageInfo();
        int totalCount =userDao.getTotalCount(username);
        pageInfo.setTotalCount(totalCount);
        double d=totalCount/5.0;
        int tp=(int)Math.ceil(d);
        pageInfo.setTotalPage(tp);
        pageInfo.setSize(5);
        if(currentPage<1){
            pageInfo.setCurrentPage(1);
        }else if(currentPage>tp){
            pageInfo.setCurrentPage(tp);
        }else{
            pageInfo.setCurrentPage(currentPage);
        }
        int start=(pageInfo.getCurrentPage()-1)*5;
        List userList=userDao.findAll(start,5,username);
        pageInfo.setList(userList);
        return pageInfo;
    }

1.4.controller pageinfo对象传给前端页面

 @RequestMapping("findAll.do")
    public ModelAndView findAll(@RequestParam(defaultValue = "1")int currentPage, String username,
                                @RequestParam(defaultValue = "0")int type,
                                HttpSession session){
        if(type==1){
            session.setAttribute("searchname",username);
        }else if(type==0){
            username=(String) session.getAttribute("searchname");
        }else if(type==2)
        {
            session.removeAttribute("searchname");
        }
        PageInfo pageInfo=userService.findAll(currentPage,username);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("user-list");
        mv.addObject("pageInfo",pageInfo);
        return  mv;
    }

实现效果
中软国际实训日记第四天-7.23_第1张图片
中软国际实训日记第四天-7.23_第2张图片
中软国际实训日记第四天-7.23_第3张图片

2.模糊查询

根据用户名搜索时采用模糊查询,把符合要求的用户搜索出来。

 
 

中软国际实训日记第四天-7.23_第4张图片
中软国际实训日记第四天-7.23_第5张图片

3.过滤器

为了避免在登录界面输入url:http://localhost:8080/user/findAll.do直接进入管理页面。
所以添加过滤器。
login.do

@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;
    }

doFilter()

 @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 url=request.getRequestURI().toString();
        if(user==null&&url.indexOf("login.do")==-1){
            response.sendRedirect("../login.jsp");
        }else{
            filterChain.doFilter(request,response);
        }

    }

在web.xml文件中加入


    loginFilter
    com.zr.filter.LoginFilter
  
  
    loginFilter
    *.do
  

这样就避免了输入url直接进入管理界面了,还是停留在登录界面。
中软国际实训日记第四天-7.23_第6张图片

你可能感兴趣的:(实习日记)