Sample Code : The HelloWorld Plugin

First implementation

This is the simplest plugin implementation. The goal of the plugin is to display the text "Hello, world!". We will use a to achieve this objective, we will use an XPageApplication. The plugin name will be helloworld and the implementation class will be HelloWorldApp. By respecting the naming rules and the internal structure of plugins, the java class will be created in the fr.paris.lutece.plugins.helloworld.web package.

Here is the simplest implementation of this class

package fr.paris.lutece.plugins.helloworld.web;

import javax.servlet.http.HttpServletRequest;

import fr.paris.lutece.portal.service.plugin.Plugin;
import fr.paris.lutece.portal.web.xpages.XPage;
import fr.paris.lutece.portal.web.xpages.XPageApplication;

public class HelloWorldApp implements XPageApplication
{
    public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
    {
        XPage page = new XPage(  );
        page.setContent( "Hello, World!" );
        page.setTitle( "Hello, World!" );
        page.setPathLabel( "Hello, World!" );
        return page;
    }
}                       
                    

In the first implementation, we create our page by creating a new instance of an XPage and we mention the following attributes:

Attribute Description
content The page content
title The title of the page(will be displayed in the title bar)
pathLabel The name of the page in the of the context path of portal.
keywords The keywords placed in the meta tags of the HTML page.

This implementation illustrates the base functions of the application XPage, nevertheless it does not adhere to certain elementary development rules which forbid use of hard coded HTML or labels variables.A correct implementation is realized by using basic Lutece services.

Packaging the plugin

For a production version,a deployment file is needed. It must be created in the WEB-INF/plugins folder and will be called helloworld.xml.

Here is the file content :

<?xml version="1.0" encoding="ISO-8859-1"?>

<plug-in>

   <!-- Plugin Information -->
   <name>helloworld</name>
   <class>fr.paris.lutece.portal.service.plugin.PluginDefaultImplementation</class>
   <version>2.0</version>
   <documentation></documentation>
   <installation></installation>
   <changes></changes>
   <user-guide></user-guide>
   <description>Application Example</description>
   <provider>Mairie de Paris</provider>
   <provider-url>http://lutece.paris.fr</provider-url>
   <icon-url>images/admin/skin/plugins/helloworld/helloworld.png</icon-url>
   <copyright>Copyright 2001-2008 Mairie de Paris</copyright>
   <db-pool-required>0</db-pool-required>

   <applications>
      <application>
	 <application-id>helloworld</application-id>
	 <application-class>fr.paris.lutece.plugins.helloworld.web.HelloWorldApp</application-class>
      </application>
   <applications>

</plug-in>
                        
                    

An application icon must be created and stored in the images/admin/skin/plugins/helloworld folder.

Here is our application icon

Second implementation

The second implementation will place HTML content code in a template file and the labels and PathLabel in the plugin's property file.

package fr.paris.lutece.plugins.helloworld.web;

import javax.servlet.http.HttpServletRequest;

import fr.paris.lutece.portal.service.plugin.Plugin;
import fr.paris.lutece.portal.service.template.AppTemplateService;
import fr.paris.lutece.portal.service.util.AppPropertiesService;
import fr.paris.lutece.portal.web.xpages.XPage;
import fr.paris.lutece.portal.web.xpages.XPageApplication;
import fr.paris.lutece.util.html.HtmlTemplate;

public class HelloWorldApp2 implements XPageApplication
{
    private static final String TEMPLATE_HELLO_WORLD = "site/plugins/helloworld/helloworld.html";
    private static final String PROPERTY_PAGE_TITLE = "helloworld.pageTitle";
    private static final String PROPERTY_PAGE_PATH_LABEL = "helloworld.pagePathLabel";
    
    public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
    {
        XPage page = new XPage(  );
        
        HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_HELLO_WORLD );
        String strPageTitle = AppPropertiesService.getProperty( PROPERTY_PAGE_TITLE );
        String strPagePathLabel = AppPropertiesService.getProperty( PROPERTY_PAGE_PATH_LABEL );

        page.setContent( template.getHtml() );
        page.setTitle( strPageTitle );
        page.setPathLabel( strPagePathLabel );
        
        return page;
    }
}                  

This implementation needs creation of helloworld.html HTML file in the folder WEB-INF/templates/skin/plugins/helloworld

Here is the content of the file :

<center>
    <h1>
        Hello, World!
    </h1>
</center>                        
                    

helloworld.properties must be created in the WEB-INF/conf/plugins folder.

Here is the content of the file :

# configuration file of helloworld plugin

helloworld.pageTitle=Hello World Application
helloworld.pagePathLabel=Hello World
                    

Third implementation of the plugin (Multilingual)

Here is the last implementation which allows to realizing an application in several languages.Lutece uses internalization functions through I18nService service to achieve this.

To generate the resources in several languages, a property file corresponding to each language must be placed in the fr.paris.lutece.plugins.helloworld.resources package.

  • helloworld_messages.properties - default language
  • helloworld_messages_fr.properties - french
  • helloworld_messages_fr_FR.properties - french (France)
  • helloworld_messages_fr_CA.properties - french (Canada)
  • helloworld_messages_en_US.properties - english (USA)
  • ...

Here is the content of the file helloworld_messages_fr.properties :

# resources file for plugin helloworld in french

content=Bonjour le Monde !
pageTitle=Application Bonjour le Monde 
pagePathLabel=Bonjour le Monde 
                    

Here is the new file content heloworld.html :

<center>
    <h1>
        #i18n{helloworld.content}
    </h1>
</center>                        
                    

Finally, Application must be updated as follows :

package fr.paris.lutece.plugins.helloworld.web;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import fr.paris.lutece.portal.service.i18n.I18nService;
import fr.paris.lutece.portal.service.plugin.Plugin;
import fr.paris.lutece.portal.service.template.AppTemplateService;
import fr.paris.lutece.portal.web.xpages.XPage;
import fr.paris.lutece.portal.web.xpages.XPageApplication;
import fr.paris.lutece.util.html.HtmlTemplate;

public class HelloWorldApp3 implements XPageApplication
{
    private static final String TEMPLATE_HELLO_WORLD = "site/plugins/helloworld/helloworld.html";
    private static final String PROPERTY_PAGE_TITLE = "helloworld.pageTitle";
    private static final String PROPERTY_PAGE_PATH_LABEL = "helloworld.pagePathLabel";
    
    public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
    {
        XPage page = new XPage(  );
        
        Locale locale = request.getLocale();
        HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_HELLO_WORLD , locale );
        String strPageTitle = I18nService.getLocalizedString( PROPERTY_PAGE_TITLE , locale );
        String strPagePathLabel = I18nService.getLocalizedString( PROPERTY_PAGE_PATH_LABEL , locale );

        page.setContent( template.getHtml() );
        page.setTitle( strPageTitle );
        page.setPathLabel( strPagePathLabel );
        
        return page;
    }
}