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.
- public class WelcomePlugin implements PlugIn {
- public static final String PLUGIN_NAME_KEY = WelcomePlugin.class.getName();
- public void init(ActionServlet actionServlet, ModuleConfig config) throws ServletException {
- //init logic
- System.out.println(“Initializing PlugIns…”);
- ServletContext contextObj = null;
- contextObj = actionServlet.getServletContext();
- WelcomePlugin welcomeObj = new WelcomePlugin();
- contextObj.setAttribute(PLUGIN_NAME_KEY, welcomeObj);
- }
- public void destroy() {
- //destroy logic
- 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>

Roshan RathodPosted Aug 4, 2020, 12:06 AM
Nice..keep it up