Mar 8, 2018 11:30:17 AM seb leridon avatar   1112

Example of creating an XPage: The HelloWorld Plugin


First implementation

Here's how to make the simplest plugin. The object of this one is to display the text "Hello, world!". We will use for this an XPageApplication. The name of the plugin will be helloworld and that of the implementation class of the application will be HelloWorldApp . By respecting the rules of naming and structure of the plugins, the class will be created in the package fr.paris.lutece.plugins.helloworld.web .

Here is the simplest implementation of this class

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

import javax.servlet.http.HttpServletRequest;

import en.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 this first implementation, we create our page in instantiating a new XPage object for which we define the following attributes:

AttributeDescription
content The content of the page
title The title of the page (appears in the title bar of the browser)
pathLabel The name of the page in the portal breadcrumb
keywords Key words to put in tags meta of the HTML page

This implementation illustrates the basic operation of an XPage application, but it does not respect certain basic rules of development that want variable elements such as labels or HTML code not to be written "hard" in Java code. To correct this, we will make a new implementation using some Lutetian basic services.

Packager the plugin

To make an operational plugin, you must now create your deployment file. That must be created in the directory WEB-INF / plugins and will be called helloworld.xml .

Here is the contents of the file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<plug-in>
     <!-- Plugin Informations -->
     <name>helloworld</name>
     <class>fr.paris.lutece.portal.service.plugin.PluginDefaultImplementation</class>
     <version>1.0</version>
     <documentation></documentation>
     <installation></installation>
     <changes></changes>
     <user-guide></user-guide>
     <description>Exemple d'application</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-2014 Mairie de Paris</copyright>
     <db-pool-required>0</db-pool-required>

     <!-- Xpage configuration -->
     <applications>
          <application>
	      <application-id>helloworld</application-id>
	      <application-class>fr.paris.lutece.plugins.helloworld.web.HelloWorldApp</application-class>
          </application>
     </applications>
</plug-in>

You must also create an icon for the application and save it in the images / admin / skin / plugins / helloworld directory.

Here is the icon of our application

Second implementation

This second implementation will deport the HTML code of the content of the page into a template file and the labels of the title and PathLabel in the plugin 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 requires the creation of an HTML helloworld.html file in the WEB-INF / templates / skin / plugins / helloworld directory.

Here is the file content:

<h1> Hello, World! </h1>

You must also create a helloworld.properties file in the WEB-INF / conf / plugins directory.

Here is the content of this file:

# helloworld plugin configuration file

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

Third implementation (multilingual)

Here is a last implementation that would easily offer the application in multiple languages. For that, Lutece uses Java's internationalization functions through the I18nService service.

To manage the resources in several languages ​​you have to create, in the package fr.paris.lutece.plugins.helloworld.resources , the properties files corresponding to each location.

  • helloworld_messages.properties - default language
  • helloworld_messages_fr.properties - french
  • helloworld_messages_fr_FR.properties - French (France)
  • helloworld_messages_uk_CA.properties - French (Canada)
  • helloworld_messages_en_US.properties - English (United States)
  • ...

Here is the content of the helloworld_messages_fr.properties file:

# helloworld plugin resource file in french

happy = Hello World!
pageTitle = Application Hello World
pagePathLabel = Hello World

Note The helloworld.properties configuration file will no longer be needed in this example.

Here is the new content of the heloworld.html file:

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

Finally the implementation of the application will be modified as follows:

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

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import en.paris.lutece.portal.service.i18n.I18nService;
import en.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 en.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 ();

          Local local = 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;
     }
}

Fourth implementation (multilingual + MVC)

This last implementation uses the MVC framework available from the Lutece 4.1

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

import fr.paris.lutece.portal.web.xpages.XPage;
import.paris.lutece.portal.util.mvc.xpage.MVCApplication;
import.paris.lutece.portal.util.mvc.commons.annotations.View;
import.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
import javax.servlet.http.HttpServletRequest;

@Controller (xpageName = "helloworld", pageTitleI18nKey = "helloworld.pageTitle", pagePathI18nKey = "helloworld.pagePathLabel")
public class HelloWorldApp4 extends MVCApplication
{
    // Templates
    private static final String TEMPLATE_HELLO_WORLD = "site / plugins / helloworld / helloworld.html";
    private static final String VIEW_HELLO_WORLD = "helloWorld";

    @View (value = VIEW_HELLO_WORLD, defaultView = true)
    public XPage viewHelloWorld (HttpServletRequest request)
    {
        return getXPage (TEMPLATE_HELLO_WORLD, request.getLocale ());
    }
}