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.
Joined: Aug 18, 2004
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 ]
Joined: Mar 22, 2007
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
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.
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 ]
When you implemented ServletContextAware did you write the setServletContext() method?
Joined: Aug 05, 2007
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 18.104.22.168 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