Solved

Spring\Struts2 HttpServletRequest injection?

Posted on 2008-06-15
8
2,607 Views
Last Modified: 2013-11-24
I am planning on integrating my Struts2 application with Spring's IoC container. The only thing that is stopping me is that I can't seem to find out how to inject the HttpServletRequest, HttpServletResponse, and HttpSession into a bean with servlet or request scope.  Is this even possible?
0
Comment
Question by:leobaz2
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 21789941
Don't think you can, why do u need to?

0
 

Author Comment

by:leobaz2
ID: 21790225
Some of my beans are going to be adding cookies and looking at the request and response.  I don't want to have to pass the request and response to the beans as method parameters.  Here is what I was thinking of doing:

I'll have a request scoped bean called something like MyWebContext that has setters for the request and response.  I'll have a struts2 intercepter that will get the bean from the Spring IoC and then set the request and responses.  When the other beans need the request or reponse, I will use DI to get MyWebContext and call the appropriate getter. I think this is the best and easiest way to do it.
0
 
LVL 8

Accepted Solution

by:
malfunction84 earned 63 total points
ID: 21790254
Yes, this is possible.  Your Action class can implement the following interfaces:

org.apache.struts2.interceptor.SessionAware
org.apache.struts2.interceptor.ServletRequestAware
org.apache.struts2.interceptor.ServletResponseAware

Each of these interfaces contains a single setter method (which is called by the ServletConfigInterceptor) that serves as the entry point for the objects you want.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 92

Expert Comment

by:objects
ID: 21790261
They don't sound like request scope beans, how r they defined?
0
 

Assisted Solution

by:srikanthradix
srikanthradix earned 62 total points
ID: 21798471
http://www.ibm.com/developerworks/web/library/j-sr2.html
http://cwiki.apache.org/WW/understanding-interceptors.html Look at the bottom you will find how to write Custom Interceptors.
May be these links can help you.
0
 

Author Comment

by:leobaz2
ID: 21803833
I've written custom intercepters before.  I think my technique might work.  I will have a request scoped bean that my intercepter will put the request and response in for each http request.  Later in the processing of the request, my code can get that request scoped bean and get the request and response.  This will allow singleton scoped beans to use the request, response, and session without having these objects passed throw method parameters.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now