Reputation: 18786
Using Spring/SpringMVC 3.0.5 I've defined a method in my controller like this:
@RequestMapping(params = { "save","!delete" }, method = RequestMethod.POST)
public ModelAndView saveFoo(...
@ModelAttribute("vo") @Valid DescriptionBuilderVO vo, BindingResult result) {
...
result.rejectValue("foo.sequenceNumber", "foo.builder", new Object[]{vo.getFoo().getSequenceNumber()}, "Sequence Number too high"); vo.getFoo().setSequenceNumber(originalNumber);
return new ModelAndView(WebConstants.VIEW_BUILDER, "vo", vo);
Notice that I'm attempting to set a value in the VO object inside the controller. The funny thing is that if I do this with @ModelAttribute the new value doesn't show up. If I remove @ModelAttribute from the method contract, the new value appears exactly as you would think. The problem comes when there are errors, the only way to get the errors is to have the @ModelAttribute in the contract.
BTW my HTML looks like:
HTML
<form:input path="foo.sequenceNumber" id="sequenceNumber" size="4" maxlength="4"/>
<form:errors path="foo.sequenceNumber" cssClass="ui-state-error" />
foo.sequenceNumber = the value the user typed in; when I use @ModelAttribute
foo.sequenceNumber = the value I set in the controller; but I lose any errors
It seems to me that SpringMVC is putting the ModelAttribute VO into a "special" place and passing it back to the jsp but not in an obvious location. Does anyone know how I can get at the VO object in this situation?
Upvotes: 2
Views: 1961
Reputation: 181
Try naming your object "descriptionBuilderVO", like the following:
@Valid @ModelAttribute("descriptionBuilderVO") DescriptionBuilderVO descriptionBuilderVO,
BindingResult result)
I know it shouldn't be this way, but I've found problems when the name of the object is different than the class name.
Note that you'll also have to change the name of the object in your jsp:
<form:form commandName="descriptionBuilderVO"
...etc...
Upvotes: 0
Reputation: 18786
I've tried many different things including the reordering the Valid and ModelAttribute annotations and it doesn't seem to make a difference.
In reading the documentation suggested by Pat in the comments it does refer to a special context where the VO is stored. Anyway, if you're trying anything similar to this I suggest you go about it in a different way perhaps building a completely new VO and passing it back to the view, which is what I did.
Upvotes: 0
Reputation: 3503
wierd. The same thing works for me. The only difference i see is the order of Valid and ModelAttribute can you try reversing the order of Valid and ModelAttribute?
public ModelAndView saveFoo(...
@Valid @ModelAttribute("vo") DescriptionBuilderVO vo, BindingResult result) {
}
BTW, which version of spring are you using?
Upvotes: 1