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;
}
根据用户名搜索时采用模糊查询,把符合要求的用户搜索出来。
为了避免在登录界面输入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