• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Keep data in session scope

 
Frank Truong
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Spring 2.5.4

In my first Spring-based project, I'm trying to cache my data which is created in controller by keeping it in session, just like the way I would have done in Struts' action. Then I can hopefully refer to this data from any jsp pages. For instance, I would have something like following in the my controller

And then in my jsp page, I would conventionally refer to this list as

But this code doesn't work.

If I return ModelAndView("showPage","list",list), this variable is bound to the request scope. I looked into the debugger and saw it. But I don't know how to keep this variable in the session scope.

My objective is to cache the data, so I can refer to it from any jsp pages.

Please help!
Frank
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try change the scope in your applicationContext. Because by default the session scope is request and thats why your session scope was overtook by request scope.
 
Frank Truong
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajkumar balakrishnan wrote:Try change the scope in your applicationContext. Because by default the session scope is request and thats why your session scope was overtook by request scope.


Please tell me how to do this. I am just a rookie to Spring. I am having a hard time understanding and using the session scope(and others) in Spring. I wish to find some reading materials on scope usage in Spring.

Spring is hard to learn

Thank you.
Frank
 
Mark Secrist
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Frank,
I'm thinking what you need to do is add a <jsp:useBean> definition with a scope="session" to your jsp. I've not tried to do it the way you initially posted but I have used the useBean approach. The key is that the id="xx" attribute must be exactly the same as the session attribute you set in your controller.

Mark

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Secrist wrote:I'm thinking what you need to do is add a <jsp:useBean> definition with a scope="session" to your jsp.
Not necessary.
the key is that the id="xx" attribute must be exactly the same as the session attribute you set in your controller.

This is only appropriate for scripting variables within scriptlets.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Truong wrote:<c:forEach var="elem" items${sessionScope.list} varStatus="status">

Check your syntax. Carefully.
 
Mark Secrist
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear,
Good catch. I'm fairly new to Spring and EL myself and that's how I figured out to solve this problem... just a legacy from my former JSP days.

Thanks,

Mark
 
Frank Truong
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I saw my typo. But it was too late after I hit the 'Submit' button. I should have cut and pasted my code, instead! Otherwise, modern IDEs would have complained about it. I'm not using Notepad...

But that was where my bumper was.

Thanks, all.
Frank

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic