Introduction to JSF 2 Using NetBeans and GlassFish

Using NetBeans 6.8M1 and GlassFish v3, its possible to write Java EE 6 web applications. In this article i’ll be showing what managed beans look like and how they are linked up in Facelets pages. I’ll also show how to localize the application for use with different languages. Finally, I’ll show how the Bean Validation Framework (JSR-303) can be used to add simple validation to POJOs.

To show these features, I’ve written a simple “Hello World” program. You can download the source code here.

This article assumes you have got NetBeans 6.8M1 or above and GlassFish v3 installed. It also assumes you have a basc understanding of JSF 1.

Anatomy of a JSF 2 application.

In JSF 2 applications, Facelets is the recommended view technology rather than JSP. Facelets allows markup to be defined as XML which gives the benefit of it being able to be automatically parsed by IDEs such as NetBeans. I’ll assume that you are familiar with Facelets, however you can read more about Facelets in my book “Seam 2.x Web Development”.

In the classic “Hello World” application, the user is requested to enter their name. This is the same in our sample application, with the input form defined as:

<h:form id="helloForm">
    <h:panelGrid columns="3">
        <h:outputText value="#{msgs.whatIsName}"/>
        <h:inputText id="name" value="#{}"/>
        <h:message for="name" styleClass="error"/>
        <h:commandButton action="#{helloBean.sayHello}" value="Hello"/>

Looking through this form, we can see several things:

  1. The output text (“What is your name?”) is defined by the EL #{msgs.whatIsName}. This allows us to internationalize our application easily. We’ll take a look at internationalization shortly.
  2. The inputted name is stored in the “name” property of the “helloBean” managed bean.
  3. We’ve used the <h:message /> component to display an error message if the name isn’t valid. This will be populated via the Bean Validation Framework from the managed bean.
  4. Pressing the button causes the method “sayHello” on the “helloBean” to be invoked.

JSF 2 Backing Bean

In JSF 1 applications, backing beans were written as POJOs and then declared to JSF via XML configuration within the faces-config.xml file. With JSF 2, backing, or managed beans are declared via Java 5 annotations. To declare a bean as a managed bean, all we need to do is annotate the bean with the @ManagedBean annotation. This annotation takes a name parameter which is the name of the managed bean.

For our sample form shown above, we could write a managed bean as:

public class HelloBean {

    @Pattern(regexp="(.+)", message="{}")
    private String name;

    public HelloBean() {

    public String sayHello() {
        return "hi";

    public String getName() {
        return name;

    public void setName(String name) { = name;

You can see that this is a smple POJO, however there are several annotations:

  1. @ManagedBean. This is defining the POJO as a managed bean. In this instance, the bean is called “helloBean”.
  2. @RequestScope. This annotation defines the bean to have request scope, meaning that the bean will be instantiated for the duration of the HTTP request.
  3. @Pattern. This annotation is part of the Bean Validation Framework. This annotation specifies that the “name” attribute must conform to the regular expression defined by regexp - (.+) in this case, i.e. any non-empty string. If the attribute validation fails, then the message defined by the “message” property is displayed.

This managed bean therefore has one property, “name”, which is bound from the Facelets page using the #{} expression. The bean has one operation, “sayHello”, which is invoked from the Facelets page via the #{helloBean.sayHello} expression. In this sample application, pressing the button invokes the “sayHello” method which returns the string “hi” to JSF. JSF then uses the navigation rules within the faces-config.xml file to determine which page to display next. There is nothing different here from a JSF 1 application.


To finish off the basic outline of a JSF 2 application, all that is left is to define the resource file for the application localization and the resources for the Bean Validation Framework.

Bean Validation Framework Messages

The Bean Validation Framework reads its localized resources from a file called in the default package of the web application. This file is a simple properties file: enter a name

Application Localization Messages

Application localization messages are stored in a simple properties file. The location of this file is defined within the faces-config.xml file as shown below.


Now that we’ve seen how a basic JSF 2 application is developed, you can try running the sample application to see it in action for yourselves. I hope this have given an insight into JSF 2 which you can now expand upon by developing further JSF 2 applications.

This article is also available in French.