I'm looking at this now if what I understand you're saying is correct Juergen, i'm not getting this behaviour.
I'm finding that the order of methods called with the @ModelAttribute is not effected by anything, whether the annotation is method or argument based.
I have the following methods all annotated with @ModelAttribute. The getOrganisations(...) and getRoles(...) methods require the bound return value from getSaveUserRequest(...) so that they can determine the correct values for the lists. I had hoped that using the @ModelAttribute("user") annotation at the method arg level would have forced the required order of these methods being called and passed the result of the getSaveUserRequest() method but it doesn't seem to be the case.
Previously I would have returned the saveUserRequest using formBackingObject and the reference data lists would have been setup in the referenceData() method. The lifecycle under SimpleFormController was very straight forward for this as formBackingObject always went off before referenceData(). I can't see any way to guarantee the order with the ModelAttribute annotation approach.
Any ideas ??
Code:
@ModelAttribute("user")
public SaveUserRequest getSaveUserRequest(@RequestParam(value = "id", required = false)
Long id) {
if (id != null) {
final UserDetails userDetails = administrationApplication.findUserById(id);
return convertToSaveUserRequest(userDetails);
} else {
final SaveUserRequest saveUserRequest = new SaveUserRequest();
saveUserRequest.setUserType(administrationApplication.getCurrentAuthenticatedUser().getUserType());
return saveUserRequest;
}
}
@ModelAttribute("roles")
public List<RoleDetails> getRoles(@ModelAttribute("user") SaveUserRequest saveUserRequest) {
if (saveUserRequest.isVendorUser()) {
return administrationApplication.findAllVendorRoles();
} else if (saveUserRequest.isClientUser()) {
return administrationApplication.findAllClientRoles();
} else {
return new ArrayList<RoleDetails>();
}
}
@SuppressWarnings("unchecked")
@ModelAttribute("userTypes")
public List<UserType> getUserTypes() {
return CollectionUtils.arrayToList(UserType.values());
}
@ModelAttribute("organisations")
public List<OrganisationDetails> getOrganisations(@ModelAttribute("user") SaveUserRequest saveUserRequest) {
if (saveUserRequest.isVendorUser()) {
return administrationApplication.findAllVendorOrganisations();
} else if (saveUserRequest.isClientUser()) {
return administrationApplication.findAllClientOrganisations();
} else {
return new ArrayList<OrganisationDetails>();
}
}
Something else that concerns me is that where the referenceData() method in SimpleFormController goes off only as part of calling showForm meaning it will go off when the form is setup for a GET and also following failed validation, it appears that the @ModelAttribute annotated methods will go off more often and not always at the required times.
Am I missing ,the point with some of this ?
cheers,
rob