分页&搜索
PageInfo:实体类,存储分页的相关信息
public class PageInfo<T> {
private List<T> list;//当前页展示对象列表
private int totalCount;//总对象条数
private int size;//每页展示对象数,默认为5
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 +
'}';
}
}
1、在IUserDao中,对于IUserDao接口,改写findAll方法的声明、写getTotalCount方法的声明
List<User> findAll(@Param("start") int start, @Param("size")int size,@Param("username") String username);
int getTotalCount(@Param("username") String username);
2、在UserMapper中,实现Dao接口,也就是对数据库进行操作
</select>
<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>
<select id="getTotalCount" resultType="int">
select count(*) from tb_user
<if test="username!=null and username !=''">
where username like concat("%",#{username},"%")
</if>
</select>
3、在IUserService中,对于IUseService接口,改写findAll方法的声明
PageInfo<User> findAll(int currentPage, String username);
4、在UserServiceImpl中实现IUserService中声明的接口
分页& 搜索:根据输入的当前页数、用户名,得到当前页展示对象列表:
(1) 调用Dao的getTotalCoun方法,得到总对象条数;
(2)总对象条数除以5再取整,得到总页数;
(3)每页展示对象数设置为5;
(4)如果输入的当前页数小于1,当前页数设置为1;
如果输入的当前页数大于总页数,当前页数设置为总页数;
其他情况,当前页数设置为输入的当前页数;
(5)当前页数减一再乘以5,得到当前展示的第一个对象(当前展示对象列表第一行)的id;
(6)根据当前展示的第一个对象id、每页展示对象数、输入的用户名,调用Dao的findAll方法,进行模糊查询,得到当前页展示对象列表
@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;
}
5、在UserController中改写findAll方法:
type=1,根据输入的待搜索用户名,设置session的searchname属性值,进行模糊查询;
(搜索界面处)
type=0,根据session的searchname属性,给username赋值,查询用户,
如果username为空,查询所有用户;
否则,进行模糊查询;
(分页界面处)
type=2,移除session的searchname属性,查询所有用户
(点击用户管理)
@Controller
@RequestMapping("user")
@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<User> pageInfo = userService.findAll(currentPage,username);
ModelAndView mv=new ModelAndView();
mv.setViewName("user-list");
mv.addObject("pageInfo",pageInfo);
return mv;
}
user-list.jsp:
改写分页界面
<div class="box-tools pull-right">
<ul class="pagination">
<li><a href="/user/findAll.do" aria-label="Previous">首页</a></li>
<li><a href="/user/findAll.do?currentPage=${pageInfo.currentPage-1}">上一页</a></li>
<c:forEach begin="1" end="${pageInfo.totalPage}" var="pageNum">
<li><a href="/user/findAll.do?currentPage=${pageNum}">${pageNum}</a></li>
</c:forEach>
<li><a href="/user/findAll.do?currentPage=${pageInfo.currentPage+1}">下一页</a></li>
<li><a href="/user/findAll.do?currentPage=${pageInfo.totalPage}" aria-label="Next">尾页</a></li>
</ul>
</div>
改写搜索界面
<form action="${pageContext.request.contextPath}/user/findAll.do?type=1"
method="post">
<div class="col-md-4 data1">
<input type="text" class="form-control" name="username"
placeholder="username" value="">
</div>
<button type="submit" class="btn bg-maroon">搜索</button>
</form>
aside.jsp:
点击用户管理
<li id="system-setting">
<a
href="/user/findAll.do?type=2"> <i
class="fa fa-circle-o"></i> 用户管理
</a>
</li>
过滤器
LoginFilte:防止用户未经登录就进入系统
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.getRequestURL().toString();
if(user==null && url.indexOf("login.do")==-1){
response.sendRedirect("../login.jsp");
}else {
filterChain.doFilter(request,response);
}
}
@Override
public void destroy() {
}
}
web.xml:配置
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.summer.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
分页——点击首页/点击page1/点击用户管理/点击上一页若干次
分页——点击尾页/点击page4/点击下一页若干次
搜索
过滤器——不登录,直接输入:http://localhost:8086/user/findAll.do,自动跳转到登录界面