#WHATIS?: JavaServer Faces. An introduction to JSF 2.3

Last Updated:  September 15, 2020 | Published: July 29, 2018

.Scrolling through my news feed on Twitter I get flooded with new releases of npm modules and more and more blog articles about server-side rendering technologies for the latest Single Page Application frameworks like Angular, React. What about proven and productive technologies likes JavaServer Faces? The following tweet summarized my current feelings:

As I personally really like React and used it in the past for several projects, I am not that happy about the bunch of additional npm packages. To be up-to-date you have stick to the latest versions and this is sometimes awful. If you are also developing the backend you have to observe more frameworks and libraries. For most of the (enterprise) applications,  it's all about validating user inputs, manipulating, storing, and displaying data. This can be easily achieved with a SPA and a backend like Spring or Java EE.  Nevertheless, you need experience in both technologies, and staying up-to-date is more complex.

My way to JavaServer Faces

To get a whole overview of the server-side vs. client-side web development and to overcome my (maybe) Junior mentality of using every new shiny framework I wanted to know more about server-side rendering with Java. I always had the prejudice that it's quite old, slow, and not usable. But I never took the time to try it for my own.

At one of the airhacks.com Adam Bien (@AdamBien) did a quick intro to JavaServer Faces (JSF). He showed us how easy it was to get started and create JSF components in seconds. To get deeper insights into this mature technology I bought the book The Definitive Guide to JSF in Java EE 8. Both authors Bauke Scholtz (@OmniFaces) & Arjan Tijms (@arjan_tijms)  did an awesome job with this book. They cover all aspects of web development with JSF: WebSockets, Validation, Internationalization, Security, etc. and I can highly recommend this awesome book.

Today I'll show you a quick Hello World with JSF 2.3 and Maven on Payara 5.182 to make you familiar with the technology. Like every Java EE part (CDI, JPA, EJB …) JSF has a formal specification and several reference implementations. The most popular implementations for JSF are Apache MyFaces and Mojarra.

JavaServer Faces project setup

To start your web development your pom.xml needs at least the following dependency:

The dependency javax:javaee-apibrings you the formal specification with several classes and annotations. The implementation of several Java EE APIs is provided by the application server (e.g. Payara, OpenLiberty, Wildfly etc.). Marking the dependency as provided will lead to a thin war deployment where the Java EE dependency is not packaged to the war file as it is part of the application server. The war file will just contain your business logic and JSF views.

To configure your web application and later on JSF you need a web.xml file which has to be placed  in /src/main/webapp/WEB-INF:

With this configuration, you enable the central FacesServlet, define the mapping of JSF related views and the welcome file. JSF follows the MVC approach and your views are mostly written as .xhtml files (a detailed JSF MVC explanation can be found here).

Writing your first JSF view

A simple Hello World view (index.xhtml) with an input field and a button to display the Hello World message can look like the following and should be stored at /src/main/webapp:

You may notice the prefix <h:…> on several HTML elements which is one of the main differences to normal HTML (.html) files. With this prefix, you reference a specific namespace and in the case of <h:…> it's the namespace of the basic HTML elements. There are several other namespaces you can use like <f:…>. They all provide a bunch of predefined components and functionality. In the example above we are using a form with a label for an input field, a submit button, and a placeholder.

Another difference to normal HTML elements is the use of the following syntax #{xyz.value}. You can find it for the label value and the action for the submit button. This is an expression language where you can reference fields and methods of so-called backing beans. In our example, I am referencing two fields (name and greet) and a method (greetPerson) of the backing bean helloWorldBackingThe values get replaced when the JSF view is rendered on the server.

Providing data with a JSF backing bean

The name of the backing bean is the name of the Java class which is annotated with @Named and a CDI scope like @RequestScoped if nothing else is configured. The backing bean looks like the following:

With this setup, you don't have to create any JavaScript code. Inserting a name to the input field and pressing submit will render the same page with the Hello world message. This is just a small example of a quick JSF intro. There is a lot more to discover like WebSockets, AJAX calls,  UI layout, notifications, etc.

For a quick deployment on your local machine I created a simple Dockerfile for starting the app:

You can find the Hello World example with the required steps to run this application locally on GitHub.

I'll definitely play more with JSF and start using the awesome component library PrimeFaces which brings a lot of ready-to-use components (e.g. date picker, dialogs, data tables, panels, menu bars etc.) and keep you up-to-date. If you want to get further information about JSF, have a look at the following book The Definitive Guide to JSF in Java EE 8. For further code examples use this overview or my other JSF blog posts.

Have fun using JavaServer Faces,

Phil.

 

  • Nice Intro,have a question
    Would you suggest using JSF2.3 for a medium to large scale project in 2018/19 with Angular/React/Vue and many other javascript frameworks leading over this? What Benefit you see using JSF over the Javascript framweorks?

    Also is there any article you have worked on Java Server Faces Life Cycle?

    Since JSF components are stored on Server(Even Client with a parameter in Config file)?Any efficient way to handle that?

    • Hi Reddy,

      thanks for your question. I think this really depends on your project. If you want to develop a pixel perfect UI with fancy animations then JSF is maybe the wrong choice. Have a look at the ready-to-use JSF components from PrimeFaces and the different themes . If they fit for your use case and your development team is more experienced in Java, you’ll be more productive with JSF. Another benefit I see in JSF is the maturity of this technology whereas the Single Page Application (SPA) frameworks are often changing and you have to keep up-to-date.

      Regarding your last three questions I have not yet written an article about that but I can highly recommend you the following book: The Definitive Guide to JSF in Java EE 8 or this overview for some tutorials and answers.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Sign up for Our Mailing List And Get

    the Testing Java Applications ($9) Cheat Sheet for Free

    Testing Java Applications Cheat Sheet Cover
    >