2

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

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Adding a @ModelAttribute method is not what I want either. I want the data that is posted in the form to be mapped to the RulesForm object. How come when my form is submitted it is not performing the binding?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.