• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get ServletContext in struts 2 action class

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to implement the interface ServletContextAware, this way I think you will can managed it.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
You should impliment the org.apache.struts2.util.ServletContextAware
Rest is clear. Hope this Helps.

Cheers
Aneesh
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you implemented ServletContextAware did you write the setServletContext() method?
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic