Dear Wiki user, You have subscribed to a wiki page or wiki category on "Struts Wiki" for change notification.
The following page has been changed by GeorgeDinwiddie: http://wiki.apache.org/struts/StrutsInitialization The comment on the change is: deleted unrelated item; cleaned up formatting ------------------------------------------------------------------------------ Up to StrutsNewFaqs ---- - {{{ How to execute an initialization method before the first (web-)user interaction... }}} + '''How to execute an initialization method before the first (web-)user interaction...''' - = Answers = - + [[TableOfContents]] + ---- - {{{ = #1 Using a plug-in = + = Using a plug-in = Using a plug-in type class I believe should fit the bill here. This class is executed upon application start-up. These values that you call or intialize from whereever can be stored in application state for use by the rest of your @@ -22, +22 @@ You also might want to look at the JavaDocs for the PlugIn interface and read up on that as well. }}} ---- - = #2 Define statics = + = Define statics = - {{{ Define statics (maybe even private with access methods) + Define statics (maybe even private with access methods) in your extended servlet. - in your extended servlet. }}} + {{{ public class yourActionServlet extends { org.apache.struts.action.ActionServlet public void init() throws ServletException { private static HashMap hashmap; - //read XML file into variable hashMap + //read XML file into variable hashMap - } - public static getHashMap(){ }}} + public static getHashMap(){ return hashmap; + } } - } + }}} Then from your actions, simply: - + {{{ yourActionServlet.getHashMap(); + }}} ---- - = #3 Define another servlet entry in web.xml = + = Define another servlet entry in web.xml = {{{ <servlet> <servlet-name> myInit @@ -74, +75 @@ <load-on-startup> 1 </load-on-startup> - </servlet> }}} + </servlet> + }}} ---- - = #4 Use a ContextListener = + = Use a ContextListener = - {{{In web.xml: + In web.xml: - + {{{ <listener> <listener-class>my.app.MyContextListener</listener-class> </listener> - + }}} Then, MyContextListener.java might be: - + {{{ package my.app; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; @@ -99, +101 @@ return myConfigData.get(key); } } - + }}} Then, in your code you can call MyContextListener.getValue("someKey"); to retrieve the value. Naturally, you probably want to do something more complex than this like reading from a config file and creating some "real" configuration holder object, but you get the point :) Remember, you can do whatever you want here... it can be as simple or as complex as you like! Note that there is an existing listener in the Java Web Parts project that will take care of all the details and allow you to just provide a configuration file. It creates a config object for you on-the-fly (and it also has more advanced capabilities if you require more control over things). Details at http://javawebparts.sourceforge.net in the Listener package. - }}} - ---- - = #5 Why does editing web.xml force a reload? = - {{{ - This is the default behaviour of a host as defined in server.xml. To disable this behaviour the host needs both autoDeploy="false" and liveDeploy="false" in its definition. For example - - <Host name="localhost" debug="0" appBase="/path/to/app" autoDeploy="false" liveDeploy="false"> - ... - </Host> - - }}} - ---- Up to StrutsNewFaqs --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]