multiple select: Failed to convert property value of type java.lang.String
to required type java.util.Set
Im getting this exception:
Failed to convert property value of type java.lang.String to required type
java.util.Set for property roles; nested exception is
java.lang.IllegalStateException: Cannot convert value of type
[com.yavale.baseProject.database.model.Rol] to required type
[com.yavale.baseProject.database.model.Usuario_Rol] for property roles[0]:
no matching editors or conversion strategy found
I have a class Usuario (User) that contains roles. In the user form I want
to be able to select roles.
I have this model:
@Entity
@Table(name = "usuario", uniqueConstraints = {
@UniqueConstraint(columnNames = "login"),
@UniqueConstraint(columnNames = "correo") })
public class Usuario implements Serializable {
@Id
@Column(name = "_id")
private String id;
@Column(name = "login")
@Size(min = 4)
@NotEmpty
private String login;
@Column(name = "password")
@NotEmpty
@Size(min = 4)
private String password;
@OneToMany(fetch = FetchType.EAGER, mappedBy =
"usuarioRol_pk.usuario", cascade = CascadeType.ALL)
private Set<Usuario_Rol> roles = new HashSet<Usuario_Rol>(0);
...
This is my controller:
@RequestMapping(value = "/new", method = RequestMethod.GET)
public ModelAndView executeNew() {
logger.info("executeNew");
ModelAndView mav = new ModelAndView("usuarioNew");
Usuario usuario = new Usuario();
List<Rol> roles = rolDao.findAll();
mav.getModelMap().put("usuarioNew", usuario);
mav.getModelMap().put("roles", roles);
return mav;
}
@RequestMapping(value = "/", method = RequestMethod.POST)
public ModelAndView executeCreate(
@Valid @ModelAttribute("usuarioNew") Usuario usuario,
BindingResult result, SessionStatus status,
final RedirectAttributes redirectAttributes) {
logger.info("executeCreate");
if (result.hasErrors()) {
ModelAndView mav = new ModelAndView("usuarioNew");
return mav;
}
usuarioDao.insert(usuario);
status.setComplete();
ModelAndView mav = new ModelAndView();
String message = this.messages.getMessage("ok.itemCreated",
new Object[] { "Usuario" }, null);
redirectAttributes.addFlashAttribute("message", message);
mav.setViewName("redirect:/usuarios/");
return mav;
}
I have this in my form:
<form:label path="roles">Rol:</form:label>
<form:select multiple="true" path="roles">
<form:options items="${roles}" itemValue="id"
itemLabel="nombre"/>
</form:select>
<form:errors cssStyle="color:red" path="roles"></form:errors>
I have read about this exception so I have this in my controller as well:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Set.class, "roles", new
RolCollectionEditor(Set.class, rolDao));
}
This is RolCollectionEditor:
public class RolCollectionEditor extends CustomCollectionEditor {
private final RolDAO rolDao;
public RolCollectionEditor(Class<?> collectionType, RolDAO rolDao) {
super(collectionType);
this.rolDao = rolDao;
}
@Override
protected Object convertElement(Object element) {
if (element instanceof Rol) {
System.out.println("Converting from Rol to Rol: " + element);
return element;
}
if (element instanceof String) {
Rol rol = rolDao.findById((String) element);
System.out.println("Looking up Rol for id " + element + ": " +
rol.toString());
return rol;
}
System.out.println("Don't know what to do with: " + element);
return null;
}
}
This class is get called, but I dont know why I still have this exception
when I try to submit my form. Any ideas?
No comments:
Post a Comment