Solved

Spring\Struts2 HttpServletRequest injection?

Posted on 2008-06-15
8
2,637 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

615 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