Developing Struts PlugIn

This article shows you how to develop custom Struts PlugIn and incorporate in
your Struts Web Applications. After completing this tutorial you will be able to
create your own custom PlugIn for your web application. Struts PlugIn allows
the programmer to enhance their web applications. There are many PlugIns available for struts e.g. Struts Tiles PlugIn, Struts Hibernate PlugIn,
Struts Spring PlugIn etc. Beside these available PlugIn you can create
your own PlugIn.
Understanding PlugIn
Struts PlugIns are configured using the <plug-in>
element within the Struts configuration file. This element has only one valid
attribute, 'className', which is the fully qualified name of the Java class
which implements the org.apache.struts.action.PlugIn interface.
For PlugIns that require configuration themselves, the nested <set-property>
element is available.
The plug-in tag in the struts-config.xml file is used to declare the PlugIn
to be loaded at the time of server start-up. Following example shows how to
declare the Tiles PlugIn:
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property
property="definitions-config"
value="/WEB-INF/tiles-defs.xml"/>
</plug-in>
The above declaration instructs the struts to load and initialize the Tiles
plugin for your application on startup.
Writing Struts PlugIn Java Code
In this example we write HelloWorld Struts PlugIn example that will give you
idea about creating, configuring and checking Struts PlugIn. Our HelloWorld
Stuts PlugIn contains a method called Say Hello, which simply returns HelloWorld
message.
Here is code of HelloWorld Struts Plugin:
package roseindia.net.plugin;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.struts.action.PlugIn;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.config.ModuleConfig;
/**
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email roseindia_net@yahoo.com
*/
public class HelloWorldStrutsPlugin implements PlugIn {
public static final String PLUGIN_NAME_KEY
= HelloWorldStrutsPlugin.class.getName();
public void destroy() {
System.out.println("Destroying Hello World PlugIn");
}
public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {
System.out.println("Initializing Hello World PlugIn");
ServletContext context = null;
context = servlet.getServletContext();
HelloWorldStrutsPlugin objPlugin = new HelloWorldStrutsPlugin();
context.setAttribute(PLUGIN_NAME_KEY, objPlugin);
}
public String sayHello(){
System.out.println("Hello Plugin");
return "Hello Plugin";
}
}
|
Configuring PlugIn
To configure the plugin add the following line your struts-config.xml file.
<plug-in className="roseindia.net.plugin.HelloWorldStrutsPlugin">
</plug-in>
Calling PlugIn From JSP Page
Here is the code for calling our PlugIn from jsp page.
<%@page contentType="text/html" import="java.util.*,roseindia.net.plugin.*" %>
<%
ServletContext servletContext = this.getServletContext();
HelloWorldStrutsPlugin plugin= (HelloWorldStrutsPlugin) servletContext.getAttribute
(HelloWorldStrutsPlugin.PLUGIN_NAME_KEY);
String strMessage = plugin.sayHello();
%>
Message From Plugin: <%=strMessage%>
|
Building and Testing
Use ant tool to build the application and then deploy on the server. Enter
the url http://localhost:8080/strutstutorial/pages/plugin.jsp
in your browser. It display "Hello Plugin" message. Your server
console also should display "Hello Plugin" message.
In this section we learnt how to develop simple struts plugin, configure,
deploy and test.

|
Current Comments
15 comments so far (post your own) View All Comments Latest 10 Comments:This is a great site. Great job !
Posted by James Huang on Thursday, 01.31.08 @ 05:22am | #46668
How to deploy the struts Application in eclipse3.0 .
Posted by periasamy on Monday, 01.21.08 @ 22:46pm | #45806
this is usefull and for general introduction.
A more comprehensive treatment is needed on this topic
Posted by Vijay on Saturday, 01.5.08 @ 11:10am | #44552
Hi Friends,
I want code to connect mySQL database with Netbeans 5.5.1 struts(1.2.9) with tomcat 5.5.x
anyone help me ?????
Posted by Ghanshyam on Thursday, 11.1.07 @ 18:34pm | #35328
Very nice site for beginers, I had almost given up learning struts when I discovered RoseIndia.
Now I can say that I am aware of fundamentals
Posted by Vijay on Tuesday, 08.28.07 @ 14:55pm | #24328
This is really very good but need more explanation and examples from easier to harder.
Posted by Indubhusan on Wednesday, 04.11.07 @ 19:37pm | #13892
Thanks.It is so easy to create a struts-plugin according to the explaination given.
Posted by Arvind Yadav on Friday, 04.6.07 @ 17:39pm | #13469
hi this site is too good for struts beginners, i have learnt many things from this site.
one more thing i need to ask is after uploading the file i need to send the file to some email address how do i do in struts? kindly send information regarding this
Posted by shankar guru on Thursday, 03.29.07 @ 12:08pm | #12896
Java code inside JSP Page?.. bad.. very bad..
Posted by nrko on Sunday, 03.18.07 @ 22:42pm | #12131
nice page for beginners
Posted by Sushil Yadav on Friday, 03.16.07 @ 08:59am | #11885