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 :
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.
- public class WelcomePlugin implements PlugIn {
- public static final String PLUGIN_NAME_KEY = WelcomePlugin.class.getName();
- public void init(ActionServlet actionServlet, ModuleConfig config) throws ServletException {
-
- System.out.println(“Initializing PlugIns…”);
- ServletContext contextObj = null;
- contextObj = actionServlet.getServletContext();
- WelcomePlugin welcomeObj = new WelcomePlugin();
- contextObj.setAttribute(PLUGIN_NAME_KEY, welcomeObj);
- }
- public void destroy() {
-
- System.out.println(“Destroying PlugIn”);
- }
- public String displayMessage() {
- System.out.println(“Welcome to PlugIn”);
- return“ Welcome to PlugIn”;
- }
- }
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.
- <struts-config>
- <plug-in className=’’com.example.util.WelcomePlugin’’ >
- <!—you may use set-property tag to set properties here-->
- </plug-in>
- </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.
- <%@page contentType=”text/html” import=”java.util.*”, com.app.struts.*” %>
- <!DOCTYPE HTML PUBLIC “…//W3C//DTD HTML5 //EN” “http://www.w3.org/html5/abc.dtd”>
- <html>
- <head>
- <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8”>
- <title>My JSP Page</title>
- </head>
- <body><%
- ServletContext servletContext=this.getServletContext();
- HelloWorldStrutsPlugin plugin=(HelloWorldStrutsPlugin) servletContext.getAttribute(HelloWorldStrutsPlugin.PLUGIN_NAME_KEY);
- String strMessage=plugin.sayHello();
- %>
- <h2> Message From Plugin : <%=strMessage%>
- </h2>
- </body>
- </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().