views:

1493

answers:

3

I think im confused a bit about session annotation in spring mvc.

I have code like this (2 steps form sample, step 1 user data, step 2 address)

@SessionAttributes({"user", "address"})
public class UserFormController {

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView show( ModelAndView mv ){
        mv.addObject( new User() );
        mv.addObject( new Address() );
        mv.setViewName("user_add_page");
        return mv;
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processForm( User user, BindingResult result ){
        new UserValidator().validate(user, result);
        if( result.hasErrors() ){
            return "user_add_page";
        }else{
            return "redirect:/user_form/user_add_address";
        }

// .........
}

Now if i submit page after my session expires i get error

org.springframework.web.HttpSessionRequiredException: Session attribute 'user' required - not found in session

How do i handle it? i would like to have 2 options

  1. i create empty objects if missing in session and accept submit
  2. i forward back to user form with some message

Im still in early stage of learning Spring so sorry if its something very obvious, i just cant see it.

ps. is that even the good way to solve this kind of form in spring mvc or would you recomment different approach?

A: 

According to the Spring 3.0 reference manual, it looks like @SessionAttributes is meant to be used on a type that you want to be stored transparently in the session, such as a "Command" or a form-backing object. I don't think you would want to store a Controller in session.

matt b
hymmm ... no i did not mean to store controller in session.My problem is that once your session expires your user and address objects (regular data holding beans) are null.I got around it with @ExceptionHandler(HttpSessionRequiredException.class) public ModelAndView handleException(){ ModelAndView mv = new ModelAndView("redirect:/user_form"); mv.addObject( new User() ); mv.addObject( new Address() ); return mv; }but its not exactly what i had in mind, i was hoping for some cleaner solution
Art79
+1  A: 

1.i create empty objects if missing in session and accept submit

Use @ModelAttribute("user")-annotated method to provide the default value

2.i forward back to user form with some message

Use @ExceptionHandler(HttpSessionRequiredException.class)-annotated method

axtavt
A: 

Try to check here:

http://forum.springsource.org/showthread.php?t=63001&highlight=HttpSessionRequiredException

@Controller
@RequestMapping(value="/simple_form")
@SessionAttributes("command")
public class ChangeLoginController {

  @ModelAttribute("command")
  public MyCommand createCommand() {
    return new MyCommand();  
  }

    @RequestMapping(method = RequestMethod.GET)
    public String get() {       
        return "form_view";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String post(@ModelAttribute("command") MyCommand command) {
        doSomething(command); // execute business logic
        return "form_view";
    }
}
andrew007