ssm
分页
1.bean pageInfo list size currentPage totalcount totalpage
public class PageInfo<T> {
private List<T> list;
private int totalCount;
private int size;
private int totalPage;
private int currentPage;
public List<T> getList() {
return list;
}
public void setList(List<T> 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 +
'}';
}
}
2.dao findAll(start,5) count
List<User> findAll(@Param("start") int start, @Param("size")int size,@Param("username") String username);
3.service pageinfo
@Override
public PageInfo<User> 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<User> userList = userDao.findAll(start, 5,username);
pageInfo.setList(userList);
return pageInfo;
}
4.controller pageinfo对象传给前端页面
PageInfo<User> pageInfo = userService.findAll(currentPage,username);
ModelAndView mv=new ModelAndView();
mv.setViewName("user-list");
mv.addObject("pageInfo",pageInfo);
return mv;
搜索
模糊查询 like session
<select id="findAll" resultType="user">
select * from tb_user
<if test="username!=null and username !=''">
where username like concat("%",#{username},"%")
</if>
limit #{start},#{size}
</select>
filter
1.loginFilter
2.web.xml
@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.getRequestURL().toString();
if(user==null && url.indexOf("login.do")==-1){
response.sendRedirect("../login.jsp");
}else {
filterChain.doFilter(request,response);
}
}
每次能够得到对应页数所需要的5条数据,等等这些数据都要在jsp中显示出来,也就是说,每次都要从后台拿那么多数据过来进行显示,所以我们就想办法把这些数据封装在一个javabean当中,每次后台都将查询到的数据放入javabean对象中,我们只需要将该对象存入request作用域,然后在jsp页面中从域中获取需要的数据即可。