Expand your Struts
1. Use the
Provide
The PLUGIN interface contains two methods, as follows: / ** * the org.apache.struts.action.plugin interface * / public interface plugin {/ ** * Receive notification That the specified sub-appllicAiton is being * started up. * * / Public void init (ActionServlet Servlet, ApplicationConfig Config) Throws servletexception;
/ ** * Receive Notification That Our OWNING SUB-Application IS Being * Shut Down. * / Public Void Destroy ();} ActionServlet When the Struts application is started, the ActionServlet has passed the init () method instance each plugin, Struts supports one or Multiple plugins, if your application takes multiple modules, and can extend to the Sub-Application: Struts1.1, I will introduce this content in the subsequent article). Once you have implemented Struts's Plugin interface, you will be able to put something you need to initialize in the init () method. (Here, it will be a very good place to "place" database connection, of course, initialization database connections can also pass Datasource.), For Destory () methods I think you will understand that it will call at the end of your app, here you You can close the database connection initialized in the init () method.
2. Add your Plugin in Struts-Config.xml
This will be very simple. As follows:
[Information: "Programming Jakarta Struts"]
Below I will give a simple example of connecting pools with plugin. package com.ifreeway.rms.v12.action.Plugins; import java.io.File; import java.util.List; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax .servlet.ServletException; import org.apache.struts.action.ActionServlet; import org.apache.struts.action.PlugIn; import org.apache.struts.config.ModuleConfig; import org.jdom.Document; import org.jdom. Element; import org.jdom.input.saxbuilder;
Import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory;
/ *** Class or Interface discrtion * @Author $ author: jack $ * @version $ revision: 1.0 $ * $ ID: INITDATASOURCE.JAVA 2003-5-7 10:03:31 jack exp. * / public class InitDataSource implements PlugIn {static String contextFactory = "com.sun.jndi.rmi.registry.RegistryContextFactory"; private Context ctx = null;. / *** Commons Logging instance * / private static log log = LogFactory.getLog (InitDataSource .class); / *** Receive notification that the specified sub-applicaiton is being * started up * * / public void init (ActionServlet servlet, ModuleConfig config) throws ServletException {try {// parse WEB-INF / database.xmlString. DBFILE = servlet.getServletContext (). getRealPath ("/ web-inf / data.xml"); File File = New File (DBFILE);
Saxbuilder Builder = new saxbuilder ();
Document doc = builder.build (file);
Element rootelm = doc.getrootelement (); element Normalelm = rootelm.getchild ("host"); element dbuserelm = rootelm.getchild ("user");
List dbuserlist = dbuserelm.getChildren (); element dbuser = null;
For (int i = 0; i I add PLUGIN's code in Struts-Config.xml. You can do not necessarily understand the specific implementation of this example, just understand this expansion mechanism of Struts. This article was originally posted on the struts area of CJW (http://www.chinajavaworld.net) JPLATEAU 2003-05-09