This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • Extending an Application with Custom Plugins
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

In this tutorial we will show how easily your application can be made extensible using the Struts 2 plugins mechanism.

The Interface

First thing we have to do is define an interface that our plugins will implement. This interface must be available to both your web application and the plugins, so keeping it on a separate jar is a good idea.
package example;

public interface IMyPlugIn {
   String saySomething();

The Plugin

Now lets create a plugin. Creating a plugin is a simple task, all you need to do is build a jar with your classes, and a file called struts-plugin.xml on its root. First, the class implementing the interface defined on the first step.
package example.impl;

import example.IMyPlugin; 

public class MyPlugin implements IMyPlugin {
   public String saySomething() {
       return "We don't need no education"; //Sorry, I couldn't help it :)

Now, struts-plugin.xml

<?xml version="1.0" encoding="UTF-8" ?>

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    <bean type="example.IMyInterface" class="example.impl.MyPlugin" name="myplugin"/>

To install the plugin into your application, just drop the jar file under WEB-INF/lib.

The Action

Now, lets get the framework to inject an instance of com.opensymphony.xwork2.inject.Container into our action, which we will use to discover available plugins at runtime.
package example.actions;

import example.IMyPlugin;

public class MyAction extends ActionSupport {
    Set<IMyPlugin> plugins;

    public void setContainer(Container container) {
        Set<String>  names = container.getInstanceNames(IMyPlugin.class);
        plugins = new HashSet<IMyPlugin>();
        for(String name : names) {
            plugins.add(container.getInstance(IMyPlugin.class, name));

    public Set<IMyPlugin> getPlugins() {
        return this.plugins;


Let's do something with those plugins:

<s:iterator id="plugin" value="plugins">
  <s:property value="#plugin.saySomething()"/>
  • No labels