springmvc checkbox标签的使用

项目中的权限控制,用户可以同时具有多个角色。
                <tr bgcolor="f5f5f5">
<td width="22%" > <div align="right">角色管理:</div></td>
<td width="78%"> <c:forEach items="${roles}" var="item" varStatus="statu">
<form:checkbox path="roles" value="${item.roleid}"/>${item.rolename}
<%--
<input type="checkbox" id="privileges${statu.index}.privilegeid" name="privileges" value="${privilege.privilegeid}"/>${privilege.privilegename}
--%>  
<c:if test="${statu.count%4==0}"><br></c:if>
</c:forEach></td>
</tr>

上面的出现方法上出现了一个问题,就是为用户指定了角色以后,在编辑这个用户的时候checkbox的选项都没有被选中。
参考网上这篇文章,里面的这段话很有用
[url]http://tech.it168.com/jd/2007-09-27/200708210930609_2.shtml
[/url]
[b]复选框组件标签相对来说复杂一些,复选框组件对应的表单属性不但可以boolean类型,还可以是String[]、Collection,Enum等类型。针对不同属性类型,复选框的选中状态的判断条件是不一样的:
 boolean类型:当对应属性为true时,该复选框选中(一个属性仅对应一个复选框);
 String[]、Collection或Enum类型:复选框对应值出现在对应属性列表中,该复选框选中;
 其它类型:当复选框对应的值可以转换为对应属性值,该复选框选中。[/b]

因为Role这个类是自定义的,即属于上面这段话中的其他类型。 看看上面的checkbox中的
value是个整形数,可能springmvc在做动态绑定的时候直接就转换成了一个整数,不满足上面可以转换为对应属性值的条件。 从这个思路出发,把上面的checkbox改成了下面的实现

<tr bgcolor="f5f5f5">
<td width="22%" > <div align="right">角色管理:</div></td>
<td width="78%"> <c:forEach items="${roles}" var="item" varStatus="statu">
<form:checkbox path="roles" value="${item.roleid},${item.rolename}"/>${item.rolename}
<%--
<input type="checkbox" id="privileges${statu.index}.privilegeid" name="privileges" value="${privilege.privilegeid}"/>${privilege.privilegename}
--%>  
<c:if test="${statu.count%4==0}"><br></c:if>
</c:forEach></td>
</tr>

但是这样又带来一个问题,value的值不是默认类型,springmvc不能动态做绑定,所以就需要注册一个转换器

public class EmployeeRoleConverter extends PropertyEditorSupport {

@Override
public void setAsText(String text) throws IllegalArgumentException {
EmployeeRole tmp = null;
try {
String id = text.substring(0, text.indexOf(','));
String name = text.substring(text.indexOf(',') + 1, text.length());
tmp = new EmployeeRole(Integer.parseInt(id), name);
} catch (Exception ex) {
//这里不抛异常的话,binder机制发现不了错误,从而simpleFormController控制流程会继续导致bug出现
throw new IllegalArgumentException("角色格式不正确");
}
this.setValue(tmp);
}

}

这样还不行,后来调试了一下,在checkbox显示的过程中会多次调用这个setAsText方法。于是猜想在判断这个选项是不是该选中,是需要对象间的比较的,也就是equals方法。然后就在Role这个类中重写了equals和hashcode方法,而equals方法就只用到这两个字段,终于搞定。


@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final EmployeeRole other = (EmployeeRole) obj;
if (this.roleid != other.roleid) {
return false;
}
if ((this.rolename == null) ? (other.rolename != null) : !this.rolename.equals(other.rolename)) {
return false;
}
return true;
}

@Override
public int hashCode() {
int hash = 7;
hash = 13 * hash + this.roleid;
hash = 13 * hash + (this.rolename != null ? this.rolename.hashCode() : 0);
return hash;
}

不知道其他同行有没有碰到类似问题的,有没有更好的解决方法,请留爪!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值