ssm使用pagehelper增加分页以及权限管理
今天的内容是在上次的基础上增加分页以及权限管理,所以上次用到的代码接着使用,这里直接给出其余的代码以及运行成功的截图
分页的代码
在pom.xml中加入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
在applicationContext.xml中添加pagehelper分页配置文件
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="resonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
修改dao层中的接口,在findAll接口方法中传递2个参数 分页和大小
public List<UserInfo> findAll(int page,int size);
在service层的实现方法中实例化pagehelper对象
public List<UserInfo> findAll(int page ,int size) {
PageHelper.startPage(page,size);
return userDao.findAll(page,size);
}
在jsp页面的跳转链接中传递page和size
<a href="${pageContext.request.contextPath}/user/findAll.do?page=1&size=5">
<i class="fa fa-circle-o"></i> 用户管理
</a>
controller.java
@RequestMapping("/findAll.do")
public ModelAndView findall(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5")int size){
ModelAndView mv =new ModelAndView();
List<UserInfo> list=userService.findAll(page,size);
PageInfo pageInfos=new PageInfo(list);
mv.addObject("pageInfos",pageInfos);
mv.setViewName("user-list");
return mv;
}
具体分页代码
<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=${pageInfos.pageNum-1}&size=5">上一页</a></li>
<c:forEach begin="1" end="${pageInfos.pages}" var="pageNumber">
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageNumber}&size=5">${pageNumber}</a></li>
</c:forEach>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfos.pageNum+1}&size=5">下一页</a></li>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfos.pages}&size=5" aria-label="Next">尾页</a></li>
</ul>
权限管理
新建两个表存储信息,角色信息以及角色的状态
之后再bean层加role类
package com.zhongruan.bean;
public class Role{
private int id;
private String roleName;
private String roleDeac;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getRoleDeac() {
return roleDeac;
}
public void setRoleDeac(String roleDeac) {
this.roleDeac = roleDeac;
}
@Override
public String toString() {
return "Role{" +
"id=" + id +
", roleName='" + roleName + '\'' +
", roleDeac='" + roleDeac + '\'' +
'}';
}
}
dao层加接口iuserdao
package com.zhongruan.dao;
import com.zhongruan.bean.Role;
import java.util.List;
public interface IRoleDao {
public List<Role> findRolesByUserId(int userId);
}
mapper里加rolemapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zhongruan.dao.IRoleDao" >
<select id="findRolesByUserId" parameterType="int" resultType="com.zhongruan.bean.Role">
select * from role where id in (select roleId from users_role where userId=#{userId})
</select>
</mapper>
aside.jsp管理旁边的状态栏
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
<aside class="main-sidebar">
<!-- sidebar: style can be found in sidebar.less -->
<section class="sidebar">
<!-- Sidebar user panel -->
<div class="user-panel">
<div class="pull-left image">
<img src="${pageContext.request.contextPath}/img/user2-160x160.jpg"
class="img-circle" alt="User Image">
</div>
<div class="pull-left info">
<p>
<security:authentication property="principal.username"></security:authentication>
</p>
<a href="#"><i class="fa fa-circle text-success"></i> 在线</a>
</div>
</div>
<!-- sidebar menu: : style can be found in sidebar.less -->
<ul class="sidebar-menu">
<li class="header">菜单</li>
<li id="admin-index"><a
href="#"><i
class="fa fa-dashboard"></i> <span>首页</span></a></li>
<li class="treeview"><a href="#"> <i class="fa fa-cogs"></i>
<span>系统管理</span> <span class="pull-right-container"> <i
class="fa fa-angle-left pull-right"></i>
</span>
</a>
<ul class="treeview-menu">
<li id="system-setting">
<security:authorize access="hasRole('ADMIN')">
<a
href="${pageContext.request.contextPath}/user/findAll.do?page=1&size=5"><i
class="fa fa-circle-o"></i> 用户管理
</a>
</security:authorize>
</li>
<li id="system-setting"><a
href="#"> <i
class="fa fa-circle-o"></i> 角色管理
</a></li>
</ul>
</li>
</ul>
</section>
<!-- /.sidebar -->
</aside>
header.jsp管理头上的状态
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
<!-- 页面头部 -->
<header class="main-header">
<!-- Logo -->
<a href="all-admin-index.html" class="logo"> <!-- mini logo for sidebar mini 50x50 pixels -->
<span class="logo-mini"><b>数据</b></span> <!-- logo for regular state and mobile devices -->
<span class="logo-lg">中软<b>ETC</b>用户管理系统</span>
</a>
<!-- Header Navbar: style can be found in header.less -->
<nav class="navbar navbar-static-top">
<!-- Sidebar toggle button-->
<a href="#" class="sidebar-toggle" data-toggle="offcanvas"
role="button"> <span class="sr-only">Toggle navigation</span>
</a>
<div class="navbar-custom-menu">
<ul class="nav navbar-nav">
<li class="dropdown user user-menu"><a href="#"
class="dropdown-toggle" data-toggle="dropdown"> <img
src="${pageContext.request.contextPath}/img/user2-160x160.jpg"
class="user-image" alt="User Image"> <span class="hidden-xs">
<security:authentication property="principal.username"></security:authentication>
</span>
</a>
<ul class="dropdown-menu">
<!-- User image -->
<li class="user-header"><img
src="${pageContext.request.contextPath}/img/user2-160x160.jpg"
class="img-circle" alt="User Image"></li>
<!-- Menu Footer-->
<li class="user-footer">
<div class="pull-left">
<a href="#" class="btn btn-default btn-flat">修改密码</a>
</div>
<div class="pull-right">
<a href="/login.jsp"
class="btn btn-default btn-flat">注销</a>
</div>
</li>
</ul></li>
</ul>
</div>
</nav>
</header>
<!-- 页面头部 /-->
运行结果
管理员登录
可以显示状态以及用户角色的管理
用户登录
只能管理自己的角色
以上就是分页+权限的所有内容,有问题可以问一下,也可以百度自己解决