0

I really need your help. I'm using my portlet app as usual when suddenly I have to get out of computer. When I return, there's NullPointerException. OK, why's that? Hmm... my session doesn't contain object it should hold. So, I'm probably loosing something but here's how I always looked at SessionAttributes.

I have my Controller annotated as so:

@SessionAttributes({
    SOME_ATTR
})

Then I have a method with following signature:

@Valid @ModelAttribute(SOME_ATTR) SomeObject someObject

And also init method for my session attribute:

@ModelAttribute(SOME_ATTR)
public SomeObject getSomeEmptyObject() {
    return someUtils.createSomeObject();
}

When I debugged my app at this point I found out that:

  • manually looking into the session, there's nothing in it
  • form is properly binded to someObject

So my two big questions are:

  • why doesn't Spring set someObject into session as well?
  • when the session was invalidated, why wasn't getSomeEmptyObject() called to fill the empty space?

Thanks in forward!

Martin D
  • 667
  • 1
  • 10
  • 25
  • Can you confirm that you're trying to access SOME_ATTR from same controller that is annotated with @SessionAttribute({SOME_ATTR})? What is even more important, getSomeEmptyObject is called before first call to handler method of controller. But objects are copied from model to HttpSession only after that first call to handler method finishes and before control is passed to view (JSP). Was this helpful? I'll try to find you some resources about it, to. – Marko Mar 29 '15 at 14:39
  • Here's something : http://www.intertech.com/Blog/understanding-spring-mvc-model-and-session-attributes/ – Marko Mar 29 '15 at 15:16

2 Answers2

0

Have a look at HttpSessionBindingListener and the HttpSessionAttributeListener. If SomeObject is a class that you control, you can implement HttpSessionBindingListener otherwise you will likely need HttpSessionAttributeListener.

This SO posting covers this pretty well.

Community
  • 1
  • 1
Marc
  • 174
  • 4
0

Well, do you set your session attribute in proper form:

@SessionAttributes(value = {"user", "register"})

Thats all that I can think about.

David
  • 131
  • 1
  • 1
  • 6