Dynamically setting the template directory in an Action
Hi all, Referring to this page: http://struts.apache.org/2.2.1/docs/selecting-template-directory.html It says the template directory can be specified in the session, for example to allow the user to change it on a per-session basis. I've been trying to do this for my application, but can't get it to work. I'm using the conventions library. If I copy WEB-INF/content to WEB-INF/content2 I can add a tag like @ResultPath(value=/WEB-INF/content2/) to an Action to change the template directory used to render the results, and this works as expected. However, that works on a per action/class basis, and I want to do it on a per session basis. I've tried setting templateDir in the session but this has no effect. Any ideas? For example, I've tried this in an action implementing the ServletRequestAware interface: public void setServletRequest(HttpServletRequest arg0) { HttpSession sess = arg0.getSession(true); sess.setAttribute(templateDir, /WEB-INF/content2/); } I also tried implementing SessionAware, eg: public void setSession(MapString, Object session) { session.put(templateDir, /WEB-INF/content2/); } - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Struts 2 Initialization Plugin
In struts 1, I used org.apache.struts.action.PlugIn as a way to create an object at web app startup and put it into the application context so that all sessions had access to it. What's the equivalent method in Struts2? That is, how can I have an object created at web application startup. I guess the alternative is lazy initialization (first guy who tries to access it and doesn't find it, creates it, and saves it into the app context; down side is I might end up with several sessions trying to create it until one finally makes it to the app context). Any better solutions?
Re: Struts 2 Initialization Plugin
You should have an initialization servlet run at startup that can create such an object. Scott Smith ssm...@mainstreamdata.com 11/10/2011 3:06 PM In struts 1, I used org.apache.struts.action.PlugIn as a way to create an object at web app startup and put it into the application context so that all sessions had access to it. What's the equivalent method in Struts2? That is, how can I have an object created at web application startup. I guess the alternative is lazy initialization (first guy who tries to access it and doesn't find it, creates it, and saves it into the app context; down side is I might end up with several sessions trying to create it until one finally makes it to the app context). Any better solutions? - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Struts 2 Initialization Plugin
Look into the ServletContextListener interface. It's a nice way to initialize/dispose of one-time resources in any web app, Struts or not. (*Chris*) On Thu, Nov 10, 2011 at 12:06 PM, Scott Smith ssm...@mainstreamdata.comwrote: In struts 1, I used org.apache.struts.action.PlugIn as a way to create an object at web app startup and put it into the application context so that all sessions had access to it. What's the equivalent method in Struts2? That is, how can I have an object created at web application startup. I guess the alternative is lazy initialization (first guy who tries to access it and doesn't find it, creates it, and saves it into the app context; down side is I might end up with several sessions trying to create it until one finally makes it to the app context). Any better solutions?
Re: Struts 2 Initialization Plugin
You could take advantage of struts dependency injection [1]. You can use servlet filter or something else. Generally this kind of things are easy to realize thank to the interceptors facility. [1] http://struts.apache.org/2.x/docs/bean-configuration.html Maurizio Cucchiara On 10 November 2011 21:09, Eric Reed ere...@mail.nysed.gov wrote: You should have an initialization servlet run at startup that can create such an object. Scott Smith ssm...@mainstreamdata.com 11/10/2011 3:06 PM In struts 1, I used org.apache.struts.action.PlugIn as a way to create an object at web app startup and put it into the application context so that all sessions had access to it. What's the equivalent method in Struts2? That is, how can I have an object created at web application startup. I guess the alternative is lazy initialization (first guy who tries to access it and doesn't find it, creates it, and saves it into the app context; down side is I might end up with several sessions trying to create it until one finally makes it to the app context). Any better solutions? - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Struts 2 Initialization Plugin
I think lazy initialization is a simple choice, because it is not dependent on the J2EE container, this make your code easy to test. If you worry about duplicated-initialization triggered by multi request, the simple solution is, make your initialization code [synchronized], using reserved word [synchronized] of Java language. However, if your initialization process is too heavy, the lazy initialization mode will make the first access slow. If you care about this problem, the [ServletContextListener] and [initialization servlet] is a better choice. As Eric and Chris said, they can be good entry point to call your initialization process. I suggest: (1)implement your initialization process as a util-class or something, so you can do unit test, out of the J2EE context. (2)call the initialization process from [ServletContextListener], or [initialization servlet], or some lazy initialize code as you want. The basic principle is: (1)implement the real business logic in an independent mode (2)create some adapter code, to connect your business logic and your run time context 2011/11/11 Scott Smith ssm...@mainstreamdata.com: In struts 1, I used org.apache.struts.action.PlugIn as a way to create an object at web app startup and put it into the application context so that all sessions had access to it. What's the equivalent method in Struts2? That is, how can I have an object created at web application startup. I guess the alternative is lazy initialization (first guy who tries to access it and doesn't find it, creates it, and saves it into the app context; down side is I might end up with several sessions trying to create it until one finally makes it to the app context). Any better solutions? - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Struts 2 Initialization Plugin
The better solution is dependency injection with Spring, use the struts2-spring-plugin. On Thu, 2011-11-10 at 15:09 -0500, Eric Reed wrote: You should have an initialization servlet run at startup that can create such an object. Scott Smith ssm...@mainstreamdata.com 11/10/2011 3:06 PM In struts 1, I used org.apache.struts.action.PlugIn as a way to create an object at web app startup and put it into the application context so that all sessions had access to it. What's the equivalent method in Struts2? That is, how can I have an object created at web application startup. I guess the alternative is lazy initialization (first guy who tries to access it and doesn't find it, creates it, and saves it into the app context; down side is I might end up with several sessions trying to create it until one finally makes it to the app context). Any better solutions? - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Struts 2 Initialization Plugin
The better solution is dependency injection with Spring, use the struts2-spring-plugin. On Thu, 2011-11-10 at 15:09 -0500, Eric Reed wrote: You should have an initialization servlet run at startup that can create such an object. Scott Smith ssm...@mainstreamdata.com 11/10/2011 3:06 PM In struts 1, I used org.apache.struts.action.PlugIn as a way to create an object at web app startup and put it into the application context so that all sessions had access to it. What's the equivalent method in Struts2? That is, how can I have an object created at web application startup. I guess the alternative is lazy initialization (first guy who tries to access it and doesn't find it, creates it, and saves it into the app context; down side is I might end up with several sessions trying to create it until one finally makes it to the app context). Any better solutions? - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org