Sammy Pawar
Sammy Pawar

Reputation: 1251

Migrating to Spring MVC 4

We are migrating our mvc code to Spring 4. Previously we had a a method formBackingObject which we converted to get method initForm. But trouble is - in previous controller which was extending SimpleFormController, formBackingObject was getting called even before submit method. We have now removed SimpleFormController. But initForm is getting called only only once on page load. It doesn't get called before submit. And there is some custom logic of creating user object and adding to UserProfileForm object.

Have you faced similar issue.

Old code

    protected Object formBackingObject(HttpServletRequest request) throws Exception {       
    final UserProfileForm userProfileForm = new UserProfileForm();
    final String id = request.getParameter("id");
    if (id != null && !id.trim().equals("")) {
        final User user = authenticationServices.findUser(ServletRequestUtils.getLongParameter(request, "id"));
        userProfileForm.setUser(user);
    } else {
        final User user = new User();
        userProfileForm.setUser(user);
    }
    return userProfileForm;
}

new code

@RequestMapping(method = RequestMethod.GET)
public String initForm(HttpServletRequest request, ModelMap model) throws Exception{
    final UserProfileForm userProfileForm = new UserProfileForm();
    final String id = request.getParameter("id");
    if (id != null && !id.trim().equals("")) {
        final User user = authenticationServices.findUser(ServletRequestUtils.getLongParameter(request, "id"));
        userProfileForm.setUser(user);
    } else {
        final User user = new User();
        userProfileForm.setUser(user);
    }
    addToModel(request, model);
    model.addAttribute("userProfileForm", userProfileForm);
    return "user-management/user-profile";
}

Upvotes: 1

Views: 1444

Answers (2)

Certain inter-module dependencies are now optional at the Maven POM level where they were once required. For example, spring-tx and its dependence on spring-context. This may result in ClassNotFoundErrors or other similar problems for users that have been relying on transitive dependency management to pull in affected downstream spring-* . To resolve this problem, simply add the appropriate missing jars to your build configuration.

Upvotes: 0

M. Deinum
M. Deinum

Reputation: 124526

Create a method annotated with @ModelAttribute to fill your model.

@ModelAttribute("userProfileForm");
public UserProfileForm formBackingObject(@RequestParam(value="id", required=false) Long id) throws Exception{
    final UserProfileForm userProfileForm = new UserProfileForm();
    if (id != null) {
        final User user = authenticationServices.findUser(id);
        userProfileForm.setUser(user);
    } else {
        final User user = new User();
        userProfileForm.setUser(user);
    }
    return userProfileForm; 
}

@RequestMapping(method = RequestMethod.GET)
public String initForm() {
    return "user-management/user-profile";
}

This way you can also use the @RequestParam annotation instead of pulling out parameters yourself.

See the reference guide for more information on the subject.

Upvotes: 3

Related Questions