aspose file tools*
The moose likes Struts and the fly likes How to get ServletContext in struts 2 action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to get ServletContext in struts 2 action class" Watch "How to get ServletContext in struts 2 action class" New topic
Author

How to get ServletContext in struts 2 action class

Sam Chang
Greenhorn

Joined: Mar 22, 2007
Posts: 5
I need the ServletContext from my struts 2 action class, but I cant find any way of getting it.

I couldn't find the getServlet() or getServletConfig() methods like in struts 1, and org.apache.struts2.ServletActionContext.getServletContext() returns me null.

Any help would be great!
sam
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
I'm not too familiar with the struts 2 framework, but if you could explain the problem you are trying to solve, then someone might be able to suggest a different approach, possibly.
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
After doing a bit of reading up on struts 2, it appears it uses a Filter (as the controller I'm assuming).

I hope someone replies to your query, I'm interested in knowing the answer too

[Edit]
If it is possible to get access to the Filter's FilterConfig you could use that to get access to the ServletContext of the Web application
[ March 23, 2007: Message edited by: Sheldon Fernandes ]
Sam Chang
Greenhorn

Joined: Mar 22, 2007
Posts: 5
Well in more detail of why I want the ServletContext is because I want to render some FreeMarker templates within the struts 2 action class (not as the result type). FreeMarker allows me to set the template loading with the three methods:
1. setDirectoryForTemplateLoading(File dir);
2. setClassForTemplateLoading(Class cl, String prefix);
3. setServletContextForTemplateLoading(Object servletContext, String path);
please see http://freemarker.sourceforge.net/docs/pgui_config_templateloading.html for my reasons to wanting to use method 3. But in short, method 1 is out of the question as its a fixed location and since i cant get a hold of ServletContext I'm forced to use method 2 which requires me to put the FreeMarker templates in the classpath specified. Its not a major obstacle but I would like to put the templates in a seperate directory and not in the classpath.

with regards to getting filterConfig, I have little idea on how to do that, and any help on that would be greatly appreciated also

cheers,
sam
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

Well, I'm not sure about Struts2, but since Struts2 is WebWork2, maybe you have to try the following :
1. Implement ServletRequestAware interface (this is in WW2, I don't know its name in Struts2).
2. Declare a HttpServletRequest instance variable and a setter for it in your action.
3. Make sure you use the right interceptor stack.
Again, this is WW2 way.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Also, if you have your action implement "ApplicationAware", you can declare an instance variable that represents the Map of objects associated with the ServletContext. However, as I read your requirement, that's not going to help you, since you need an instance of the ServletContext itself.

It looks like John's method of getting the HttpServletRequest object and then using that to get the ServletContext is your best bet.
[ March 23, 2007: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Marco Faleiros
Greenhorn

Joined: May 14, 2008
Posts: 1
Try to implement the interface ServletContextAware, this way I think you will can managed it.
Aneesh Vijendran
Ranch Hand

Joined: Jun 29, 2008
Posts: 125
Hello All,
You should impliment the org.apache.struts2.util.ServletContextAware
Rest is clear. Hope this Helps.

Cheers
Aneesh


Cheers
Aneesh
Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
Hi, I am hoping there are people still reading this thread,
I follow the last poster's suggestion with implementing ServletContextAware
from org.apache.struts2.util package

I am getting compile error

the error says that my class
is not abstract and does not override abstract method setServletContext(javax.servlet.ServletContext) in org.apache.struts2.util.ServletContextAware


but from the apache struts2 doc,

Apache Struts 2

I don't see how it was an abstract method from the doc, and so be it, how do I overwrite it? can anyone give suggestions?

Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
Ok, I don't know why ServletContextAware didn't work for me but luckily I found another way to get ServletContext

I use org.apache.struts2.ServletActionContext.getServletContext()

and it compile and did what I wanted in my action class

just thought I share that with people here

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

When you implemented ServletContextAware did you write the setServletContext() method?
Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
Hey Mr. Newton, it's nice to see you read this post

No, I didn't overwrite setServletContect(ServletContext context) method from org.apache.struts2.util package

I just have this 2 lines of code



and the compiler freaks out on me, said that my class is not abstract and does not overwrite setServletContext()

I am using 2.0.11.2 version. I sure would like to know why and I look into the documentation, I had no idea from what I saw in documentation that setServletContext() is an abstract method. so if it is, how do I overwrite it? what I meant was, what should be in the method's body? I would have to use some other way (like import other package) to set the ServletContext?

Please let me know what you think, greatly appreciated
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

When a Java class "implements an interface" you have to write the methods expected by the interface.

The ServletContextAware interface defines a single method:

So your class that "implements ServletContextAware" must either (a) define that method, or (b) be declared as an abstract class and defer its concrete implementation to a subclass.

In general, people that write this method just save the context to a local property:

Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
I can always count on your reply to shed some light into things

so my situation is (a) define that method since my class is not abstract

thanks again for your input, I will try it out
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get ServletContext in struts 2 action class
 
Similar Threads
sending the javascript object to struts 2 action class
S2: Actions are not Servlet
Struts 2 EJB injection
How to Access SevletContext Object in Action Class
Accessing ServletContext attribute from Action Class.