ssm3 分页,过滤器
@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;
}
@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;
}
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 +
'}';
}
}
<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>
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 url =request.getRequestURI();
if (user==null &&url.indexOf("login.do")==-1){
response.sendRedirect(request.getContextPath()+"../login.jsp");
}else{
filterChain.doFilter(request,response);
}
}
@Override
public void destroy() {
}
}