whardwick
Jul 27th, 2009, 11:05 PM
I am currently trying to setup admin pages for Spring Security groups and authorities (or roles and permissions). The page will allow for the selection/deselection of Authorities for a specific Group. I have a GroupFormController where I am registering a CustomCollectionEditor as follows:
@Override
protected void initBinder(HttpServletRequest req, ServletRequestDataBinder binder) throws Exception {
super.initBinder(req, binder);
binder.registerCustomEditor(Set.class, "groups.authorities", new AuthorityEditor(Set.class, authorityDAO));
}
My AuthorityEditor looks like this:
public class AuthorityEditor extends CustomCollectionEditor {
private AuthorityDAO authorityDAO;
public AuthorityEditor(Class collectionType, AuthorityDAO authorityDAO) {
super(collectionType, false);
this.authorityDAO = authorityDAO;
}
@Override
protected Object convertElement(Object element) {
Authority authority = null;
if (element != null)
authority = authorityDAO.getAuthorityById(Long.parseLong((Stri ng)element));
return authority;
}
@Override
protected boolean alwaysCreateNewCollection() {
return true;
}
}
And my JSP form looks like this:
<form:form method="post" action="/admin/editGroup.htm" commandName="group">
<form:hidden path="id"/>
Name: <form:input path="name"/><br/><br/>
Authorities: <form:select path="authorities" multiple="true" size="5">
<form:options items="${group.authorities}" itemLabel="name" itemValue="id"/>
<%--
<c:forEach varStatus="loop" items="${group.authorities}" var="authority">
<form:option value="${authority.id}" label="${authority.name}"/>
</c:forEach>
--%>
</form:select><br/><br/>
<input type="submit" value="Save" />
</form:form>
The page is displaying Authorities properly in a multi-select box, but the save/submit is not working. I have stepped through the Spring code and my convertElement method is not being called, but instead the superclass method is being called, which is basically causing it to return a Set of strings and the attempted save of the group (and thus the Set of Strings) through hibernate fails.
Can someone explain to me or help me figure out why my Override method is not being called?
Thanks.
@Override
protected void initBinder(HttpServletRequest req, ServletRequestDataBinder binder) throws Exception {
super.initBinder(req, binder);
binder.registerCustomEditor(Set.class, "groups.authorities", new AuthorityEditor(Set.class, authorityDAO));
}
My AuthorityEditor looks like this:
public class AuthorityEditor extends CustomCollectionEditor {
private AuthorityDAO authorityDAO;
public AuthorityEditor(Class collectionType, AuthorityDAO authorityDAO) {
super(collectionType, false);
this.authorityDAO = authorityDAO;
}
@Override
protected Object convertElement(Object element) {
Authority authority = null;
if (element != null)
authority = authorityDAO.getAuthorityById(Long.parseLong((Stri ng)element));
return authority;
}
@Override
protected boolean alwaysCreateNewCollection() {
return true;
}
}
And my JSP form looks like this:
<form:form method="post" action="/admin/editGroup.htm" commandName="group">
<form:hidden path="id"/>
Name: <form:input path="name"/><br/><br/>
Authorities: <form:select path="authorities" multiple="true" size="5">
<form:options items="${group.authorities}" itemLabel="name" itemValue="id"/>
<%--
<c:forEach varStatus="loop" items="${group.authorities}" var="authority">
<form:option value="${authority.id}" label="${authority.name}"/>
</c:forEach>
--%>
</form:select><br/><br/>
<input type="submit" value="Save" />
</form:form>
The page is displaying Authorities properly in a multi-select box, but the save/submit is not working. I have stepped through the Spring code and my convertElement method is not being called, but instead the superclass method is being called, which is basically causing it to return a Set of strings and the attempted save of the group (and thus the Set of Strings) through hibernate fails.
Can someone explain to me or help me figure out why my Override method is not being called?
Thanks.