PlugIn In Struts

Introduction

 
Plugins are strut elements that provide a listener-like capability. Plugins are Strut elements that allow developers to enhance web applications.
 

Understanding Plugins

 
It is a kind of configuration wrapper that wraps around a module-specific resource or service that needs to be notified about application startup and shutdown events. For example, struts support Tiles Plugin that allows creation regions within the Web page. You can also declare the configuration of the application in a tiles configuration file that lies external to the system.
 
There are three ways by which you can extend the Struts Framework. PlugIns is one of them. Struts provide the ability to create your own plugin class if you wish to execute some business logic at application startup or shutdown.
 
To achieve this, simply need to create a class that implements the PlugIn interface. The class implementing PlugIn interface must implement two methods :
  • init()
  • destroy()
The init() method is called when the application starts up whereas the destroy() method is called at shutdown. Struts allow you to pass init parameters to your plugin class. To pass parameters, you have to create setter methods in your plugin class for each parameter.
 
Below is the source code to demonstrate how to create your own plugin class.
  1. public class WelcomePlugin implements PlugIn {  
  2.     public static final String PLUGIN_NAME_KEY = WelcomePlugin.class.getName();  
  3.     public void init(ActionServlet actionServlet, ModuleConfig config) throws ServletException {  
  4.         //init logic    
  5.         System.out.println(“Initializing PlugIns…”);  
  6.         ServletContext contextObj = null;  
  7.         contextObj = actionServlet.getServletContext();  
  8.         WelcomePlugin welcomeObj = new WelcomePlugin();  
  9.         contextObj.setAttribute(PLUGIN_NAME_KEY, welcomeObj);  
  10.     }  
  11.     public void destroy() {  
  12.         //destroy logic    
  13.         System.out.println(“Destroying PlugIn”);  
  14.     }  
  15.     public String displayMessage() {  
  16.         System.out.println(“Welcome to PlugIn”);  
  17.         return“ Welcome to PlugIn”;  
  18.     }  
  19. }   
The above code creates a user-defined plugin named “WelcomePlugin” that implements the interface PlugIn. It has two methods init( ) and destroy ( ) that are called when applications start and end respectively.
 
Once the user has created his plugin class, its existence needs to be informed to Struts. This is known as configuring your plugin. It can be achieved by simply adding a <plug-in> element in the struts configuration file.
 
The following code demonstrates how to declare a new plugin in struts-config.xml.
  1. <struts-config>    
  2.    <plug-in className=’’com.example.util.WelcomePlugin’’ >    
  3.       <!—you may use set-property tag to set properties here-->    
  4.    </plug-in>    
  5. </struts-config>   
This code uses a plug-in element to declare the “WelcomePlugin” that was created above. The attribute className of this tag is fully qualified name of the class implementing the PlugIn interface. One can add a <set-property> element for every initialization parameterwhich is to be passed to the user-defined “WelcomePlugin” class.
 
Once the plugin has been configured it has to be used in a JSP page. The following code demonstrates the use of a user-defined plugin.
  1. <%@page contentType=”text/html” import=”java.util.*”, com.app.struts.*”  %>  
  2. <!DOCTYPE HTML PUBLIC “…//W3C//DTD HTML5 //EN” “http://www.w3.org/html5/abc.dtd”>  
  3. <html>  
  4.     <head>  
  5.         <meta http-equiv=”Content-Type” content=”text/html;  charset=UTF-8”>  
  6.             <title>My JSP Page</title>  
  7.         </head>  
  8.         <body><%    
  9. ServletContext servletContext=this.getServletContext();    
  10. HelloWorldStrutsPlugin plugin=(HelloWorldStrutsPlugin) servletContext.getAttribute(HelloWorldStrutsPlugin.PLUGIN_NAME_KEY);    
  11. String strMessage=plugin.sayHello();    
  12. %>  
  13.             <h2> Message From Plugin : <%=strMessage%>  
  14.             </h2>  
  15.         </body>  
  16.     </html>   

Advantages of Using Plugin

  • Using plugins allow the author to enhance their Web application.
  • The Tiles and Validator frameworks can use PlugIn for initialization by reading configuration files.
  • The plugin class can check the availability of configuration files that are needed for your application. If these files are not available, then it will throw a ServletException . This will result in ActionServlet becoming unavailable.
  • You can utilize its init() method to change something in ModuleConfig which is a collection of static configuration information that describes a Struts-based module. Struts will freeze ModuleConfig once all the plugins are processed.

Summary

 
Plugins are Struts elements that allow developers to enhance Web applications. Struts provide the facility to create your own plugin class if you wish to execute some business logic at application startup or shutdown. The plugin class can check the availability of configuration files that are needed for an application. The plugIn interface must implement two methods, init() and destroy().


Similar Articles