saas-export项目-用户角色页面显示

在这里插入图片描述

进入用户角色页面

user-list.jsp

location.href="${path}/system/user/toUserRole.do?userId="+id;

UserController

//location.href="${path}/system/user/toUserRole.do?userId="+id;
    @RequestMapping(path = "/toUserRole",method = {RequestMethod.GET,RequestMethod.POST})
    public String toUserRole(String userId){//接收页面传过来的userId
        l.info("toUserRole userId="+userId);
        //使用userId查找用户对象
        User user = service.findUserById(userId);
        //转发给页面
        request.setAttribute("user",user);
        return "system/user/user-role";
    }

在这里插入图片描述

查找用户角色的业务

编写sql完成查询用户角色列表

select * from pe_role_user
# 查找所有的角色
select * from pe_role
# 查找老王的所有的角色 
select * from pe_role_user ru inner join pe_role r
on ru.role_id=r.role_id
where ru.user_id='002108e2-9a10-4510-9683-8d8fd1d374ef'

TestUserService

@Autowired
    IRoleService iRoleService;

    @Test
    public void test06() {
        String userId="002108e2-9a10-4510-9683-8d8fd1d374ef";
        String companyId="1";
        //查询所有角色
        List<Role> roleList=iRoleService.findAllRole(companyId);
        //查询老王的所有角色
        List<Role> userRoleList=iRoleService.findRolesByUserId(userId);
        l.info("test05 roleList="+roleList);
        l.info("test05 userRoleList="+userRoleList);
    }

IRoleService

List<Role> findAllRole(String companyId);

List<Role> findRolesByUserId(String userId);

RoleServiceImpl

@Override
    public List<Role> findAllRole(String companyId) {
        return dao.findAll(companyId);
    }

    @Override
    public List<Role> findRolesByUserId(String userId) {
        return dao.findByUserId(userId);
    }

IRoleDao

List<Role> findAll(String companyId);

List<Role> findByUserId(String userId);

IRoleDao.xml

<select id="findAll" parameterType="string" resultMap="roleMap">
        select * from pe_role
    </select>

    <select id="findByUserId" parameterType="string" resultMap="roleMap">
        select * from pe_role_user ru inner join pe_role r
        on ru.role_id=r.role_id
        where ru.user_id=#{userId}
    </select>

UserController

//location.href="${path}/system/user/toUserRole.do?userId="+id;
    @RequestMapping(path = "/toUserRole",method = {RequestMethod.GET,RequestMethod.POST})
    public String toUserRole(String userId){//接收页面传过来的userId
        l.info("toUserRole userId="+userId);
        //使用userId查找用户对象
        User user = service.findUserById(userId);
        //转发给页面
        request.setAttribute("user",user);

        //所有角色的数据
        String companyId=getLoginCompanyId();
        List<Role> roleList=iRoleService.findAllRole(companyId);
        //每个用户的角色数据
        List<Role> userRoleList = iRoleService.findRolesByUserId(userId);
        l.info("toUserRole roleList="+roleList);
        l.info("toUserRole userRoleList="+userRoleList);

        for(Role role: roleList){
            //当前公司的所有的角色
            if(isInUserRoleList(role,userRoleList)){
                role.setChecked(true);
            }
        }

        request.setAttribute("roleList",roleList);
        request.setAttribute("userRoleList",userRoleList);

        return "system/user/user-role";
    }
    
    //当前的复选框 要不要打勾 取决于是否在 用户的角色列表中
    private boolean isInUserRoleList(Role role, List<Role> userRoleList) {
        for( Role r:userRoleList){
            if(r.getRoleId().equals(role.getRoleId())){
                return true;
            }
        }//end for
        return false;
    }

Role增加checked变量

private boolean checked;//在角色列表中打上勾

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

user-role.jsp

<c:forEach items="${roleList}" var="role" varStatus="vs">
	<span style="padding:3px;margin-right:30px;width: 160px;display: inline-block">
		<input type="checkbox" name="roleIds" value="${role.roleId}"
			<%--根据role对象中的checked属性进行判断,如果为true,则打勾,否不打勾--%>
			<c:if test="${role.checked}">
				checked
			</c:if>
			<%--<c:if test="${fn:contains(userRoleList,role.roleId)}">checked</c:if>--%>
		/>
		${role.name}
	</span>
</c:forEach>

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值