进入用户角色页面
(1)先查看页面
UserController
@RequestMapping ( path = "/toUserRole" , method = { RequestMethod. GET, RequestMethod. POST} )
public String toUserRole ( String userId) {
l. info ( "toUserRole userId=" + userId) ;
User user = iUserService. findUserById ( userId) ;
request. setAttribute ( "user" , user) ;
return "system/user/user-role" ;
}
查找用户角色的业务
编写sql完成查询用户角色列表
select * from pe_role order by order_no asc ;
select *
from pe_role_user ru, pe_role r
where ru. role_id= r. role_id
and ru. user_id = '002108e2-9a10-4510-9683-8d8fd1d374ef'
UserController
@Autowired
IRoleService iRoleService;
@RequestMapping ( path = "/toUserRole" , method = { RequestMethod. GET, RequestMethod. POST} )
public String toUserRole ( String userId) {
l. info ( "toUserRole userId=" + userId) ;
User user = iUserService. findUserById ( userId) ;
request. setAttribute ( "user" , user) ;
String companyId= getLoginCompanyId ( ) ;
List< Role> roleList = iRoleService. findAll ( 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 ;
}
}
return false ;
}
IRoleService ,RoleSeviceImpl
List< Role> findAll ( String companyId) ;
List< Role> findRolesByUserId ( String userId) ;
@Override
public List< Role> findAll ( String companyId) {
return iRoleDao. findAll ( companyId) ;
}
@Override
public List< Role> findRolesByUserId ( String userId) {
return iRoleDao. findByUserId ( userId) ;
}
IRoleDao,IRoleDao.xml
List< Role> findByUserId ( String userId) ;
< 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>
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>
/>
${role.name}
</ span>
</ c: forEach>