This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

AnnouncementAnnouncement Module

Collapse

No announcement yet.

Migrating old controllers to Spring 3, question about forms and @ModelAttributePage Title Module

But I noticed that getUserChoices() is being called before the GET and the POST. Turns out, I only need that data on the page during the initial GET or if there were errors in the POST and I need to send the user back to the form. If the post ends up being successful and I forward them to a success page, then fetching that model data was unnecessary; it will never be used. So I cooked up a helper method, populateModel(), that adds this data to the Model in the case of a GET or an error. Is there a "Spring way" to do this, or am I on the right track?

In this code, it appears as if the author is setting up some data for the Model under "types", which appears to be a Collection of enums that may be used as options in a selectbox, or perhaps used in a label or somewhere else on the page.

My question with this code pertains to the @ModelAttribute("types") annotation for populatePetTypes(). Let's say for the sake of my example that this.clinic.getPetTypes() is an expensive method call. Spring will invoke the populatePetTypes() method before the GET (setupForm()) and before the POST (processSubmit()) methods in his controller. Now, for the GET and in the case of a POST validation error, that's great because the "petForm" view page will be rendered, and the collection of PetType object will be used.

But if the post succeeds, however, there will be a redirection to "redirect:owner.do..." and fetching the collection of PetTypes will have been unnecessary.

Is there any way to get finer control over the population of model data in a controller like this?