扩展你的STRUTS

王朝java/jsp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

扩展你的STRUTS

1.使用<Plugin>机制

在struts1.1中利用新增的org.apache.struts.action.PlugIn 接口提供<Plugin>扩展机制。

任何的JAVA类都可以当作你自己的PLUGIN,只要该类实现前面提到的PlugIn接口。

PlugIn接口包含两个方法,如下:

/**

*The org.apache.struts.action.PlugIn Interface

*/

public interface PlugIn {

/**

* Receive notification that the specified sub-applicaiton 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();

}

在struts应用程序启动的时候,ActionServlet通过init()方法实例每一个Plugin,struts支持一个或多个

Plugin,如果你的应用程序采取多模块的话,并且可以延伸到“子应用模块”(sub-application:struts1.1新增

功能,此类内容我会在以后的文章中介绍)。

一旦你实现了struts的PlugIn接口,你将可以在init()方法中放一些需要初始化的东西。(这里将是一个

非常好的“安放”数据库连接的地方,当然初始化数据库连接也可以通过datasource。),对于destory()方法我

想你一定明白它将在你的应用程序结束时调用,这里你可以关闭你在init()方法中初始化的数据库连接等等。

2.在struts-config.xml中添加你的plugin

这将是非常简单的工作。如下代码:

<plug-in

className="com.ifreeway.rms.v12.action.Plugins.InitDataSource"/>

[资料:《Programming Jakarta Struts》]

下面我将给出一个简单的用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 Discription

* @author $Author:jack$

* @version $ReVision:1.0 $ <br/>

* $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 {

//解析WEB-INF/database.xml

String dbfile =

servlet.getServletContext().getRealPath(

"/WEB-INF/database.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 < dbUserList.size(); i++) {

dbuser = (Element) dbUserList.get(i);

Context ctx = new InitialContext();

ctx.addToEnvironment(

javax.naming.Context.INITIAL_CONTEXT_FACTORY,

contextFactory);

//DriverManager.setLogStream(System.out); // to create more info

// for technical support

// create a DataSource with pooling

com.inet.tds.PDataSource ds = new com.inet.tds.PDataSource();

ds.setServerName(normalElm.getChildText("servername"));

ds.setDatabaseName(dbuser.getAttributevalue("dbname"));

ds.setUser(dbuser.getAttributevalue("username"));

ds.setPassword(dbuser.getAttributevalue("password"));

ds.setLoginTimeout(10);

ds.setMaxPoolSize(6); // default 0

ds.setInitialPoolSize(5); // default 0

ds.setMinPoolSize(3); // default 0

ds.setMaxIdleTime(300); // PLEXA default 600

ds.setDescription("A Test Data Source");

//ds.setSqlServer7Modus( false );

ctx.bind(dbuser.getAttributevalue("JNDIName"), ds);

}

} catch (Exception e) {

log.info(e);

}

}

/**

* Receive notification that our owning sub-application is being

* shut down.

*/

public void destroy() {

try {

if (ctx != null) {

ctx.close();

ctx = null;

}

} catch (NamingException ex) {

ex.printStackTrace();

}

}

}

[该例中除了struts中常用的jar以外还要包括:jdom.jar providerutil.jar rmiregistry.jar jndi.jar]

我在struts-config.xml中添加plugin的代码同上。

你可以不必理会该例的具体实现,只要理解Struts的这一扩展机制即可。

这篇文章最初是发贴在CJW(http://www.chinajavaworld.net)的struts专区

Jplateau 2003-05-09

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航