We know that when a Web Application is started at the web application, the server calls the server init () function in the web application for initialization. Each Server's basic information and initialization information are configured in /Web-inf/web.xml. If the web application uses the Struts framework, it is equipped with a basic Servervet called Action, which has an initialization file: /web-inf/struts-config.xml. With this file, Action is initialized in the application startup all form-bean, action-mappings, etc. Struts framework information. But sometimes we need to initialize information related to specific applications. For such a demand, the solution is definitely a lot, but the methods mentioned herein should be a good choice.
The method is to extend the Apache Struts's org.apache.struts.Action.ActionSerlvet class, override the init () function, initialize information related to the specific application, then call an ActionServlet to initialize the Struts framework. Next, a simple example is combined to illustrate the specific practices of this method.
Suppose your web application needs to initialize some information, and this information is configured in a text file called DBJH_EN.Properties, its contents are as follows:
Listing 1: dbjh_en.properties profile
# System initialization data item
MailServer = mail.chinabidding.com
Docroot = http://192.168.0.141: 8080 / dbjh_en
WCMIMAGESPATH = http://192.168.0.141: 8080 / WebPic
ActivePath = http://192.168.0.141: 8080 / dbjh_en / main / activ.do
First, of course, you are writing your own Action class MyActionSerlvet, read information in the configuration file dbjh_en.properties. In order to be able to use the Strus framework, the MyActionSerlvet class also needs to expand the org.Apache.Struts.Action.ActionSerlvet class. The code is as follows:
Listing 2: MyActionServlet class
Package com.bidlink.dbjh;
Import org.apache.struts.action.ActionServlet;
Import javax.servlet.servletException;
Import org.apache.commons.logging. *;
Import Java.io.InputStream;
Import java.util.properties;
Import com.bidlink.commons.const;
Public class myActionservlet
Extends actionServlet {
Private log log = logfactory.getlog (this.getclass (). getname ());
Public myActionservlet () {
}
Public void init () throws servletexception {
Log.info
"Initializing, My MyActionServlet Init this system's const variable");
Properties dbprops = new profment ();
Try {
String cfile = getServletConfig (). GetinitParameter ("const"); log.info (cfile);
Log.info (GetServletConfig (). GetInitParameter ("config");
InputStream IS = GetServletContext (). GetResourceAsStream (cfile);
DBProps.Load (IS);
}
Catch (Exception E) {
;
}
Log.info (const.root);
NEW const (dbprops);
Log.info (DBProps.getProperty);
Log.info (DBProps.getProperty ("DOCROOT");
LOG.INFO ("Initializing, End My Init);
Super.init ();
}
}
Note that the Const class is the initialization information used to save the configuration file DBJH_EN.Properties, which is convenient to obtain configuration data information directly after the system starts initialization. The most important place for your MyActionServlet class is the init () function, which initializes information related to a specific application, then call the ActionServlet's init () function to initialize the Struts framework.
The next step is to modify the configuration file Web.xml for the web application. For this example, it is mainly modified two places. First, modify the name called an action, modify its
Listing 4: Web.xml Action configuration
init-param>
init-param>
init-param>
init-param>
servlet>
At this point, this extension
Org.apache.struts.Action.ArtionsSerlvet class is over. This article only plays a role of throwing jade, I hope to give some friends who are interested.