Marty/DrPompeji, thank you for your replies:
It seems my issue was not resolved completely. Form, gets rendered properly when the a new request is sent to the controller. However, if there are any errors in the form and it has to be redisplayed then the binding of courses fails.
Here is the User, Role, and RoleEditor:
Code:
public class UserDO {
@NotEmpty(message="Username must be between 4 and 7 characters.")
@Size(min=4)
private String userName;
@NotEmpty(message="User must have at least one role.")
@Valid
private List<Role> roles;
}
public class Role {
private int roleId;
@NotEmpty(message="Role ID cannot be blank.")
private String roleName;
}
public class RoleEditor extends CustomCollectionEditor {
private List<Role> roles;
public RoleEditor(Class collectionType, boolean nullAsEmptyCollection) {
super(collectionType, true);
}
public void setValue( Object object ){
if(object!=null)
System.out.println("Object is of type - " + object.getClass().getCanonicalName());
String[] roleIds = (String[])object;
roles=new ArrayList<Role>();
if(roleIds!=null && roleIds.length<=0)
for( int i=0; i<roleIds.length; i++ ){
try {
int id = Integer.parseInt(roleIds[i]);
Role role = new Role();
role.setRoleId(id);
if(id==1) role.setRoleName("ADMIN");
if(id==2) role.setRoleName("EVERYONE");
roles.add(role);
}catch( NumberFormatException ne ){}
}
}
public Object getValue(){
System.out.println("Roles are - " + roles);
return roles;
}
}
Here is the controller for managing user:
Code:
@Controller
@SessionAttributes({"user","userroles"})
public class UserController {
@InitBinder
public void bindForm(WebDataBinder binder) {
binder.registerCustomEditor(List.class, new RoleEditor(List.class,true));
}
@RequestMapping(value="/register", method=RequestMethod.GET)
public String showForm(Model model){
model.addAttribute("user",new UserDO());
//creating list of roles for the form
Role roleAdmin = new Role();
Role roleOthers = new Role();
roleAdmin.setRoleId(1);roleAdmin.setRoleName("ADMIN");
roleOthers.setRoleId(2);roleOthers.setRoleName("EVERYONE");
List<Role> roles = new ArrayList<Role>();
roles.add(roleAdmin);
roles.add(roleOthers);
model.addAttribute("userroles",roles);
return "register";
}
@RequestMapping(value="/submitUserDeatils", method=RequestMethod.POST)
public String formSubmit(@ModelAttribute("user") @Valid UserDO user,BindingResult results, Model model){
if(results.hasErrors()){
return "register";
}
return "submitRequest";
}
//following is the jsp form
<form:form action="submitUserDeatils" modelAttribute="user">
<table>
<tr>
<td>User Name :</td><form:errors path="userName" cssClass="error"/>
<td><form:input path="userName" /></td>
</tr>
<tr>
<td>User Role :</td><form:errors path="roles" cssClass="error"/>
<td>
<form:checkboxes path="roles" items="${userroles}" itemLabel="roleName" itemValue="roleId"/>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
So, the form loads fine when GET request is submitted however upon submit (POST) if the form has any errors the list of roles binding with the form fails with following error:
Code:
//if I select both roles and submit I get
SystemOut O Object is of type - java.lang.String[]
SystemOut O Object is of type - java.util.ArrayList
ServletWrappe E SRVE0068E: Uncaught exception thrown in one of the service methods of the servlet: /WEB-INF/views/sample/simpleForm.jsp. Exception thrown : java.lang.ClassCastException: java.util.ArrayList incompatible with [Ljava.lang.String;
at com.sample.domain.RoleEditor.setValue(RoleEditor.java:20)
//if I select only one role and submit I get
Property roles threw exception; nested exception is java.lang.ClassCastException: java.lang.String incompatible with [Ljava.lang.String;
//Also the type of obect in RoleEditor is when one role is selected.
[6/15/11 14:38:51:167 EDT] 0000001e SystemOut O Object is of type - java.lang.String
[6/15/11 14:38:51:199 EDT] 0000001e SystemOut O Object is of type - java.lang.Integer
[6/15/11 14:38:51:199 EDT] 0000001e SystemOut O Object is of type - java.lang.Integer
[6/15/11 14:38:51:199 EDT] 0000001e SystemOut O Object is of type - com.sample.domain.Role
Any suggestions????