Announcement Announcement Module
Collapse
No announcement yet.
Setting session attributes in POST method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Setting session attributes in POST method

    I'm using the following code to set a couple of session attributes in a POST method:
    Code:
    @RequestMapping(method = RequestMethod.POST)
    public String processSubmit(HttpSession session, Model model,
    		@ModelAttribute EmailForm form, BindingResult result, 
    		@ModelAttribute("emailAddresses") Set<String> emailAddresses) {
    	new EmailFormValidator().validate((EmailForm)form, result);
    	if (result.hasErrors()) {
    		return "email";
    	} else {
    		Set<String> sent = new TreeSet<String>(emailTasks.sendEmails(emailAddresses, form
    					.getSubject(), form.getContent()));
    		Set<String> notSent = new TreeSet<String>();
    		notSent.addAll(emailAddresses);
    		notSent.removeAll(sent);
    		session.setAttribute("sent", sent);
    		session.setAttribute("notSent", notSent);
    		return "redirect:emailsent.htm";
    	}
    }
    The controller class is annotated with @SessionAttributes("emailAddresses"). This all works fine. However, if I try instead to add the "sent" and "notSent" attributes to the model and annotate the contoller class with @SessionAttributes({"emailAddresses", "sent", "notSent"}) the code (below) executes Ok but produces a null view. I'm curious to know why this.
    Code:
    @RequestMapping(method = RequestMethod.POST)
    public String processSubmit(HttpSession session, Model model,
    		@ModelAttribute EmailForm form, BindingResult result, 
    		@ModelAttribute("emailAddresses") Set<String> emailAddresses) {
    	new EmailFormValidator().validate((EmailForm)form, result);
    	if (result.hasErrors()) {
    		return "email";
    	} else {
    		Set<String> sent = new TreeSet<String>(emailTasks.sendEmails(emailAddresses, form
    				.getSubject(), form.getContent()));
    		model.addAttribute("sent", sent);
    		Set<String> notSent = new TreeSet<String>();
    		notSent.addAll(emailAddresses);
    		notSent.removeAll(sent);
    		model.addAttribute("notSent", notSent);
    		return "redirect:emailsent.htm";
    	}
    }
Working...
X