View on GitHub

Weblets

weblets full project mirror

Download this project as a .zip file Download this project as a tar.gz file

Usage of Weblets in a jsf centric environment

As Weblets originated from a project which was developed for an ajax and JSF centric book, Weblets has extensive tooling support for jsf, and tries to utilize JSFs internal structures extensively, to ease the configuration and usage for the application programmer.

Weblets itself is not JSF centric, but besides its generic web framework coverage it has a very strong JSF tooling support. Over time it might be possible that other web frameworks could be supported in a similar way where needed.

The generic tooling support of course also works in JSF, but it doesn�t have to be used, due to the easier configuration and usage of the internal JSF mechanisms

Setup

Details of the setup are described in the Setup guide .

Setup Short summary:

Every basic JSF setup should work out of the box in a plug and play way. The only thing you which is needed is a valid path triggering pattern for the faces servlet.

Code 0: valid path pattern in the web.xml

   <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>

Of course file patterns can be used additionally.

Another limitation of this system is, if you use a Faces Servlet which is not the standard JSF one you have to do a fallback to the generic configuration! However you still can use all of the other features Weblets provides for jsf.

Basic Functions

Introduction

The Weblets APIs follow the general patterns as described in Weblets Users Guide: Introduction to the API Patterns . for further information please follow the link.

JSF Frontend APIs

Over the years JSF has become the base technology of various frameworks and view technologies, hence to cover Weblets in JSF entirely, we have to cover several frontend technologies

JSF 1.1

Although nowadays basically a legacy technology, but still widely used, JSF 1.1 has several limitations regarding its frontend technologies and the usage of EL. JSF 1.1 in most cases relies on JSP as frontend view technology with its own api providing the additional work, and as EL it provides its own EL technology.

Now to use Weblets in JSF 1.1, you have basically two options

  1. For cases outside of the JSF area (areas in your page outside of f:view) rely on basic jsp mechanisms as described in JSP : Usage page
  2. Within JSF areas you can rely on an EL dummy map provided automatically to your JSF system. To use it no additional configuration is needed

The following code example showcases the usage of this EL dummy map:

Code 3: Usage or resource and url in a JSF frontend environment

  <h:outputText value="#{weblet.url['weblets.demo']['/welcome.js']}" />
  <h:graphicImage value="#{weblet.resource['weblets.demo']['/img/icon_alert.gif']}" />

In this example you can precisely see why you need sometimes resource weblet.resource or url weblet.url , in this example h:graphicImage adds the application context itself so we dont need a full url including the web application context, instead we just need a path relative to it.

Code 4: Difference between url and resource in a JSF specific api

   #{weblet.url['weblets.demo']['/welcome.js']}

Is a typical JSF dummy map. One of JSF 1.1s EL implementation is, it does not allow functions which would easy the extension of the wel, all it allows is properties and maps. To allow a function like behavior you can map the functionality into dummy maps and pass the parameters over the map keys.

Facelets and JSF 1.2

The last years facelets has become the frontend technology for many projects. One of the advantages of facelets is that it already provides a JSP 2.x and JSF 1.2 el, so that you automatically can start to use the features of the EL in there. The above mentioned things also are valid for JSF 1.2 which also has the latest EL implementation available.

If you are in a facelet domain you have two options to utilize weblets

  1. via EL functions (the same as in jsp 2.x)
  2. via the above described dummy maps

Newer EL implementations provide so called EL functions, those are functions which can be added to the actual EL implementation to extend its functionality. If you are in a system which uses newer EL implementations, it is highly recommended to use those instead of the dummy maps.

The next example shows how to use the EL functions:

Code 5: Usage of EL functions in faclets

     xmlns:wbl="http://weblets.dev.java.net/facelet-tags"  this has to be added to your namespace definitions

      <h:outputText value="${weblet:url('weblets.demo','/welcome.js')}" />
      <h:graphicImage value="${weblet:resource('weblets.demo','/img/icon_alert.gif')}" />
For Facelet 1.2 and below the tag definitionw was following:
     xmlns:wbl="http://weblets.dev.java.net/tags"
      <h:outputText value="${weblet:url('weblets.demo','/welcome.js')}" />
      <h:graphicImage value="${weblet:resource('weblets.demo','/img/icon_alert.gif')}" />

As you can see the calls are very similar, but instead of reverting into a (still available) JSF dummy map we call directly EL functions we added to our page by adding another xml namespace to our xmlns definitions.

For an extensive examples of the EL function calls, check out the weblets-demo application provided with your weblets download package

JSF 2.x Resource Handlers

If you have reached this section in the documentation you are probably aware that JSF has introduced its own Resource mechanism. The so called ResourceHandlers and Resources. So how do you combine this with Weblets. Weblets 1.3+ provides its own ResourceHandler and Resource class which allows to access your Weblets hosted resources from within the JSF Resource API. The usage is very simple, any JSF Resource request which hits a valid Weblet resource will be served as is. So following code:
<h:graphicImage id="yyy2" value="./javax.faces.resource/img/icon_alert.gif.jsf?ln=weblets.demo"/>
    
Will trigger a weblet resource loading request and will load your weblet appopriately. Of course As usual Weblets comes with a handful of nifty utility functions/methods which makes the generation of such URLs easy. Following example explains how to generate such urls in different ways.
<h:graphicImage id="standardRequest" value="./javax.faces.resource/img/icon_alert.gif.jsf?ln=weblets.demo"/>

    <h:graphicImage id="helperMap" value="#{weblet.jsfURL['weblets.demo']['/img/icon_alert.gif']}"/>

    <h:graphicImage id="elFunction" value="#{wbl:jsfURL('weblets.demo','/img/icon_alert.gif')}"/>

    <h:graphicImage id="elFunctionWithResource" value="./#{wbl:jsfResource('weblets.demo','/img/icon_alert.gif')}"/>
    
We use a standard manual request, and various types of map access or el functions to generate valid JSF 2.0 url requests. Also resource handlers can use subbundles and can use the h:outputScript tag. Here is an example which uses both:
<h:outputScript id="loadingBundleScript" library="weblets.demo" name="subbundle/script1.js"  target="body"/>
    <h:outputScript id="loadingBundleScript2" library="weblets.demo" name="subbundle/script2.js"
                      rendered="#{wbl:isResourceLoaded('weblets.demo','/subbundle/script2.js')}"  target="body"/>
    
In this example we use h:outputScript to load a Weblets resource and then we check for isResourceLoaded for the second script in case it is a subbundled script. (Note we have a difference in how we use the Weblets uri from within jsf and Weblets. In Weblets we always use a leading "/" while jsf omits the leading "/")

Backend APIs for the Component and Bean Developers

Component and backend authors in JSF generally face the problem, of not having a standardized way to load resources. The classical way is either to use

  • a document root and relative resources
  • a view handler handler.getResourceURL(context, "/myresources/myScript.js");

    In both methods you have to pack the resources into your application, you have to take care about their precise location in your document tree etc... This is burden and error prone, and resources can be traced down to their actual location which opens another door for hackers to bring in trojan horses! Besides that, changing the location or even physical source of any resource might cause changes in the entire application tree and even some components!

    Now Weblets is here to solve all those problems. And again we revert to our proven url and uri notation.

    Weblets provides for many backend technologies Utils classes which provide the usual url and uri notations, JSF as well:

    Code 6: Usage contractual API the <<<JSF >> way>

        FacesWebletUtils.getURL(context, "demo.webletname", "/subpath/myresource.js")
        FacesWebletUtils.getResource(context, "demo.webletname", "/subpath/myresource.js")
    
        Context being the Faces Context!

    That is basically it, nothing more and nothing less. It is easy to call Weblets from your backing bean or component, all you have to do is, to use the provided API calls! And then work with their return values as you would with other url strings!