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:
Parent controller has other methods with @InitBinder, @ExceptionHandler, etc. that are common to all my controllers.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; } ....... }
And my child controller looks something like this:
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"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"; } }
I've tried many combinations but couldn't make it work and I keep getting:
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.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
Thanks.
Any help on this or recommendation is very appreciated.
Thanks,
nicolas.loriente


Reply With Quote