Reputation: 19854
I am receiving the following error on submitting my form:
org.springframework.web.HttpSessionRequiredException: Session attribute 'rulesForm' required - not found in session
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseSessionRequiredException(AnnotationMethodHandlerAdapter.java:722)
My JSP contains the following:
<form:form id="rulesForm" modelAttribute="rulesForm" action="save.do">
...
</form>
My Controller contains the following:
@Controller
@RequestMapping("/rules")
@SessionAttributes({"rulesForm", "deskForm"})
public class RulesController {
.
.
.
@RequestMapping(value = "/save.do")
public ModelAndView saveRuleAttributesAndRules(@Valid
@ModelAttribute("rulesForm")
RulesFormDTO rulesForm, BindingResult bindingResult, HttpSession session, Principal principal) {
It seems that if I leave my browser open for a while with my form displaying and then I attempt to perform a submit after some time I get this error.
Really what I want to happen in this case is for the new "rulesForm" object to be created...how can I achieve this?
Thanks
Upvotes: 2
Views: 9809
Reputation: 5018
As the Javadoc on @SessionAttribute indicates use of the annotation means you want store the specified model attributes in the session, which means you need to add them to the model first. Spring MVC will not create them.
In other words when you add a @ModelAttribute("rulesForm") controller method argument you're telling Spring MVC to look for it in the model or create a new instance if not found. However if you also add @SessionAttributes, Spring MVC will not attempt to create a new instance and will expect the object to be either in the model or in the session. You can use a @ModelAttribute method to add your object initially.
Upvotes: 6