Dashboard > blojsom 3.0 > ... > Plugin API Examples > Hello World Plugin Example
  blojsom 3.0 Log In   View a printable version of the current page.  
  Hello World Plugin Example
Added by David Czarnecki, last edited by David Czarnecki on Dec 02, 2006  (view change)
Labels: 

Hello World Plugin

This plugin will add the string, "Hello world", to the context so that you can print out that string in your templates.

Coding the Plugin

The following HelloWorldPlugin.java file should be placed in a package called org.blojsom.plugin.example and compiled.

org.blojsom.plugin.example.HelloWorldPlugin.java
package org.blojsom.plugin.example;

import org.blojsom.plugin.Plugin;
import org.blojsom.plugin.PluginException;
import org.blojsom.blog.Blog;
import org.blojsom.blog.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class HelloWorldPlugin implements Plugin {

    public HelloWorldPlugin() {
    }

    public void init() throws PluginException {
    }

    public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException {
        context.put("HELLO_WORLD", "Hello world");

        // Make sure we pass along the entries to the next plugin
        return entries;
    }

    public void cleanup() throws PluginException {
    }

    public void destroy() throws PluginException {
    }
}

You'll notice that in the plugin's process method, we add the "Hello world" string under the key, HELLO_WORLD. It's as easy as that!

Configuring the Plugin

The compiled HelloWorldPlugin.class should be placed in blojsom's /WEB-INF/classes directory keeping the org/blojsom/plugin/example directory structure.

Add the following to blojsom's plugin configuration file. /WEB-INF/classes/blojsom-plugins.xml.

<bean id="hello-world" class="org.blojsom.plugin.example.HelloWorldPlugin" init-method="init" destroy-method="destroy"/>

After starting your server, you can login to blojsom's web administration console and add this plugin to run in one of your plugin chains. This is configured under Plugins | Mappings. As this plugin doesn't depend on any other plugins, you can add it anywhere in the plugin chain.

Using the Plugin

In your template, for example using Velocity, you can now print out the string placed into the context from the plugin.

My plugin says: $HELLO_WORLD

Congratulations! You have just written your first plugin for blojsom.

Site running on a free Atlassian Confluence Open Source Project License granted to blojsom. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.4 Build:#809 Jun 12, 2007) - Bug/feature request - Contact Administrators