040905

来源:互联网 发布:软件商城下载 编辑:程序博客网 时间:2024/06/03 15:11

角色的增删改查



nsfw.role.dao


RoleDao extends BaseDao<Role>




dao.impl


RoleDaoImpl extends BaseDaoImpl<Role>



nsfw.role.service


RoleService 


实现基础方法


增删改查5方法


service.impl


@Service(“roleService”)

public classRoleServiceImpl implements



@Resource

private RoleDao roleDao;


下面写action

role.action


RoleAction extends BaseAction

实现七个方法

保存新增 编辑两个删除两个 查找一个


UserAction全部复制过来,修改



配置文件



role.conf


role-spring.xml

role-struts.xml



user替换成Role




struts.xml 

包含角色管理的struts的配置文件

<include file=“cn/itcast/nsfw/role/conf/role-struts.xml”>


引入前台页面




/nsfw/role_addUI.action



复选框标签

checkBoxList标签


<s:checkboxlist list=“#privilegeMap” name=“privilegeIds”></>


后台定义一个数组来接收




public String addUI(){

ActionContext.getContext().getContextMap().put(“privilegeMap”,Constant.PIRVILEGE_MAP);


Constant.PIRVILEGE_MAP 常量类




}



ActionContext级别是request级别



后台接收权限




@Resource

…..

private String[] privilegeIds;


setget




<set name=“rolePrivileges”inverse=“true” lazy=“true” cascade=”save-update,delete”>




public String add(){

。。。。


if(role!=null)

{

//处理权限保存

if(privilegeIds!=null){

HashSet<RolePrivilege> set=new HashSet<RolePrivilege>();

for(int i=0;i<privilegeIds.length;i++){

set.add(new RolePrivilege(new RolePrivilegeId(role,privilegeIds[i])));

}

role.setRolePrivileges(set);

}

roleService.save(role);

}

。。。。

}


以上要重点看,要学会!!!!


编辑方法也类似




editUI .jsp


没有回显



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=new String[role.getRolePrivileges().size()];

int i=0;

for(RolePrivilege rp:role.getRolePrivileges()){

privilegeIds[i++]=rp.getId().getCode();

}

}

}

}



lazy=“false”



public void update(Role role){

//删除该角色对于所有的权限


roleDao.deleteRolePrivilegeByRoleId(role.getRoleId());

//更新角色及其权限

roleDao.update(role);

}



RoleDaoImpl

public void deleteRolePrivilegeByRoleId(String roleId){

getSession().createQuery(“Delete from RolePrivilege where id.role.roleId=?”)

query.setParameter(0,roleId);

query.executeUpdate();

}




代码如下:


Constant.java

public class Constant {

//系统权限集合

publicstatic String PRIVILEGE_XZGL="xzgl";

publicstatic String PRIVILEGE_HQFW="hqfw";

publicstatic String PRIVILEGE_ZXXX="zxxx";

publicstatic String PRIVILEGE_NSFW="nsfw";

publicstatic 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,"我的空间");

}

}

Role.java

public classRole implements Serializable{

private StringroleId;

private Stringname;

private Stringstate;

private Set<RolePrivilege>rolePrivileges;

public StringROLE_STATE_VALID = "1";

public StringROLE_STATE_INVALID = "0";

public Role() {

// TODO Auto-generated constructor stub

}


public Role(String roleId, String name, String state,

Set<RolePrivilege> rolePrivileges, String rOLE_STATE_VALID,

String rOLE_STATE_INVALID) {

this.roleId = roleId;

this.name = name;

this.state = state;

this.rolePrivileges = rolePrivileges;

ROLE_STATE_VALID = rOLE_STATE_VALID;

ROLE_STATE_INVALID = rOLE_STATE_INVALID;

}


public String getRoleId() {

returnroleId;

}


public void setRoleId(String roleId) {

this.roleId = roleId;

}


public String getName() {

returnname;

}


public void setName(String name) {

this.name = name;

}


public String getState() {

returnstate;

}


public void setState(String state) {

this.state = state;

}


public Set<RolePrivilege> getRolePrivileges() {

returnrolePrivileges;

}


public void setRolePrivileges(Set<RolePrivilege> rolePrivileges) {

this.rolePrivileges = rolePrivileges;

}


}


Role.hbm.xml

<hibernate-mapping>

<classname="cn.itcast.nsfw.role.entity.Role"table="role">

<idname="roleId"type="java.lang.String">

<columnname="role_id"length="32"/>

<generatorclass="uuid.hex"/>

</id>

<propertyname="name"type="java.lang.String">

<columnname="name"length="20"not-null="true"/>

</property>

<propertyname="state"type="java.lang.String">

<columnname="state"length="1"/>

</property>

<setname="rolePrivileges"inverse="true"lazy="false"cascade="save-update,delete">

<key>

<columnname="role_id"></column>

</key>

<one-to-manyclass="cn.itcast.nsfw.role.entity.RolePrivilege"/>

</set>

</class>

</hibernate-mapping>

RolePrivilege.java

public classRolePrivilege  implements Serializable{

private RolePrivilegeIdid;


public RolePrivilegeId getId() {

returnid;

}

public void setId(RolePrivilegeId id) {

this.id = id;

}

public RolePrivilege(RolePrivilegeId id) {

this.id = id;

}

public RolePrivilege() {

}

}

RolePrivilege.hbm.xml

<hibernate-mapping>

<classname="cn.itcast.nsfw.role.entity.RolePrivilege"table="role_privilege">

<composite-idname="id"class="cn.itcast.nsfw.role.entity.RolePrivilegeId">

<key-many-to-onename="role"class="cn.itcast.nsfw.role.entity.Role"lazy="false">

<columnname="role_id"></column>

</key-many-to-one>

<key-propertyname="code"type="java.lang.String">

<columnname="code"length="20"></column>

</key-property>

</composite-id>

</class>

</hibernate-mapping>

RolePrivilegeId.java

public classRolePrivilegeId  implements Serializable{

private Role role;

private Stringcode;

@Override

public int hashCode() {

finalint prime = 31;

int result = 1;

result = prime * result + ((code ==null) ? 0 : code.hashCode());

result = prime * result + ((role ==null) ? 0 : role.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

returntrue;

if (obj ==null)

returnfalse;

if (getClass() != obj.getClass())

returnfalse;

RolePrivilegeId other = (RolePrivilegeId) obj;

if (code ==null) {

if (other.code !=null)

returnfalse;

} elseif (!code.equals(other.code))

returnfalse;

if (role ==null) {

if (other.role !=null)

returnfalse;

} elseif (!role.equals(other.role))

returnfalse;

returntrue;

}

public RolePrivilegeId() {

}

public RolePrivilegeId(Role role, String code) {

this.role = role;

this.code = code;

}

public Role getRole() {

returnrole;

}

public void setRole(Role role) {

this.role = role;

}

public String getCode() {

returncode;

}

public void setCode(String code) {

this.code = code;

}

}

RoleDao.java

public interface RoleDaoextends BaseDao<Role> {

void deleteRolePrivilegeByRoleId(String roleId);

}

RoleDaoImpl.java

public class RoleDaoImplextends BaseDaoImpl<Role> implements RoleDao {

@Override

public void deleteRolePrivilegeByRoleId(String roleId) {

// TODO Auto-generated method stub

Query query=getSession().createQuery("DELETE FROM RolePrivilege where id.role.roleId=?")

.setParameter(0, roleId);

query.executeUpdate();

}

}

RoleService.java

public interface RoleService {

/*

* 新增 

*/

public void save(Role role);

/*

* 更新 

*/

public void update(Role role);

/*

* 删除 

*/

public void delete(Serializable id);

/*

* 根据id查找

*/

public Role findObjectById(Serializable id);

/*

* 查找列表

*/

public List<Role> findObjects();

}

RoleServiceImpl.java

@Service("roleService")

public class RoleServiceImplimplements RoleService{

@Resource

private RoleDaoroleDao;

@Override

public void save(Role role) {

// TODO Auto-generated method stub

roleDao.save(role);

}

@Override

public void update(Role role) {

// TODO Auto-generated method stub

//1.删除该角色所有对象权限

roleDao.deleteRolePrivilegeByRoleId(role.getRoleId());

//2.更新角色及权限

roleDao.update(role);

}

@Override

public void delete(Serializable id) {

// TODO Auto-generated method stub

roleDao.delete(id);

}

@Override

public Role findObjectById(Serializable id) {

// TODO Auto-generated method stub

returnroleDao.findObjectById(id);

}

@Override

public List<Role> findObjects() {

// TODO Auto-generated method stub

returnroleDao.findObjects();

}

}

RoleAction.java

public classRoleAction extends BaseAction {

// 调用roleService(注入名称,不能随便改)

@Resource

private RoleServiceroleService;

private List<Role>roleList;

// 用户信息接收(普通变量,可以随便改)

private Role role;

//用于接收addUI的权限数组

private String[]privilegeIds;


// 调用增删改查方法

// 一个CRUD中包含:

// 列表页面

public String listUI() {

//加载权限集合

ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP);

roleList = roleService.findObjects();


return"listUI";

}


// 跳转到新增页面

public String addUI() {

//加载权限集合

ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP);

return"addUI";

}


// 保存新增

public String add() {

if (role !=null) {

if(privilegeIds!=null){

System.out.println("不为空");

HashSet<RolePrivilege> set=new HashSet<RolePrivilege>();

for(int i=0;i<privilegeIds.length;i++){

//???????

set.add(newRolePrivilege(new RolePrivilegeId(role,privilegeIds[i])));

}

role.setRolePrivileges(set);

}

roleService.save(role);

}

return"list";

}


/**

* 跳转到编辑页面

* 

* */

public String editUI() {

//加载权限集合

ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP);

if (role !=null && role.getRoleId() !=null) {

role =roleService.findObjectById(role.getRoleId());

//如果set集合为空,初始化数组,长度为set集合的长度

if(role.getRolePrivileges()!=null){

privilegeIds=new String[role.getRolePrivileges().size()];

int i=0;

for(RolePrivilege rp:role.getRolePrivileges()){

privilegeIds[i++]=rp.getId().getCode();

}

}

}

return"editUI";

}


// 保存编辑

public String edit() {

if (role !=null) {

if(privilegeIds!=null){

System.out.println("不为空");

HashSet<RolePrivilege> set=new HashSet<RolePrivilege>();

for(int i=0;i<privilegeIds.length;i++){

//???????

set.add(newRolePrivilege(new RolePrivilegeId(role,privilegeIds[i])));

}

role.setRolePrivileges(set);

}

roleService.update(role);

}

return"list";

}


// 删除

public String delete() {

if (role !=null && role.getRoleId() !=null) {

System.out.println(role.getRoleId());

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() {

returnroleList;

}


public void setRoleList(List<Role> roleList) {

this.roleList = roleList;

}


public void setRole(Role role) {

this.role = role;

}


public Role getRole() {

returnrole;

}

public String[] getPrivilegeIds() {

returnprivilegeIds;

}


public void setPrivilegeIds(String[] privilegeIds) {

this.privilegeIds = privilegeIds;

}

}

role-spring.xml

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    

    <beanid="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>

</beans>

role-struts.xml

<struts>

<packagename="role-action"namespace="/nsfw"extends="base-default">

<actionname="role_*"class="cn.itcast.nsfw.role.action.RoleAction"method="{1}">

<resultname="{1}">/WEB-INF/jsp/nsfw/role/{1}.jsp</result>

<resultname="list"type="redirectAction">

<paramname="actionName">role_listUI</param>

</result>

</action>

</package>


</struts>


原创粉丝点击