I need help with understanding following example with @ModelAttribute
from Spring
documentation: (method populatePetTypes()
)
@Controller
@RequestMapping("/owners/{ownerId}/pets/{petId}/edit")
@SessionAttributes("pet")
public class EditPetForm {
// ...
@ModelAttribute("types")
public Collection<PetType> populatePetTypes() {
return this.clinic.getPetTypes();
}
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(
@ModelAttribute("pet") Pet pet,
BindingResult result, SessionStatus status) {
new PetValidator().validate(pet, result);
if (result.hasErrors()) {
return "petForm";
}
else {
this.clinic.storePet(pet);
status.setComplete();
return "redirect:owner.do?ownerId=" + pet.getOwner().getId();
}
}
}
I am undestood this like additional "value" which our Model object could get in the whole current controller. Is it true?
I am trying to do some test, by adding that code to my controller:
@ModelAttribute("user")
public Integer getKod(){
return new Integer(123321);
}
another method is:
@RequestMapping("/register")
public String register(Map<String, Object> map, @ModelAttribute("user") MyUser user, BindingResult result) {
...
}
and now I am try just show "kod" into my form:
<form:form method="post" action="" commandName="user">
(...)
<form:label path="kod">kod</form:label>:
<form:input path="kod"/>
(...)
</form:form>
but I got:
java.lang.IllegalArgumentException: java.lang.ClassCastException@229d7c57
please help!