Announcement Announcement Module
Collapse
No announcement yet.
@SessionAttributes and method @ModelAttribute Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @SessionAttributes and method @ModelAttribute

    Cannot create a session after the response has been committed

    Hi everyone,

    I've been having some issues getting @SessionAttributes and method level @ModelAttribute working together.
    Also, I'm open to suggestions for better/cleaner solutions if there is.

    I have a parent controller that looks like this:

    Code:
    @Controller
    @SessionAttributes("userDefaults")
    public class ParentController {	
    	
            @Resource(name="userService")
            private UserService userService;
    
    	@ModelAttribute("userDefaults")
    	public UserDefaults getUserDefaults(@ModelAttribute("userDefaults")UserDefaults userDefaults) {
    		// grab userDefaults from Model
                    // if not initialized initialize it
                    // return either the existing or new userDefaults to make it part of the model
                   // make it a session attribute
    
    		if (userDefaults.getSomeDefaultValue() == null) {
    			userDefaults = userService.getUserDefaults();
    		}
    		
    		return userDefaults;
    	}
            .......
    }
    Parent controller has other methods with @InitBinder, @ExceptionHandler, etc. that are common to all my controllers.

    And my child controller looks something like this:
    Code:
    @Controller
    public class SomeChildController extends ParentController {
    	
    	@RequestMapping(value="/someMapping", method=RequestMethod.GET)
    	public String getUser(@ModelAttribute("userDefaults") User userDefaults) {
                    
                    // use userDefaults to retrieve data
                    .........
    		return "userForm";
    	}
    }
    What am I trying to achieve???? Basically, I want to set "userDefaults" on the session before any @RequestMapping happens so that handler methods can use this session bounded "userDefaults"

    I've tried many combinations but couldn't make it work and I keep getting:

    Code:
    SEVERE: Servlet.service() for servlet [appServlet] in context with path [/IGCService] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: Cannot create a session after the response has been committed] with root cause
    java.lang.IllegalStateException: Cannot create a session after the response has been committed
    By the way.... I do get my response rendered correctly on my view but right after that I get the error as somehow a new session is trying to be created. I'm not seeing why or how or where a new session would be want to be created after the response has been committed.

    Thanks.

    Any help on this or recommendation is very appreciated.

    Thanks,


    nicolas.loriente
    Last edited by nicolas.loriente; Oct 19th, 2012, 12:19 PM.
Working...
X