Solved

Spring\Struts2 HttpServletRequest injection?

Posted on 2008-06-15
8
2,609 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

930 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

10 Experts available now in Live!

Get 1:1 Help Now