SSM初步的权限管理

关键代码

<input type="hidden" name="userId" value="${user.id}">
				
					<table id="dataList"
							class="table table-bordered table-striped table-hover dataTable">
							<thead>
								<tr>
									<th class="" style="padding-right: 0px">
									<input id="selall" 
										type="checkbox" class="icheckbox_square-blue"></th>
									<th class="sorting_asc">ID</th>
									<th class="sorting">角色名称</th>
									<th class="sorting">角色描述</th>									
								</tr>
							</thead>
							<tbody>
								<c:forEach items="${roles}" var="role">
									<tr>
										<td>
										
										<input name="roleId" type="checkbox" value="${role.id}">
										
										</td>
										<td>${role.id}</td>
										<td>${role.rolename}</td>
										<td>${role.roledesc}</td>
										
									</tr>
								</c:forEach>
							</tbody>

						</table>
@RequestMapping("toAddRole.do")
    public ModelAndView toAddRole(int id){
        List<Role> roleList = roleService.findRoleByUserId(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("roles",roleList);
        mv.setViewName("user-role-add");
        return mv;
    }
package com.zhongruan.bean;

public class Role {
    private int id;
    private String rolename;
    private String roledesc;

    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 getRoledesc() {
        return roledesc;
    }

    public void setRoledesc(String roledesc) {
        this.roledesc = roledesc;
    }

    @Override
    public String toString() {
        return "Role{" +
                "id=" + id +
                ", rolename='" + rolename + '\'' +
                ", roledesc='" + roledesc + '\'' +
                '}';
    }
}

<select id="findRoleByUserId" parameterType="int" resultType="role">
        select * from tb_role where id not in (select roleId from user_role where userId = #{id})
    </select>

就能将数据库里没有的那部分人拿出来了。

作业部分(未实现)

function addRoles() {
			var checkedNum=$("input[name='roleId']:checked").length;
			alert(checkedNum);
			if(checkedNum==0){
				alert("请至少选择一个进行添加!!!");
				return;
			}
			if(confirm("确认要添加这些用户吗?")){
				var roleList=new Array();
				var userList=new Array();
				$("input[name='roleId']:checked").each(
						function () {
							roleList.push($(this).val())
						}
				);
				$("input[name='userId']:checked").each(
						function () {
							userList.push($(this).val())
						}
				);
				alert(roleList);
				alert(userList);
				$.ajax({
					type:"post",
					url: "${pageContext.request.contextPath}/user/addRoleAll.do",
					data:{roleList:roleList.toString(),userList:userList.toString()},
					success:function () {
						alert("添加成功");
						location.reload();

					},
					error:function () {
						alert("删除失败");
					}
				});
			}





		}
 @RequestMapping("addRoleAll.do")
    public String addRoleAll(String roleList,String userList){
        String[] strs1 = roleList.split(",");
        String[] strs2 = userList.split(",");
        List<Integer> ids=new ArrayList<>();
        for(int i = 0;i<strs1.length;i++){
            userService.addroleuser(strs1[i],strs2[i]);
        }


        return "redirect:findAll.do";
    }
<insert id="add" parameterType="list">
        insert into user_role (userId,roleId) values (#{userId},#{roleId})
    </insert>

思路是靠ajax把数据拿过去装进数组里,然后逐个添加,然而根本没法运行今天晚上也有点事没法仔细研究,等明天老师解答了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值