File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Probably Easy Struts2 Question - SessionAware Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Probably Easy Struts2 Question - SessionAware " Watch "Probably Easy Struts2 Question - SessionAware " New topic
Author

Probably Easy Struts2 Question - SessionAware

Aaron Wilt
Ranch Hand

Joined: Sep 26, 2001
Posts: 49
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

Joined: Sep 26, 2001
Posts: 49
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
Wes Wannemacher
Greenhorn

Joined: Jul 07, 2007
Posts: 13
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


Discussion of Java, Struts, Spring, Hibernate, etc. <a href="http://www.wantii.com" target="_blank" rel="nofollow">www.wantii.com</a>
Wes Wannemacher
Greenhorn

Joined: Jul 07, 2007
Posts: 13
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.
Nick Williamson
Ranch Hand

Joined: Jan 06, 2007
Posts: 73
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
Vishal Sinha
Greenhorn

Joined: Jul 17, 2008
Posts: 6
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Probably Easy Struts2 Question - SessionAware