Re: [s2] getting servletContext in actions
hi Joe, Implementing the ResourceLoaderAware interface has solved my problem. many thanks! i havent tried to use ServletContextAware but I think it will work equally well. many thanks Jeff On 31 Jan 2007, at 16:38, Joe Germuska wrote: Jeff: I think there are a lot of possible routes, but I'm not sure I understand your goal. Could you just have your service implement ResourceLoaderAware? If your Spring ApplicationContext is a WebApplicationContext (as it would be if set up by ContextLoaderListener), then the ResourceLoader would check the ServletContext for resources in addition to the class path, etc. If you don't want your service to implement a Spring interface, there are some Spring beans that help bridge the gap, briefly name-checked here http://www.interface21.com/news-home/2005/spring-1.1.4-released These mostly just let you apply ServletContext attributes to Spring beans in various ways without binding your code to the Servlet API. My hunch is trusting that the ResourceLoaderAware is the right solution, especially now that I realize that if you're passing in a ServletContextResource, then you've already accepted the Spring API into your service layer. Does that help? Joe On 1/31/07, Jeffrey Hau <[EMAIL PROTECTED]> wrote: Hi Joe, Thanks for the helpful information. I am just wondering is it possible to get the ServletContext object in my spring applicationContext.xml? What i am trying to do now is to create a Resource object (ServletContextResource) and inject into a service class and then use this service class in a struts action. At the moment, I have to pass in the resource path (as a string) into the action, instantiate the ServletContextResource in the action then pass it to the service class. many thanks, Jeff On 31 Jan 2007, at 12:53, Joe Germuska wrote: > You need to make sure that your action is passing through an > interceptor-stack that includes ServletConfigInterceptor > > http://struts.apache.org/2.x/core/apidocs/org/apache/struts2/ > interceptor/ServletConfigInterceptor.html > > > The struts-default package does include this. > > http://struts.apache.org/2.x/docs/struts-defaultxml.html > > Another thing you can do is call the static method > ServletActionContext.getServletContext() but this would be harder > to support > in a unit testing environment. If you implement > ServletContextAware, then > you could provide a mock ServletContext implementation in your unit > test > setup, while you'd have a lot more awkward setup to do to make sure > that > ServletActionContext.getServletContext was prepared to return a usable > value. > > Joe > > > On 1/31/07, Nagraj Rao <[EMAIL PROTECTED]> wrote: >> >> were u actually able to get the servletContext Object thru >> servletContextAware?? I'd tried it but couldn't get it :( >> do we need to do any additional stuff for that?? >> >> - Original Message - >> From: "Jeffrey Hau" <[EMAIL PROTECTED]> >> To: "Struts Users Mailing List" >> Sent: Wednesday, January 31, 2007 4:55 PM >> Subject: [s2] getting servletContext in actions >> >> >> > Hi, >> > If i need to get a servletContext object in my action class, is >> > implementing the ServletContextAware interface the standard way >> of doing >> > this? Is there any other alternatives? >> > >> > thanks, >> > >> > Jeff >> > >> > >> - >> > To unsubscribe, e-mail: [EMAIL PROTECTED] >> > For additional commands, e-mail: [EMAIL PROTECTED] >> > >> >> >> - >> To unsubscribe, e-mail: [EMAIL PROTECTED] >> For additional commands, e-mail: [EMAIL PROTECTED] >> >> > > > -- > Joe Germuska > [EMAIL PROTECTED] * http://blog.germuska.com > > "The truth is that we learned from João forever to be out of tune." > -- Caetano Veloso - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- Joe Germuska [EMAIL PROTECTED] * http://blog.germuska.com "The truth is that we learned from João forever to be out of tune." -- Caetano Veloso - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [s2] getting servletContext in actions
On 1/31/07, Tom Schneider <[EMAIL PROTECTED]> wrote: It would be very easy to create a Spring FactoryBean for this. In this case, couldn't one simply implement Spring's ServletContextAware interface? http://www.springframework.org/docs/api/org/springframework/web/context/ServletContextAware.html Joe -- Joe Germuska [EMAIL PROTECTED] * http://blog.germuska.com "The truth is that we learned from João forever to be out of tune." -- Caetano Veloso
Re: [s2] getting servletContext in actions
Jeff: I think there are a lot of possible routes, but I'm not sure I understand your goal. Could you just have your service implement ResourceLoaderAware? If your Spring ApplicationContext is a WebApplicationContext (as it would be if set up by ContextLoaderListener), then the ResourceLoader would check the ServletContext for resources in addition to the class path, etc. If you don't want your service to implement a Spring interface, there are some Spring beans that help bridge the gap, briefly name-checked here http://www.interface21.com/news-home/2005/spring-1.1.4-released These mostly just let you apply ServletContext attributes to Spring beans in various ways without binding your code to the Servlet API. My hunch is trusting that the ResourceLoaderAware is the right solution, especially now that I realize that if you're passing in a ServletContextResource, then you've already accepted the Spring API into your service layer. Does that help? Joe On 1/31/07, Jeffrey Hau <[EMAIL PROTECTED]> wrote: Hi Joe, Thanks for the helpful information. I am just wondering is it possible to get the ServletContext object in my spring applicationContext.xml? What i am trying to do now is to create a Resource object (ServletContextResource) and inject into a service class and then use this service class in a struts action. At the moment, I have to pass in the resource path (as a string) into the action, instantiate the ServletContextResource in the action then pass it to the service class. many thanks, Jeff On 31 Jan 2007, at 12:53, Joe Germuska wrote: > You need to make sure that your action is passing through an > interceptor-stack that includes ServletConfigInterceptor > > http://struts.apache.org/2.x/core/apidocs/org/apache/struts2/ > interceptor/ServletConfigInterceptor.html > > > The struts-default package does include this. > > http://struts.apache.org/2.x/docs/struts-defaultxml.html > > Another thing you can do is call the static method > ServletActionContext.getServletContext() but this would be harder > to support > in a unit testing environment. If you implement > ServletContextAware, then > you could provide a mock ServletContext implementation in your unit > test > setup, while you'd have a lot more awkward setup to do to make sure > that > ServletActionContext.getServletContext was prepared to return a usable > value. > > Joe > > > On 1/31/07, Nagraj Rao <[EMAIL PROTECTED]> wrote: >> >> were u actually able to get the servletContext Object thru >> servletContextAware?? I'd tried it but couldn't get it :( >> do we need to do any additional stuff for that?? >> >> - Original Message - >> From: "Jeffrey Hau" <[EMAIL PROTECTED]> >> To: "Struts Users Mailing List" >> Sent: Wednesday, January 31, 2007 4:55 PM >> Subject: [s2] getting servletContext in actions >> >> >> > Hi, >> > If i need to get a servletContext object in my action class, is >> > implementing the ServletContextAware interface the standard way >> of doing >> > this? Is there any other alternatives? >> > >> > thanks, >> > >> > Jeff >> > >> > >> - >> > To unsubscribe, e-mail: [EMAIL PROTECTED] >> > For additional commands, e-mail: [EMAIL PROTECTED] >> > >> >> >> - >> To unsubscribe, e-mail: [EMAIL PROTECTED] >> For additional commands, e-mail: [EMAIL PROTECTED] >> >> > > > -- > Joe Germuska > [EMAIL PROTECTED] * http://blog.germuska.com > > "The truth is that we learned from João forever to be out of tune." > -- Caetano Veloso - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- Joe Germuska [EMAIL PROTECTED] * http://blog.germuska.com "The truth is that we learned from João forever to be out of tune." -- Caetano Veloso
Re: [s2] getting servletContext in actions
Hi Tom, I have tried your suggestion, my applicationContex.xml is now defined as, method="getServletContext"/> class="org.springframework.web.context.support.ServletContextResource"> my StrutsServletContextLoader class consists of one method public ServletContext getServletContext() { return ServletActionContext.getServletContext(); } i am getting error from spring complaining missing servlet context. You mentioned ServletActionContext needs to the executed within the context of a webwork request, i think the problem is related to that. Do you know how can i make my StrutsServletcontextLoader be executed within a webwork request? many thanks Jeff On 31 Jan 2007, at 13:52, Tom Schneider wrote: It would be very easy to create a Spring FactoryBean for this. (Checkout the Spring Documentation if that doesn't make sense) Then, to access the servlet context, just do a: ServletContext servletContext = ServletActionContext.getServletContext(); The FactoryBean would allow you to use dependency injection in spring to inject the servlet context into any spring bean. (I'm doing a similar thing with some of the our legacy services and components) You could even convert the ServletContext into a Map to decouple yourself from the servlet API. That way your process beans would only be dependent on a Map instead of a ServletContext. Keep in mind that the ServletActionContext is a thread local, so it must be executed from within the context of a webwork request. Tom Jeffrey Hau wrote: Hi Joe, Thanks for the helpful information. I am just wondering is it possible to get the ServletContext object in my spring applicationContext.xml? What i am trying to do now is to create a Resource object (ServletContextResource) and inject into a service class and then use this service class in a struts action. At the moment, I have to pass in the resource path (as a string) into the action, instantiate the ServletContextResource in the action then pass it to the service class. many thanks, Jeff On 31 Jan 2007, at 12:53, Joe Germuska wrote: You need to make sure that your action is passing through an interceptor-stack that includes ServletConfigInterceptor http://struts.apache.org/2.x/core/apidocs/org/apache/struts2/ interceptor/ServletConfigInterceptor.html The struts-default package does include this. http://struts.apache.org/2.x/docs/struts-defaultxml.html Another thing you can do is call the static method ServletActionContext.getServletContext() but this would be harder to support in a unit testing environment. If you implement ServletContextAware, then you could provide a mock ServletContext implementation in your unit test setup, while you'd have a lot more awkward setup to do to make sure that ServletActionContext.getServletContext was prepared to return a usable value. Joe On 1/31/07, Nagraj Rao <[EMAIL PROTECTED]> wrote: were u actually able to get the servletContext Object thru servletContextAware?? I'd tried it but couldn't get it :( do we need to do any additional stuff for that?? - Original Message - From: "Jeffrey Hau" <[EMAIL PROTECTED]> To: "Struts Users Mailing List" Sent: Wednesday, January 31, 2007 4:55 PM Subject: [s2] getting servletContext in actions > Hi, > If i need to get a servletContext object in my action class, is > implementing the ServletContextAware interface the standard way of doing > this? Is there any other alternatives? > > thanks, > > Jeff > > --- -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --Joe Germuska [EMAIL PROTECTED] * http://blog.germuska.com "The truth is that we learned from João forever to be out of tune." -- Caetano Veloso - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [s2] getting servletContext in actions
It would be very easy to create a Spring FactoryBean for this. (Checkout the Spring Documentation if that doesn't make sense) Then, to access the servlet context, just do a: ServletContext servletContext = ServletActionContext.getServletContext(); The FactoryBean would allow you to use dependency injection in spring to inject the servlet context into any spring bean. (I'm doing a similar thing with some of the our legacy services and components) You could even convert the ServletContext into a Map to decouple yourself from the servlet API. That way your process beans would only be dependent on a Map instead of a ServletContext. Keep in mind that the ServletActionContext is a thread local, so it must be executed from within the context of a webwork request. Tom Jeffrey Hau wrote: Hi Joe, Thanks for the helpful information. I am just wondering is it possible to get the ServletContext object in my spring applicationContext.xml? What i am trying to do now is to create a Resource object (ServletContextResource) and inject into a service class and then use this service class in a struts action. At the moment, I have to pass in the resource path (as a string) into the action, instantiate the ServletContextResource in the action then pass it to the service class. many thanks, Jeff On 31 Jan 2007, at 12:53, Joe Germuska wrote: You need to make sure that your action is passing through an interceptor-stack that includes ServletConfigInterceptor http://struts.apache.org/2.x/core/apidocs/org/apache/struts2/interceptor/ServletConfigInterceptor.html The struts-default package does include this. http://struts.apache.org/2.x/docs/struts-defaultxml.html Another thing you can do is call the static method ServletActionContext.getServletContext() but this would be harder to support in a unit testing environment. If you implement ServletContextAware, then you could provide a mock ServletContext implementation in your unit test setup, while you'd have a lot more awkward setup to do to make sure that ServletActionContext.getServletContext was prepared to return a usable value. Joe On 1/31/07, Nagraj Rao <[EMAIL PROTECTED]> wrote: were u actually able to get the servletContext Object thru servletContextAware?? I'd tried it but couldn't get it :( do we need to do any additional stuff for that?? - Original Message - From: "Jeffrey Hau" <[EMAIL PROTECTED]> To: "Struts Users Mailing List" Sent: Wednesday, January 31, 2007 4:55 PM Subject: [s2] getting servletContext in actions > Hi, > If i need to get a servletContext object in my action class, is > implementing the ServletContextAware interface the standard way of doing > this? Is there any other alternatives? > > thanks, > > Jeff > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --Joe Germuska [EMAIL PROTECTED] * http://blog.germuska.com "The truth is that we learned from João forever to be out of tune." -- Caetano Veloso - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [s2] getting servletContext in actions
Hi Joe, Thanks for the helpful information. I am just wondering is it possible to get the ServletContext object in my spring applicationContext.xml? What i am trying to do now is to create a Resource object (ServletContextResource) and inject into a service class and then use this service class in a struts action. At the moment, I have to pass in the resource path (as a string) into the action, instantiate the ServletContextResource in the action then pass it to the service class. many thanks, Jeff On 31 Jan 2007, at 12:53, Joe Germuska wrote: You need to make sure that your action is passing through an interceptor-stack that includes ServletConfigInterceptor http://struts.apache.org/2.x/core/apidocs/org/apache/struts2/ interceptor/ServletConfigInterceptor.html The struts-default package does include this. http://struts.apache.org/2.x/docs/struts-defaultxml.html Another thing you can do is call the static method ServletActionContext.getServletContext() but this would be harder to support in a unit testing environment. If you implement ServletContextAware, then you could provide a mock ServletContext implementation in your unit test setup, while you'd have a lot more awkward setup to do to make sure that ServletActionContext.getServletContext was prepared to return a usable value. Joe On 1/31/07, Nagraj Rao <[EMAIL PROTECTED]> wrote: were u actually able to get the servletContext Object thru servletContextAware?? I'd tried it but couldn't get it :( do we need to do any additional stuff for that?? - Original Message - From: "Jeffrey Hau" <[EMAIL PROTECTED]> To: "Struts Users Mailing List" Sent: Wednesday, January 31, 2007 4:55 PM Subject: [s2] getting servletContext in actions > Hi, > If i need to get a servletContext object in my action class, is > implementing the ServletContextAware interface the standard way of doing > this? Is there any other alternatives? > > thanks, > > Jeff > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- Joe Germuska [EMAIL PROTECTED] * http://blog.germuska.com "The truth is that we learned from João forever to be out of tune." -- Caetano Veloso - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [s2] getting servletContext in actions
You need to make sure that your action is passing through an interceptor-stack that includes ServletConfigInterceptor http://struts.apache.org/2.x/core/apidocs/org/apache/struts2/interceptor/ServletConfigInterceptor.html The struts-default package does include this. http://struts.apache.org/2.x/docs/struts-defaultxml.html Another thing you can do is call the static method ServletActionContext.getServletContext() but this would be harder to support in a unit testing environment. If you implement ServletContextAware, then you could provide a mock ServletContext implementation in your unit test setup, while you'd have a lot more awkward setup to do to make sure that ServletActionContext.getServletContext was prepared to return a usable value. Joe On 1/31/07, Nagraj Rao <[EMAIL PROTECTED]> wrote: were u actually able to get the servletContext Object thru servletContextAware?? I'd tried it but couldn't get it :( do we need to do any additional stuff for that?? - Original Message - From: "Jeffrey Hau" <[EMAIL PROTECTED]> To: "Struts Users Mailing List" Sent: Wednesday, January 31, 2007 4:55 PM Subject: [s2] getting servletContext in actions > Hi, > If i need to get a servletContext object in my action class, is > implementing the ServletContextAware interface the standard way of doing > this? Is there any other alternatives? > > thanks, > > Jeff > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- Joe Germuska [EMAIL PROTECTED] * http://blog.germuska.com "The truth is that we learned from João forever to be out of tune." -- Caetano Veloso
Re: [s2] getting servletContext in actions
were u actually able to get the servletContext Object thru servletContextAware?? I'd tried it but couldn't get it :( do we need to do any additional stuff for that?? - Original Message - From: "Jeffrey Hau" <[EMAIL PROTECTED]> To: "Struts Users Mailing List" Sent: Wednesday, January 31, 2007 4:55 PM Subject: [s2] getting servletContext in actions Hi, If i need to get a servletContext object in my action class, is implementing the ServletContextAware interface the standard way of doing this? Is there any other alternatives? thanks, Jeff - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]