Single class pure Java JSF application

18 September 2011, by: arjan tijms

In my previous blog entry, Authoring JSF pages in pure Java, it was explained how to set up a JSF based web application using nothing but pure Java. No XML based templating (Facelets) was required and the view was build purely programmatically.

I got one remark though that the example code did used expression language (EL) to patch some of the parts together. Although EL is really convenient, as it provides a way to point to a (nested) method on a scoped object, it’s not regular type-safe Java.

Luckily, the Java component API of JSF doesn’t only use EL based ValueExpressions and MethodExpressions, but also allows regular Java types to be set as listeners and values. This is demonstrated in the code below. While at it, I’ve done away with the separate backing bean for this example and let the same class that defines the page handle the action event.

The result is a pure Java, xml-less, EL-less, config-less, single class JSF application [phew]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@ManagedBean
public class Intro implements Page, ActionListener {
 
    private HtmlOutputText message;
 
    @Override
    public void buildView(FacesContext context, UIViewRoot root) throws IOException {
 
        List<UIComponent> rootChildren = root.getChildren();
 
        UIOutput output = new UIOutput();
        output.setValue("&lt;html xmlns=\"http://www.w3.org/1999/xhtml\">");                
        rootChildren.add(output);
 
        HtmlBody body = new HtmlBody();
        rootChildren.add(body);
 
        HtmlForm form = new HtmlForm();
        body.getChildren().add(form);
 
        message = new HtmlOutputText();
        form.getChildren().add(message);
 
        HtmlCommandButton actionButton = new HtmlCommandButton();
        actionButton.addActionListener(this);
        actionButton.setValue("Do action");
        form.getChildren().add(actionButton);
 
        output = new UIOutput();
        output.setValue("&lt;/html>");
        rootChildren.add(output);
    }
 
    @Override
    public void processAction(ActionEvent event) throws AbortProcessingException {
        message.setValue("hello, world");
    }    
}

To run this on Glassfish, only this class and the small (8,980 bytes) javavdl.jar library are required:

After requesting localhost:8080/demo/intro.jsf, a single button will be shown. The message “hello, world” will be displayed when this button is clicked.

Interesting to note perhaps is that the components themselves only exist for the duration of the request and after every post-back the component tree will be re-created. State set on the components during buildView will be marked as the so-called initial state and will typically not be taken into account by JSF’s state saving mechanism. Since only the HtmlCommandButton in the example code references the Page instance after this buildView it will effectively have request scope.

The java based vdl is still rather simple, so it’s still just a proof of concept. I’ve planned some enhancements that I’ll work on soon if time permits (e.g. perhaps support for scopes other than request scope without using EL). Stay tuned!

Arjan Tijms

8 Comments on “Single class pure Java JSF application”

  1. J2EE Video Tutorial says:

    You made it like a Servlet

  2. Mike says:

    Amazing that this too is JSF…

    @J2EE Video Tutorial… at first sight it may look like a Servlet, but I think that’s only because the root element has the tags. There’s obviously is no component for this in JSF, but you can easily make one.

    The rest of the code is really component based, big difference with Servlets. With Servlets you would have to do low level handling with request parameters, here a given action listener is called. It’s definitely on a higher level than Servlets…

  3. Hello says:

    Should’ve been Hello World!!

  4. development says:

    @Hello
    >Should’ve been Hello World!!

    Good point, I changed it right away :P

    @J2EE, @Mike
    It’s the simplicity of a Servlet with the higher level functionality of JSF. Note that I normally would not recommend doing it this way though and I prefer a separate class for the UI and a separate backing bean. It was done this way to show separation is not enforced by the framework.

  5. Ian says:

    interesting!

  6. What’s new in JSF 2.2? | J-Development says:

    [...] before the view is build again. As a very contrived example, the bean Intro from the code shown in Single class pure Java JSF application could not be view scoped, since the view scope doesn’t exists at the time the view needs to [...]

  7. Michaelc says:

    Vaadin guys should look at this.

  8. Michaelc says:

    Sucks to code ui like this.

Type your comment below:

best counter