SSH项目--国税(三)

6角色管理
6.1角色与权限说明
6.1.1角色与权限的关系
系统中可以存在多个角色,每个角色可以自由的组合系统定义的权限集合。即角色和权限的关系是多对多的关系。为了保存这种多对多关系,需要一个角色权限表来保存。角色与角色权限的关系是一对多的关系;而权限与角色权限的关系也为一对多关系。
6.1.2定义系统权限集
将系统中需要使用到的权限先定义出来:粗粒度的分为各个子系统的访问权限;这些权限可以定义在常量文件中。
public class Constant {
public static String PRIVILEGE_XZGL ="xzgl"; 
public static String PRIVILEGE_HQFW ="hqfw"; 
public static String PRIVILEGE_ZXXX ="zxxx"; 
public static String PRIVILEGE_NSFW ="nsfw"; 
public static String PRIVILEGE_SPACE ="spaces"; 


public static Map<String, String> PRIVILEGE_MAP;
static {
PRIVILEGE_MAP = new HashMap<String, String>();
PRIVILEGE_MAP.put(PRIVILEGE_XZGL, "行政管理");
PRIVILEGE_MAP.put(PRIVILEGE_HQFW, "后勤服务");
PRIVILEGE_MAP.put(PRIVILEGE_ZXXX, "在线学习");
PRIVILEGE_MAP.put(PRIVILEGE_NSFW, "纳税服务");
PRIVILEGE_MAP.put(PRIVILEGE_SPACE, "我的空间");
}
}


6.2角色管理CRUD
6.2.1实体类及映射文件
①Role/Role.hbml.xml
<hibernate-mapping>
<class name="cn.itcast.nsfw.role.entity.Role"table="role">
<id name="roleId" type="java.lang.String">
<column name="role_id" length="32" />
<generator class="uuid.hex" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" not-null="true" />
</property>
<property name="state" type="java.lang.String">
<column name="state" length="1" />
</property>
<set name="rolePrivileges" inverse="true" lazy="false"cascade="save-update,delete">
<key>
<column name="role_id"></column>
</key>
<one-to-manyclass="cn.itcast.nsfw.role.entity.RolePrivilege"/>
</set>
</class>
</hibernate-mapping>


②RolePrivilege/RolePrivilegeId,RolePrivilege.hbm.xml

<hibernate-mapping>
<class name="cn.itcast.nsfw.role.entity.RolePrivilege"table="role_privilege">
<composite-id name="id"class="cn.itcast.nsfw.role.entity.RolePrivilegeId">
<key-many-to-one name="role" lazy="false"class="cn.itcast.nsfw.role.entity.Role">
<column name="role_id" ></column>
</key-many-to-one>
<key-property name="code" type="java.lang.String">
<column name="code" length="20"></column>
</key-property>
</composite-id>
</class>
</hibernate-mapping>


6.2.2dao、service层主要操作方法
dao中主要方法:
public class RoleDaoImpl extends BaseDaoImpl<Role> implementsRoleDao {
@Override
public void deleteRolePrivilegeByRoleId(String roleId) {
Query query = getSession().createQuery("DELETE FROMRolePrivilege WHERE id.role.roleId=?");
query.setParameter(0, roleId);
query.executeUpdate();
}
}


service中主要方法:
@Service("roleService")
public class RoleServiceImpl implements RoleService {

@Resource
private RoleDao roleDao;
@Override
public void save(Role role) {
roleDao.save(role);
}

@Override
public void update(Role role) {
//1、删除该角色对于的所有权限
roleDao.deleteRolePrivilegeByRoleId(role.getRoleId());
//2、更新角色及其权限
roleDao.update(role);
}

@Override
public void delete(Serializable id) {
roleDao.delete(id);
}

@Override
public Role findObjectById(Serializable id) {
return roleDao.findObjectById(id);
}

@Override
public List<Role> findObjects() {
return roleDao.findObjects();
}
}




6.2.3action中主要方法
public class RoleAction extends BaseAction {
@Resource
private RoleService roleService;
private List<Role> roleList;
private Role role;
private String[] privilegeIds;
//列表页面
public String listUI() throws Exception{
//加载权限集合
ActionContext.getContext().getContextMap().put("privilegeMap",Constant.PRIVILEGE_MAP);
try {
roleList = roleService.findObjects();
} catch (Exception e) {
throw new Exception(e.getMessage());
}
return "listUI";
}
//跳转到新增页面
public String addUI(){
//加载权限集合
ActionContext.getContext().getContextMap().put("privilegeMap",Constant.PRIVILEGE_MAP);
return "addUI";
}
//保存新增
public String add(){
try {
if(role != null){
//处理权限保存
if(privilegeIds != null){
HashSet<RolePrivilege> set = newHashSet<RolePrivilege>();
for(int i = 0; i < privilegeIds.length; i++){
set.add(new RolePrivilege(new RolePrivilegeId(role,privilegeIds[i])));
}
role.setRolePrivileges(set);
}
roleService.save(role);
}
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
//跳转到编辑页面
public String editUI(){
//加载权限集合
ActionContext.getContext().getContextMap().put("privilegeMap",Constant.PRIVILEGE_MAP);
if (role != null && role.getRoleId() != null) {
role = roleService.findObjectById(role.getRoleId());
//处理权限回显
if(role.getRolePrivileges() != null){
privilegeIds = newString[role.getRolePrivileges().size()];
int i = 0;
for(RolePrivilege rp: role.getRolePrivileges()){
privilegeIds[i++] = rp.getId().getCode();
}
}
}
return "editUI";
}
//保存编辑
public String edit(){
try {
if(role != null){
//处理权限保存
if(privilegeIds != null){
HashSet<RolePrivilege> set = newHashSet<RolePrivilege>();
for(int i = 0; i < privilegeIds.length; i++){
set.add(new RolePrivilege(new RolePrivilegeId(role,privilegeIds[i])));
}
role.setRolePrivileges(set);
}
roleService.update(role);
}
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
//删除
public String delete(){
if(role != null && role.getRoleId() != null){
roleService.delete(role.getRoleId());
}
return "list";
}
//批量删除
public String deleteSelected(){
if(selectedRow != null){
for(String id: selectedRow){
roleService.delete(id);
}
}
return "list";
}
public List<Role> getRoleList() {
return roleList;
}
public void setRoleList(List<Role> roleList) {
this.roleList = roleList;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public String[] getPrivilegeIds() {
return privilegeIds;
}
public void setPrivilegeIds(String[] privilegeIds) {
this.privilegeIds = privilegeIds;
}
}




6.2.4配置文件
配置role-spring.xml
 <bean id="roleDao"class="cn.itcast.nsfw.role.dao.impl.RoleDaoImpl"parent="baseDao"></bean>
    <!-- 扫描service -->
    <context:component-scanbase-package="cn.itcast.nsfw.role.service.impl"></context:component-scan>
role-struts.xml
<package name="role-action" namespace="/nsfw"extends="base-default">
<action name="role_*"class="cn.itcast.nsfw.role.action.RoleAction" method="{1}">
<resultname="{1}">/WEB-INF/jsp/nsfw/role/{1}.jsp</result>
<result name="list" type="redirectAction">
<param name="actionName">role_listUI</param>
</result>
</action>
</package>


并将role-struts.xml加入到struts.xml总配置文件中。
<!-- 包含角色管理的struts配置文件 -->
<includefile="cn/itcast/nsfw/role/conf/role-struts.xml"></include>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值