• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Probably Easy Struts2 Question - SessionAware

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what I'm doing wrong... but I have a feeling I'm making a stupid error I cannot find. I'm trying to use the SessionAware interface to give access to the Session object in my Struts 2 action class.

When I inspect the session map in the execute() method, it's an empty Map (non-null). Shouldn't this map contain session context information in addition to the any session attributes, such as the host, server name, port, etc ? Or does it only hold attributes?

Thanks for any help you can provide.

Here's my struts.xml and package.xml:


Here's my Action class:



[ July 12, 2007: Message edited by: Aaron Wilt ]
[ July 12, 2007: Message edited by: Aaron Wilt ]
 
Aaron Wilt
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured out what I was doing wrong... I was expecting too much from the session map. Somehow I got confused into thinking that the session map had request data in it. dumb mistake.

What I really should have done was implement ServletRequestAware, which allows me to get a hold of the HttpServletRequest, which of course is the same request object as struts 1, which has the data that I needed.

Hopefully this post will save someone some trouble in the future. None of the docs I've read on struts2 spend much time talking about ServletRequestAware, which is why I probably made this oversight.

At least my code works now
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem with ServletRequestAware is that your action is now dependent on the JSP/Servlet API. Writing a jUnit test is going to be very difficult. In your situation, I would ask the following - What is it exactly that you need out of the session object and is there another way to get it? Next, I would ask whether or not you might be better off placing this logic in an interceptor... It would seem that you may be trying to get this sort of info just to keep track of where a user came from, etc. By placing this logic in an interceptor, you could then configure it to work on any number of actions.

-Wes
 
Wes Wannemacher
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another thing...

I am pretty sure that servlet-config interceptor is in the default stack. It looks like your package extends "struts-default," so unless you changed the struts-default.xml in struts2-core-2.x.jar (which I would recommend against), then your attempt to insert the interceptor in your action configuration is redundant.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
testing wont be that hard, you can include spring and use their mockhttpservletresponse, they have a bunch of mock objects that are really good for junit testing with j2ee dependencies. And truthfully (not defending it, or saying you shouldn't), but how many people unit test their actions, much less anything they write. A
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
I am really breaking my head to get session object in my action class.
When I login to a certain portal, the portal sets the user name to the session object. Now on the portal user clicks on my application which is written in struts2. When I get the session map using sessionaware interface, I get an empty map.
I also tried using ServeletrequestAware interfacae to get httprequest and called getSession() on it. Still I am getting empty session.

Can someone tell me how can I get the session which contains username set by the portal? Do I need to make any changes to struts-config.xml for getting the session.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic