So I'm building a an @MVC form controller to perform email stuff. The shell of my controller is listed below and is pretty much a text book example of adding a model attribute on GET and accessing it on POST...and that works fine for things where a user is logged in a has a session...but as a user I would expect to be able to come to this email form, start typing my email, get up and walk away and have everything still be there when I come back 5 hours later. Currently when the session expires, I get the following exception thrown.
org.springframework.web.HttpSessionRequiredExcepti on: Session attribute 'email' required - not found in session
Do I have to use the @SessionAttributes("email") and if not, what will happen if I don't? It appears that as long as my domain object has an empty constructor it will instantiate and return a new object. Can someone verify what is suppose to happen without @SessionAttributes.
Code:@Controller @SessionAttributes("email") public class EmailController { @InitBinder public void initBinder(ServletRequestDataBinder binder) { binder.setAllowedFields(new String[] {"fromName","fromEmail","subject","message"}); binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); } @RequestMapping(value="/email.htm", method=RequestMethod.GET) public String loadForm(Model model) throws Exception { model.addAttribute("email", new Email()); return context; } @RequestMapping(value="/email.htm", method=RequestMethod.POST) public String processForm(HttpServletRequest request, @ModelAttribute("email") final Email email, BindingResult result, SessionStatus status, Model model) { this.validator.validate(email, result); if(result.hasErrors()) { return context; } else { //send email } } }


Reply With Quote
