Java SSM 项目实战 day07用户角色关联操作,添加角色,以及添加权限

href=“${pageContext.request.contextPath}/plugins/ionslider/ion.rangeSlider.skinNice.css”>

href=“${pageContext.request.contextPath}/plugins/bootstrap-slider/slider.css”>

href=“${pageContext.request.contextPath}/plugins/bootstrap-datetimepicker/bootstrap-datetimepicker.css”>

<jsp:include page=“header.jsp”></jsp:include>

<jsp:include page=“aside.jsp”></jsp:include>

用户管理 添加角色表单

  • class=“fa fa-dashboard”> 首页

  • href=“${pageContext.request.contextPath}/user/findAll.do”>用户管理

  • 添加角色表单
  • action=“${pageContext.request.contextPath}/user/addRoleToUser.do”

    method=“post”>

    class=“table table-bordered table-striped table-hover dataTable”>

    <input id=“selall”

    type=“checkbox” class=“icheckbox_square-blue”>

    ID 角色名称 角色描述

    <c:forEach items=“${roleList}” var=“role”>

    ${role.id} ${role.roleName } ${role.roleDesc}

    </c:forEach>

    保存

    <button type=“button” class=“btn bg-default”

    οnclick=“history.back(-1);”>返回

    Version 1.0.8

    Copyright © 2014-2017 <a

    href=“http://www.itcast.cn”>研究院研发部.

    All rights reserved.

    5、勾选并保存

    在这里插入图片描述

    (1)在UserController当中创建对应addRoleToUser方法

    在这里插入图片描述

    //给用户添加角色

    @RequestMapping(“/addRoleToUser”)

    public void addRoleToUser(@RequestParam(name = “userId”, required = true) String userId,

    @RequestParam(name = “ids”, required = true) String[] roleIds) {

    userService.addRoleToUser(userId,roleIds);

    }

    在这里插入图片描述

    (2)在IUserService当中以及对应的实现类当中创建对应的方法

    在这里插入图片描述

    void addRoleToUser(String userId, String[] roleIds);

    在UserServiceImpl当中

    在这里插入图片描述

    @Override

    public void addRoleToUser(String userId, String[] roleIds) {

    for(String roleId:roleIds){

    userDao.addRoleToUser(userId,roleId);

    }

    }

    (3)在IUserDao当中

    在这里插入图片描述

    @Insert(“insert into users_role(userId,roleId) values (#{userId},#{roleId})”)

    void addRoleToUser(String userId, String roleId);

    (4)在UserController当中addRoleToUser方法

    在这里插入图片描述

    //给用户添加角色

    @RequestMapping(“/addRoleToUser”)

    public String addRoleToUser(@RequestParam(name = “userId”, required = true) String userId,

    @RequestParam(name = “ids”, required = true) String[] roleIds) {

    userService.addRoleToUser(userId,roleIds);

    return “redirect:findAll.do”;

    }

    (5)运行测试

    重新运行后访问:http://localhost:8888/zheng_ssm_web

    在这里插入图片描述

    在这里插入图片描述

    发现报错证明dao出错了

    在这里插入图片描述

    (6)修改IUserDao当中的addRoleToUser方法

    在这里插入图片描述

    @Insert(“insert into users_role(userId,roleId) values (#{userId},#{roleId})”)

    void addRoleToUser(@Param(“userId”)String userId,@Param(“roleId”)String roleId);

    (7)清理工程并重新运行

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    二、添加权限


    1、修改页面
    (1)修改role-list.jsp

    在这里插入图片描述

    详情

    添加权限

    2、修改RoleController完善对应的方法以及其service和dao
    (1)RoleController

    在这里插入图片描述

    //根据roleId查询role,并查询出可以添加的权限信息

    @RequestMapping(“/findRoleByIdAndAllPermission.do”)

    public void findRoleByIdAndAllPermission(@RequestParam(name = “id”,required = true) String roleId) throws Exception {

    //根据roleId查询role

    Role roel = roleService.findById(roleId);

    //根据roleId查询可以添加的权限

    List otherPermission = roleService.findOtherPermission(roleId);

    }

    创建对应的方法在service当中

    在这里插入图片描述

    在这里插入图片描述

    (2)RoleService

    在这里插入图片描述

    Role findById(String roleId) throws Exception ;

    List findOtherPermission(String roleId) throws Exception ;

    (3)RoleServiceImpl当中

    在这里插入图片描述

    @Override

    public Role findById(String roleId) throws Exception {

    return roleDao.findById(roleId);

    }

    在这里插入图片描述

    (4)对应的IRoleDao

    在这里插入图片描述

    @Select(“select * from role where id = #{roleId}”)

    Role findById(String roleId);

    在这里插入图片描述

    在这里插入图片描述

    @Select(“select * from permission where id not in (select permissionId from role_permission where roleId = #{roleId})”)

    List findOtherPermission(String roleId);

    (5)完善RoleController当中的findRoleByIdAndAllPermission方法

    在这里插入图片描述

    //根据roleId查询role,并查询出可以添加的权限信息

    @RequestMapping(“/findRoleByIdAndAllPermission.do”)

    public ModelAndView findRoleByIdAndAllPermission(@RequestParam(name = “id”,required = true) String roleId) throws Exception {

    ModelAndView mv = new ModelAndView();

    //根据roleId查询role

    Role role = roleService.findById(roleId);

    //根据roleId查询可以添加的权限

    List otherPermission = roleService.findOtherPermission(roleId);

    mv.addObject(“role”,role);

    mv.addObject(“permissionList”,otherPermission);

    mv.setViewName(“role-permission-add”);

    return mv;

    }

    3、创建role-permission-add.jsp

    在这里插入图片描述

    <%@ page language=“java” contentType=“text/html; charset=UTF-8”

    pageEncoding=“UTF-8”%>

    <%@taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core”%>

    数据 - AdminLTE2定制版

    <meta

    content=“width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no”

    name=“viewport”>

    href=“${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css”>

    href=“${pageContext.request.contextPath}/plugins/font-awesome/css/font-awesome.min.css”>

    href=“${pageContext.request.contextPath}/plugins/ionicons/css/ionicons.min.css”>

    href=“${pageContext.request.contextPath}/plugins/iCheck/square/blue.css”>

    href=“${pageContext.request.contextPath}/plugins/morris/morris.css”>

    href=“${pageContext.request.contextPath}/plugins/jvectormap/jquery-jvectormap-1.2.2.css”>

    href=“${pageContext.request.contextPath}/plugins/datepicker/datepicker3.css”>

    href=“${pageContext.request.contextPath}/plugins/daterangepicker/daterangepicker.css”>

    href=“${pageContext.request.contextPath}/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css”>

    href=“${pageContext.request.contextPath}/plugins/datatables/dataTables.bootstrap.css”>

    href=“${pageContext.request.contextPath}/plugins/treeTable/jquery.treetable.css”>

    href=“${pageContext.request.contextPath}/plugins/treeTable/jquery.treetable.theme.default.css”>

    href=“${pageContext.request.contextPath}/plugins/select2/select2.css”>

    href=“${pageContext.request.contextPath}/plugins/colorpicker/bootstrap-colorpicker.min.css”>

    href=“${pageContext.request.contextPath}/plugins/bootstrap-markdown/css/bootstrap-markdown.min.css”>

    href=“${pageContext.request.contextPath}/plugins/adminLTE/css/AdminLTE.css”>

    href=“${pageContext.request.contextPath}/plugins/adminLTE/css/skins/_all-skins.min.css”>

    href=“${pageContext.request.contextPath}/css/style.css”>

    href=“${pageContext.request.contextPath}/plugins/ionslider/ion.rangeSlider.css”>

    href=“${pageContext.request.contextPath}/plugins/ionslider/ion.rangeSlider.skinNice.css”>

    href=“${pageContext.request.contextPath}/plugins/bootstrap-slider/slider.css”>

    href=“${pageContext.request.contextPath}/plugins/bootstrap-datetimepicker/bootstrap-datetimepicker.css”>

    <jsp:include page=“header.jsp”></jsp:include>

    <jsp:include page=“aside.jsp”></jsp:include>

    角色管理 添加权限表单

  • class=“fa fa-dashboard”> 首页

  • href=“${pageContext.request.contextPath}/role/findAll.do”>角色管理

  • 添加权限表单
  • action=“${pageContext.request.contextPath}/user/addPermissionToRole.do”

    method=“post”>

    class=“table table-bordered table-striped table-hover dataTable”>

    ID 权限名称 权限URL

    <c:forEach items=“${permissionList}” var=“permission”>

    ${permission.id} ${permission.permissionName } ${permission.url}

    </c:forEach>

    保存

    <button type=“button” class=“btn bg-default”

    οnclick=“history.back(-1);”>返回

    Version 1.0.8

    Copyright © 2014-2017 <a

    href=“http://www.itcast.cn”>研究院研发部.

    All rights reserved.

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值