我已经用这个问题撞了一堵砖了一段时间,没有多少搜索和阅读就找到了答案。我已经发布在Spring论坛上,但无济于事。也许有人可以帮忙吗?
我有一个包含HashMap的bean,我已经使用Spring:form taglib和Spring:bind绑定了它。绑定到视图可以很好地工作,但在提交表单并使用基于注释的控制器进行处理时,绑定似乎无法正常工作。我没有例外,但没有填充HashMap。这是我的设置:
JSP:
<form:form method="post" action="updateUserPermissions" modelAttribute="wsPermissions" >
<table>
<tr class="head">
<td>Workspace</td>
<td>Read?</td>
<td>Write?</td>
<td>Manage?</td>
</tr>
<c:forEach var="ws" varStatus="wsItem" items="${selectedUserWSs}">
<c:set var="wsURI" value="'${ws.uri}'"/>
<tr>
<td>${ws.kbInfo.name} @ ${ws.community.name}</td>
<td>
<spring:bind path="wsPermissions.map[${ws.uri}].read">
<input type="checkbox" <c:if test="${status.value}">checked</c:if> disabled="disabled"/>
</spring:bind>
</td>
<td>
<spring:bind path="wsPermissions.map[${ws.uri}].write">
<input type="checkbox" <c:if test="${status.value}">checked</c:if>/>
</spring:bind>
</td>
<td>
<spring:bind path="wsPermissions.map[${ws.uri}].manage">
<input type="checkbox" <c:if test="${status.value}">checked</c:if>/>
</spring:bind>
</td>
<td><a href="removeUserWorkspace?userid=${selectedUser.email}&wsid=${ws.uri}">[Remove]</a></td>
</tr>
</c:forEach>
</table>
<input type="hidden" name="userid" value="${selectedUser.email}" />
<input type="submit" value="Update user permissions" />
</form:form>
这显示精细的绑定效果,复选框显示正确的初始值。然后通过此控制器方法处理表单提交:
@RequestMapping
public String updateUserPermissions(@ModelAttribute(value="wsPermissions") WorkspacePermissionMap wsPermissions,
@RequestParam String userid,
HttpServletRequest request, ModelMap modelMap){
// code to update permissions here.....
LOG.debug(wsPermissions.getMap().size());
//the above prints zero
return "redirect:editUser?id="+userid;
}
调试控制器方法显示HashMap为空。
以下是WorkspacePermissionMap的代码:
public class WorkspacePermissionMap {
private Map<String, WorkspacePermission> map = new HashMap<String, WorkspacePermission>();
/**
* @param map the map to set
*/
public void setMap(Map<String, WorkspacePermission> map) {
this.map = map;
}
/**
* @return the map
*/
public Map<String, WorkspacePermission> getMap() {
return map;
}
}
和WorkspacePermission:
public class WorkspacePermission {
private boolean read = false;
private boolean write = false;
private boolean manage = false;
/**
* @return the write
*/
public boolean isWrite() {
return write;
}
/**
* @param write the write to set
*/
public void setWrite(boolean write) {
this.write = write;
if (write){
setRead(write);
} else {
setManage(write);
}
}
/**
* @return the moderate
*/
public boolean isManage() {
return manage;
}
/**
* @param moderate the moderate to set
*/
public void setManage(boolean moderate) {
this.manage = moderate;
if (moderate){
setWrite(moderate);
setRead(moderate);
}
}
/**
* @param read the read to set
*/
public void setRead(boolean read) {
this.read = read;
if (!read){
setWrite(read);
setManage(read);
}
}
/**
* @return the read
*/
public boolean isRead() {
return read;
}
}
任何人都可以指出我哪里出错了吗?我完全是在错误的树上狂奔,还是我犯了一个愚蠢的错误?
感谢您的帮助,
克里斯
答案 0 :(得分:2)
对于此所需的jsp路径表达式肯定有些奇怪。
我正在使用硬编码值:
applicationDetails['first_name']
或您的例子:
wsPermissions.map['hardcodeduri'].read
这些对我来说很好。
我的建议是首先尝试硬编码然后查看是否可以改变它以使用动态变量。
答案 1 :(得分:1)
你的HTML复选框元素怎么没有名称或值属性?春天不应该为你做“全部”的魔力;)