角色的修改

1、打开修改页面
--根据ID查询出角色数据
--查询出中间表数据,并用其做默认勾选
--查询出全部的权限数据,并用其生成默认的选项
2、修改保存
--修改角色表
--修改中间表,采取先删除,再新增的方式

DAO:

public Role findById(Integer id) throws DAOException {

	if(id==null){
		return null;
	}
	String sql = "select * from role_info where id=?";
	Connection con = DBUtil.getConnection();
	try {
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		if(rs.next()){
			Role role = new Role();
			role.setId(rs.getInt("id"));
			role.setName(rs.getString("name"));
			//查询角色对应的权限
			String sql2 = "select privilege_id from role_privilege where" +
					" role_id=?";
			PreparedStatement ps2 = con.prepareStatement(sql2);
			ps2.setInt(1, id);
			ResultSet rs2 = ps2.executeQuery();
			List<Integer> pids = new ArrayList<Integer>(); 
			while(rs2.next()){
				int pid = rs2.getInt(1);
				pids.add(pid);
			}
			role.setPrivilegeIds(pids);
			return role;
		}
	} catch (SQLException e) {
		e.printStackTrace();
		throw new DAOException("查询角色失败", e);
	} finally{
		DBUtil.close();
	}
	
	return null;
}

public void update(Role role) throws DAOException {
	if(role == null){
		return;
	}
	
	String sql = "update role_info" +
			" set name=?" +
			" where id=?";
	Connection con = DBUtil.getConnection();
	
	try {
		con.setAutoCommit(false);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, role.getName());
		ps.setObject(2, role.getId());
		ps.executeUpdate();
		
		String sql2 = "delete from role_privilege where role_id=?";
		PreparedStatement ps2 = con.prepareStatement(sql2);
		ps2.setObject(1, role.getId());
		ps2.executeUpdate();
		
		List<Integer> pids = role.getPrivilegeIds();
		if(pids!=null && pids.size()>0){
			String sql3 = "insert into role_privilege values(?,?)";
			PreparedStatement ps3 = con.prepareStatement(sql3);
			for(Integer pid: pids){
				ps3.setObject(1, role.getId());
				ps3.setObject(2, pid);
				ps3.addBatch();
			}
			ps3.executeBatch();
		}
		con.commit();
	} catch (SQLException e) {
		e.printStackTrace();
		try {
			con.rollback();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		throw new DAOException("更新角色失败", e);
		
	} finally{
		DBUtil.close();
	}
			
}

Action:

public class ToUpdateRoleAction {
	public String execute() {

		IRoleDAO dao = DAOFactory.getRoleDAO();

		try {
			role = dao.findById(id);
			privileges = PrivilegeReader.getPrivileges();
		} catch (DAOException e) {
			e.printStackTrace();
			return "error";
		}
		return "success";
	}

	// input
	private Integer id;
	// output
	private Role role;
	private List<Privilege> privileges;// 查询当前角色的所有权限,用于初始化复选框

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public List<Privilege> getPrivileges() {
		return privileges;
	}

	public void setPrivileges(List<Privilege> privileges) {
		this.privileges = privileges;
	}

}

public class UpdateRoleAction {
	
	public String execute(){
		IRoleDAO dao = DAOFactory.getRoleDAO();
		try {
			dao.update(role);
		} catch (DAOException e) {
			e.printStackTrace();
			return "error";
		}
		
		return "success";
	}
	
	//input
	private Role role;

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}
	
}

struts.xml

<!-- 转到更新角色页面Action -->
<action name="toUpdateRole" class="netctoss.action.role.ToUpdateRoleAction">
	<result name="success">
		/WEB-INF/role/updateRole.jsp
	</result>
</action>
<!-- 更新角色Action -->
<action name="updateRole" class="netctoss.action.role.UpdateRoleAction">
	<result name="success" type="redirectAction">
		findRole
	</result>
</action>



Jsp:

<form action="updateRole" method="post" class="main_form">
	<!-- 将id放入hidden,保存时提交给Action -->
	<s:hidden name="role.id"/>
	<div class="text_info clearfix"><span>角色名称:</span></div>
	<div class="input_info">
		<s:textfield name="role.name" cssClass="width200"/>
	    <span class="required">*</span>
	    <div class="validate_msg_medium error_msg">不能为空,且为20长度的字母、数字和汉字的组合</div>
	</div>                    
	<div class="text_info clearfix"><span>设置权限:</span></div>
	<div class="input_info_high">
	    <div class="input_info_scroll">
		<s:checkboxlist name="role.privilegeIds" list="privileges" listKey="id" listValue="name"/>
	    </div>
	    <span class="required">*</span>
	    <div class="validate_msg_tiny">至少选择一个权限</div>
	</div>
	<div class="button_info clearfix">
	    <input type="submit" value="保存" class="btn_save" />
	    <input type="button" value="取消" class="btn_save" />
	</div>
    </form> 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值