实现SSM登录及分页
在Dao层添加登录
在Mapper中添加sql操作
Service中添加登录
在ServiceImpl中复写
在Controller层中实现登录的具体操作
@RequestMapping("/doLogin.do")
public String doLogin(String username, String password, HttpSession session){
UserInfo userInfo = userInfoService.doLogin(username);
if(userInfo!=null){
if(userInfo.getPassword().equals(password)){
System.out.println("登陆成功");
session.setAttribute("userInfo",userInfo);
return "main";
}else{
System.out.println("密码错误,请重新登陆!");
session.setAttribute("message","密码错误,请重新输入");
return "../login";
}
}else{
System.out.println("用户不存在,请重新输入!");
session.setAttribute("message","用户不存在,请重新输入");
return "../login";
}
分页
在applicationContext.xml中配置分页
修改Service和ServiceImpl中的方法
改写Controller中对应的方法
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size){
List<UserInfo> userInfoList = userInfoService.findAll(page,size);
PageInfo pageInfo = new PageInfo(userInfoList); //把结果集给pageHelper中的内置对象
ModelAndView mv = new ModelAndView();
mv.addObject("pageInfo",pageInfo);
mv.setViewName("user-list");
return mv;
}
同时修改user-list.jsp中的遍历显示
<tbody>
<c:forEach var="user" items="${pageInfo.list}">
<tr>
<td><input name="ids" type="checkbox"></td>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td class="text-center">
<a href="${pageContext.request.contextPath}/user/findUserById.do?id=${user.id}" class="btn bg-olive btn-xs">更新</a>
<a href="${pageContext.request.contextPath}/user/deleteUser.do?id=${user.id}" class="btn bg-olive btn-xs">删除</a>
<a href="${pageContext.request.contextPath}/user/toAddUser.do" class="btn bg-olive btn-xs">添加角色</a>
</td>
</tr>
</c:forEach>
</tbody>
对页面的页数显示也进行修改
<div class="box-tools pull-right">
<ul class="pagination">
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=1&size=5" aria-label="Previous">首页</a></li>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfo.pageNum-1}&size=5">上一页</a></li>
<c:forEach begin="1" end="${pageInfo.pages}" var="pagenum">
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pagenum}&size=5">${pagenum}</a></li>
</c:forEach>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfo.pageNum+1}&size=5">下一页</a></li>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfo.pages}&size=5" aria-label="Next">尾页</a></li>
</ul>
</div>