Thursday, October 23, 2008

JSF and Weblets, how simple it is …

When creating JSF components libraries, the rendering part of the JSF component needs make some references to resource files (.css, .js, images).

Those resources needs to be stored in the component library jar file. By this way the webapp using this library does not require any configuration.

Main JSF frameworks (Trinidad, Adf, IceFaces…) already include a resource manager servlet used to retrieve resources files.

When creating a JSF component library from scratch (starting from MyFaces or Sun JSF implementation), this kind of resource manager does not exist and it is in charge to the library developer to manage the resource management.

Different solutions are available and are described in the post:

This post will sum-up the weblets1.0 solution.

Objectives of the weblets project is to simplify the management of resources. Weblets are not restricted to JSF frameworks and can be use in multiple ways.

Steps to call a resource from JSF component:

1. Downloading the weblets libraries(weblets-api-1.0.jar, weblets-impl-1.0.jar) and add it in your project

2. Creating the weblets-config.xml file located in the META-INF directory of the component library.

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




The only thinks to configure are:
-Defining a weblet-name (myapp.resources)
-Referencing your resource directory path containing your css, js with the weblet init-param element (com.myapp.resources).
com.myapp.resources refers to the directory path com/myapp/resources

3. Referencing a resource managed by the weblets from the JSF component

        String cssFile = FacesWebletUtils.getURL(context, "myapp.resources","/myapp.css");
writer.startElement("link", this);
writer.writeAttribute("rel", "stylesheet", null);
writer.writeAttribute("href", cssFile, null);

FacesWebletUtils.getURL(context,, "/") is used to retrieve the resources stored in the library jar file.
In the sample code, myapp.css is located in the com/myapp/resources directory.


Weblets is a very usefull library used to simplify the resource management. We can regret that this is not part of the basic JSF framework. You can download and access all weblets documentation here

No comments:

Frederic shared items