Quick-n-Dirty: Use your own Factory classes in XPages (2) – The VirtualPageTransformer

While playing a little bit with core XPages functionality I found the interesting VirtualPageTransformer interface. With this interface it is possible to implement „virtual“ XPages, and this allows to do something which can be described like an url mapping (the $$OpenDominoDocument.xsp url for example was implemented this way).

To do create your own transformations you have to do the following:

1. Read the first article about using Factory classes in XPages and create the required /WEB_INF/com.ibm.xsp.factories.properties file in your database

2. In this file add a new entry for the page transformer class to use:

PageTransformer=ch.hasselba.factory.PageTransformer

You can choose every key name you want for the factory. The class will be identified from the XPages runtime by the implemented interface com.ibm.xsp.page.VirtualPageTransformer.

3. Create a java class which implements this interface:

package ch.hasselba.factory;

import com.ibm.xsp.page.VirtualPageTransformer;
import com.ibm.xsp.FacesExceptionEx;
import javax.faces.context.FacesContext;

public class PageTransformer implements VirtualPageTransformer {

    public boolean isVirtualPage(FacesContext fc, String pStr) {
        return (pStr.indexOf("$$VirtualPage$$") > (-1));
    }

    public String transformPageName(FacesContext fc, String pStr) {
        try {
            return "/Test.xsp";
        } catch (Exception e) {
            throw new FacesExceptionEx("Error! " + e.getMessage());
        }
    }
}

This adds the „virtual“ page handler „$$VirtualPage$$“ to your database.

This means that every request to your  database is checked if the keyword is contained in the url. For this test, the method isVirtualPage is called with the current url as parameter. If this method returns true, the method transformPageName is called; this method must return a string which identifies the XPage to use. In this case, it is the XPage „Test.xsp

If you open this URL…

http://localhost/Factories.nsf/$$VirtualPage$$.xsp/blahblahblah/blubb/blubb/?etc=etc

… the XPage „Test.xsp“ is rendered instead.

You can add as much VirtualPageTransformers you want to a database.

P.S. Keep in mind that this article has been posted in the “Quick-n-Dirty” category.

Dieser Beitrag wurde unter Java, JSF, XPages abgelegt und mit , , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

3 Antworten zu Quick-n-Dirty: Use your own Factory classes in XPages (2) – The VirtualPageTransformer

  1. Naveen sagt:

    Great find! I always wondered where the heck was ‚$$OpenDominoDocument.xsp‘ stored in NSF. 🙂

    But why is this technique ‚Dirty‘?

Schreibe einen Kommentar zu Sven Hasselbach Antworten abbrechen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.