A Plugin is defined as the component which are easily integrated with the applications and helps to enhance the functionality of the system. A plug-in is any Java class that is initialized when struts application starts up and destroy when application shuts down. We have already seen many plugin available for the struts like validations, tiles etc. The struts framework also gives the developer flexibility to create their own plugin classes and then implement in the struts applications.
Example For Custom Plugins :-
(1) Creating the Plugin Class
The struts provide org.apache.struts.action.PlugIn interface. All the custom Plugin should implement the PlugIn interface and override the init() and destroy() methods.
package com.visualbuilder.plugin;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet; import org.apache.struts.action.PlugIn; import org.apache.struts.config.ModuleConfig;
public class CustomPlugin implements PlugIn { public CustomPlugin() { super(); }
public void destroy() {
System.out.println("The plug in Destroyed"); }
public void init(ActionServlet controller, ModuleConfig config) throws ServletException { controller.getServletContext().setAttribute("testPlugin","Testing for the object"); System.out.println("The plug in initialized"); }
}
(2) Struts-config.xml file
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <!-- ================================================ Form Bean Definitions --> <form-beans></form-beans> <!-- ========================================= Global Exception Definitions --> <global-exceptions></global-exceptions> <!-- =========================================== Global Forward Definitions --> <global-forwards></global-forwards> <!-- =========================================== Action Mapping Definitions --> <action-mappings> <action path="/plugin" type="com.visualbuilder.TestPluginAction" input="/index.jsp" > <forward name="SUCCESS" path="/index.jsp"/>
</action> </action-mappings> <!-- ======================================== Message Resources Definitions --> <message-resources parameter="MessageResources" /> <!-- ======================================== Custom Plugin Definitions --> <plug-in className="com.visualbuilder.plugin.CustomPlugin"></plug-in> </struts-config>
|