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

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

12 comments to “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.

  9. Det says:

    This very much looks like a pure JavaFX API. Additionally, JavaFX has a Builder library to provide a fluent-interface API (not to mention GroovyFX and ScalaFX).

    The Builder API would make a great addition here, and help to visually structure the code.

  10. development says:

    @Det

    JavaFX’s fluent API would actually work nicely here. It’s just that it’s not in the native JSF API and the code as shown uses as little new API as possible.

    It’s a great idea though to add this ;)

  11. Rodmar CONDE says:

    Cool !

    However, I don’t understand very well why it is needed to add begin and end tags. Normally that can be added by the framework on any request, isn’t it ?

    Regards,

  12. Arjan Tijms says:

    @Rodmar, I assume you mean the <html> and </html> “tags”?

    They are needed in this case because there is not standard component for “html”. If there was, I could have added that component as the root one, and added the rest as children.

    Such component however is rather trivial to create, and I would have indeed done that if this wasn’t just a proof of concept.

Type your comment below:

best counter