This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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 ]
Joined: Sep 26, 2001
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.
The problem with ServletRequestAware is that your action is now dependent on the JSP/Servlet API. Writing a jUnittest 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.
Discussion of Java, Struts, Spring, Hibernate, etc. <a href="http://www.wantii.com" target="_blank" rel="nofollow">www.wantii.com</a>
Joined: Jul 07, 2007
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.
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
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com