java4all@1986 java. Powered by Blogger.

Developing our own struts PlugIn?

>> Friday, June 24, 2011

Understanding Of Plug In:

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 Sailajaword Struts PlugIn example that will give you idea about creating, configuring and checking Struts PlugIn. Our Sailajaword Stuts PlugIn contains a method called Say Hello, which simply returns HelloWorld message.

Here is code of Sailajaword 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 SailajawordStrutsPlugin implements PlugIn {


  public static final String PLUGIN_NAME_KEY 
  = SailajawordStrutsPlugin.class.getName();

 public void destroy() {
  System.out.println("Destroying Sailajaword World PlugIn");
 }

 public void init(ActionServlet servlet, ModuleConfig config) 
throws ServletException {
  System.out.println("Initializing Sailajaword World PlugIn");
 ServletContext context = null;
 context = servlet.getServletContext();
SailajawordStrutsPlugin objPlugin = new SailajawordStrutsPlugin();
 context.setAttribute(PLUGIN_NAME_KEY, objPlugin);

 }

  public String sayHello(){
  System.out.println("Sailaja Plugin");
  return "Hello Plugin";
  }
  
}

Configuring PlugIn in Struts-config.xml


To configure the plugin add the following line your struts-config.xml file.

<plug-in className="roseindia.net.plugin.SailajawordStrutsPlugin">
</plug-in>

How to Call PlugIn In Jsp Page
 Here is the code how to call the plugIn
<%@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%>

The output will be sailaja PlugIn

0 comments:

Post a Comment

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP