1

I'm using a domain object as a command object in the web layer. In one case, this command object is backing a form that represents a partial update of the domain object.

@RequestMapping( value = "/club/edit", method = RequestMethod.GET )
public String setupEditClubForm( ModelMap model, @RequestParam( "clubId" ) Long clubId ) {
    Club club = clubService.findClubById( clubId );
    model.addAttribute( "club", club );
    model.addAttribute( "action", "edit" );
    return "clubForm";
}

@RequestMapping( value = "/club/edit", method = RequestMethod.POST )
public String processEditClubForm( ClubEntity club, BindingResult result ) {
    if ( result.hasErrors() ) {
        return "clubForm";
    }
    clubService.updateClub( club );
    return "redirect:/club/" + club.getId();
}

My problem is that the domain object has some fields that are not changed by submitting this form. These fields that don't have the corresponding request parameters become null, I need them to stay as they are.

I though that this could be fixed by putting the object in the session (via @SessionAttributes) to allow it to live between the two requests, but it doesn't work.

I looked in the Spring reference, but I couldn't find any information on how Spring manipulates the command objects.

6
  • 1
    is there any reason one is a club and one is a clubentity? maybe try using the same interface for both. Commented Oct 18, 2010 at 2:58
  • Good point! I'm not sure why I used the ClubEntity type in the second method, but when I changed it to the interface type Club, Spring complained about being unable to instantiate an interface and I realized my mistake! I'm actually creating a new command object in the processEditClubForm method! Thanks for pointing this out! Commented Oct 18, 2010 at 7:05
  • Just a note on your @RequestParam( "clubId" ) Long clubId. You may want to have the type be String then do a Long.parseLong(blah) in your code so you catch any NumberFormatExceptions Commented Oct 18, 2010 at 14:28
  • dwb: That's a very good advice, thanks! I haven't thought of that. Commented Oct 19, 2010 at 15:03
  • Sorry, I was curious. What did the problem turn out to be (for the question you just deleted)? Commented Apr 22, 2013 at 19:40

1 Answer 1

2

Well, the problem was simple - I was actually creating a new command object in the processEditClubForm method. Here is the correct method code:

@RequestMapping( value = "/club/edit", method = RequestMethod.POST )
public String processEditClubForm( @ModelAttribute Club club, BindingResult result ) {
    if ( result.hasErrors() ) {
        return "clubForm";
    }
    clubService.updateClub( club );
    return "redirect:/club/" + club.getId();
}

Thanks to Daniel for making me see it :-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.