{"id":189,"date":"2007-09-06T00:00:00","date_gmt":"2007-09-06T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=189"},"modified":"2007-09-06T00:00:00","modified_gmt":"2007-09-06T00:00:00","slug":"Developing Struts PlugIn","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=189","title":{"rendered":"Developing Struts PlugIn"},"content":{"rendered":"<p><DIV><br \/>\n<P>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.<\/P><br \/>\n<P><B>Understanding PlugIn<\/B><\/P><br \/>\n<P>Struts PlugIns are configured using the <CODE>&lt;plug-in&gt;<\/CODE> element within the Struts configuration file. This element has only one valid attribute, &#8216;className&#8217;, which is the fully qualified name of the Java class which implements the org.apache.struts.action.PlugIn interface.<\/P><br \/>\n<P>For PlugIns that require configuration themselves, the nested &lt;set-property&gt; element is available.<\/P><br \/>\n<P>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:<\/P><br \/>\n<P>&lt;plug-in className=&#8221;org.apache.struts.tiles.TilesPlugin&#8221;&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;set-property<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; property=&#8221;definitions-config&#8221;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value=&#8221;\/WEB-INF\/tiles-defs.xml&#8221;\/&gt;<BR>&lt;\/plug-in&gt;<BR><\/P><br \/>\n<P>The above declaration instructs the struts to load and initialize the Tiles plugin for your application on startup.&nbsp; <\/P><br \/>\n<P>&nbsp;<\/P><br \/>\n<P>&nbsp;<\/P><br \/>\n<P><B>Writing Struts PlugIn Java Code<\/B> <\/P><br \/>\n<P>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. <\/P><br \/>\n<P>Here is code of HelloWorld Struts Plugin:&nbsp; <\/P><br \/>\n<DIV class=java align=left><br \/>\n<TABLE borderColor=#7f0055 cellSpacing=0 cellPadding=3 bgColor=#ffffcc border=1><br \/>\n<TBODY><br \/>\n<TR><!-- start source code --><br \/>\n<TD vAlign=top noWrap align=left><CODE><FONT color=#7f0055><B>package&nbsp;<\/B><\/FONT><FONT color=#000000>roseindia.net.plugin;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>javax.servlet.ServletContext;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>javax.servlet.ServletException;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>org.apache.struts.action.PlugIn;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>org.apache.struts.action.ActionServlet;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>org.apache.struts.config.ModuleConfig;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#3f5fbf>\/**<\/FONT><BR><FONT color=#3f5fbf>*&nbsp;<\/FONT><FONT color=#7f9fbf>@author&nbsp;<\/FONT><FONT color=#3f5fbf>Deepak&nbsp;Kumar<\/FONT><BR><FONT color=#3f5fbf>*&nbsp;@Web&nbsp;http:\/\/www.roseindia.net<\/FONT><BR><FONT color=#3f5fbf>*&nbsp;@Email&nbsp;roseindia_net@yahoo.com<\/FONT><BR><FONT color=#3f5fbf>*\/<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#7f0055><B>public&nbsp;class&nbsp;<\/B><\/FONT><FONT color=#000000>HelloWorldStrutsPlugin&nbsp;<\/FONT><FONT color=#7f0055><B>implements&nbsp;<\/B><\/FONT><FONT color=#000000>PlugIn&nbsp;<\/FONT><FONT color=#000000>{<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;static&nbsp;final&nbsp;<\/B><\/FONT><FONT color=#000000>String&nbsp;PLUGIN_NAME_KEY&nbsp;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>=&nbsp;HelloWorldStrutsPlugin.<\/FONT><FONT color=#7f0055><B>class<\/B><\/FONT><FONT color=#000000>.getName<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#7f0055><B>void&nbsp;<\/B><\/FONT><FONT color=#000000>destroy<\/FONT><FONT color=#000000>()&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.out.println<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#2a00ff>&#8220;Destroying&nbsp;Hello&nbsp;World&nbsp;PlugIn&#8221;<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#7f0055><B>void&nbsp;<\/B><\/FONT><FONT color=#000000>init<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>ActionServlet&nbsp;servlet,&nbsp;ModuleConfig&nbsp;config<\/FONT><FONT color=#000000>)&nbsp;<BR><\/FONT><FONT color=#7f0055><B>throws&nbsp;<\/B><\/FONT><FONT color=#000000>ServletException&nbsp;<\/FONT><FONT color=#000000>{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.out.println<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#2a00ff>&#8220;Initializing&nbsp;Hello&nbsp;World&nbsp;PlugIn&#8221;<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>ServletContext&nbsp;context&nbsp;=&nbsp;<\/FONT><FONT color=#7f0055><B>null<\/B><\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>context&nbsp;=&nbsp;servlet.getServletContext<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>HelloWorldStrutsPlugin&nbsp;objPlugin&nbsp;=&nbsp;<\/FONT><FONT color=#7f0055><B>new&nbsp;<\/B><\/FONT><FONT color=#000000>HelloWorldStrutsPlugin<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>context.setAttribute<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>PLUGIN_NAME_KEY,&nbsp;objPlugin<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#000000>String&nbsp;sayHello<\/FONT><FONT color=#000000>(){<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.out.println<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#2a00ff>&#8220;Hello&nbsp;Plugin&#8221;<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>return&nbsp;<\/B><\/FONT><FONT color=#2a00ff>&#8220;Hello&nbsp;Plugin&#8221;<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;<\/FONT><BR><FONT color=#000000>}<\/FONT><\/CODE> <\/TD><!-- end source code --><\/TR><\/TBODY><\/TABLE><\/DIV><br \/>\n<P><BR><B>Configuring PlugIn<\/B> <\/P><br \/>\n<P>To configure the plugin add the following line your struts-config.xml file. <\/P><br \/>\n<P><FONT color=#0000ff>&lt;plug-in className=&#8221;roseindia.net.plugin.HelloWorldStrutsPlugin&#8221;&gt;<BR>&lt;\/plug-in&gt;<\/FONT><\/P><br \/>\n<P><B>Calling PlugIn From JSP Page<\/B><\/P><br \/>\n<P>Here is the code for calling our PlugIn from jsp page.<\/P><br \/>\n<P><br \/>\n<TABLE borderColor=#7f9fbf cellSpacing=0 cellPadding=0 width=515 border=1><br \/>\n<TBODY><br \/>\n<TR><br \/>\n<TD width=513><FONT color=#0000ff>&lt;%@page contentType=&#8221;text\/html&#8221; import=&#8221;java.util.*,roseindia.net.plugin.*&#8221; %&gt;<BR><\/FONT>&lt;%<BR><BR><FONT color=#0000ff>ServletContext servletContext = this.getServletContext();<BR><BR><BR>HelloWorldStrutsPlugin plugin= (HelloWorldStrutsPlugin) servletContext.getAttribute<BR>(HelloWorldStrutsPlugin.PLUGIN_NAME_KEY);<BR><BR>String strMessage = plugin.sayHello();<\/FONT><BR><BR>%&gt;<BR><BR>Message From Plugin: &lt;%<FONT color=#0000ff>=strMessage<\/FONT>%&gt;<BR><\/TD><\/TR><\/TBODY><\/TABLE><\/P><br \/>\n<P><B>Building and Testing<\/B><\/P><br \/>\n<P>Use ant tool to build the application and then deploy on the server. Enter the url <A href=\"http:\/\/localhost:8080\/strutstutorial\/pages\/plugin.jsp\">http:\/\/localhost:8080\/strutstutorial\/pages\/plugin.jsp<\/A> in your browser. It display &#8220;<B>Hello Plugin<\/B>&#8221; message. Your server console also should display &#8220;Hello Plugin&#8221; message.<\/P><br \/>\n<P>In this section we learnt how to develop simple struts plugin, configure, deploy and test.<\/P><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.strongd.net\/?p=189\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Developing Struts PlugIn<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-189","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=189"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/189\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}