I am using Spring MVC 2.5.5.
I have a controller that saves an object from an editing form and then returns to that form again with the saved object. I add the result of the save to the ModelAndView but it gets overwritten by the MVC framework, so I always get the pre-save version (the version posted to the save method). I have tried to understand why the binding overwrites my object values but with no luck. Basically in the saveProfile() method below - the values from the profileWrapper always end up overwriting those from the savedProfileWrapper. Here is my code:
Code:@Controller public final class ProfilesFormController { @Autowired private Validator validator; @ModelAttribute public ProfileWrapper formBackingObject(final Integer id) { if (id != null) { ProfileWrapper profile = loadProfile(id); return profile; } return new ProfileWrapper(new Profile(), ADMIN_ID); } @RequestMapping("/profiles/edit") public ModelAndView displayProfile(@ModelAttribute ProfileWrapper profileWrapper) { LOG.debug("Request to load profile with id: " + profileWrapper.getProfile().getId()); return createEditModelAndView(); } @RequestMapping(value = "/profiles/save", method = RequestMethod.POST) public ModelAndView saveProfile(@ModelAttribute("profileWrapper") ProfileWrapper profileWrapper, BindingResult result) { ModelAndView modelAndView = createEditModelAndView(); // validator.validate(profileWrapper, result); if (result.hasErrors()) { // return createEditModelAndView(); } ProfileWrapper savedProfileWrapper = null; try { Profile profile = profileRESTClient.post(extractProfileFromForm(profileWrapper)); savedProfileWrapper = createProfileWrapper(profile); addSuccessMessage(modelAndView); } catch (PAD2PADException e) { if (STALE_PROFILE_VERSION.equals(e.getErrorResponse().getErrorCode())) { savedProfileWrapper = loadProfile(profileWrapper.getProfile().getId()); addPageError(modelAndView, "The profile has been updated while you were editing." + " Please make your changes and save again." + e.getErrorResponse().getErrorMessage()); } else { throw e; } } // Reloading the profile to ensure we have a fully populated object modelAndView.addObject("profileWrapper", savedProfileWrapper); return modelAndView; } /** * Load the profile. * * @param id the id * @return the profile wrapper */ protected ProfileWrapper loadProfile(final int id) { Profile restCriteria = new Profile(); restCriteria.setId(id); Profile profile = profileRESTClient.getUnique(restCriteria, "attributes"); LOG.debug("Loaded profile (via REST): " + profile); return createProfileWrapper(profile); } /** * Translate the data on the HTML form (profileWrapper) to a <code>Profile</code> * for transmission to the back end. * * @param profileWrapper the profile wrapper * @return extract profile from form */ private Profile extractProfileFromForm(ProfileWrapper profileWrapper) { //converting from form to domain object return profile; } protected ModelAndView createEditModelAndView() { ModelAndView modelAndView = new ModelAndView("profiles/edit"); modelAndView.addObject("statuses", loadProfileStatuses()); modelAndView.addObject("products", loadProducts()); modelAndView.addObject("privacyPrefs", loadPrivacyPreferences()); modelAndView.addObject("genders", loadGenders()); modelAndView.addObject("dateDisplayFormats", loadDateDisplayFormats()); modelAndView.addObject("suspensionPeriods", loadSuspensionPeriods()); addDecoratorData(modelAndView); return modelAndView; } }


Reply With Quote
