17 min read

Develop professional enterprise Java EE applications quickly and easily with this popular IDE

Introduction to JavaServer faces

Before JSF existed, most Java web applications were typically developed using non-standard web application frameworks such as Apache Struts, Tapestry, Spring Web MVC, or many others. These frameworks are built on top of the Servlet and JSP standards, and automate a lot of functionality that needs to be manually coded when using these APIs directly.

Having a wide variety of web application frameworks available, often resulted in “analysis paralysis”, that is, developers often spend an inordinate amount of time evaluating frameworks for their applications.

The introduction of JSF to the Java EE specification resulted in having a standard web application framework available in any Java EE compliant application server.

We don’t mean to imply that other web application frameworks are obsolete or that they shouldn’t be used at all. However, a lot of organizations consider JSF the “safe” choice since it is part of the standard and should be well supported for the foreseeable future. Additionally, NetBeans offers excellent JSF support, making JSF a very attractive choice.

Strictly speaking, JSF is not a web application framework per se, but a component framework. In theory, JSF can be used to write applications that are not web-based, however, in practice JSF is almost always used for this purpose.

In addition to being the standard Java EE component framework, one benefit of JSF is that it provides good support for tools vendors, allowing tools such as NetBeans to take advantage of the JSF component model with drag and drop support for components.

Developing our first JSF application

From an application developer’s point of view, a JSF application consists of a series of XHTML pages containing custom JSF tags, one or more JSF managed beans, and an optional configuration file named faces-config.xml.

faces-config.xml used to be required in JSF 1.x, however, in JSF 2.0, some conventions were introduced that reduce the need for configuration. Additonally, a lot of JSF configuration can be specified using annotations, reducing, and in some cases, eliminating the need for this XML configuration file.

Creating a new JSF project

To create a new JSF project, we need to go to File | New Project, select the Java Web project category, and Web Application as the project type.

After clicking Next>, we need to enter a project name, and optionally change other information for our project, although NetBeans provides sensible defaults.

On the next page in the wizard, we can select the server, Java EE version, and context path of our application. In our example we will simply pick the default values.

On the next page of the new project wizard, we can select what frameworks our web application will use.

Unsurprisingly, for JSF applications we need to select the JavaServer Faces framework.

When clicking on Finish, the wizard generates a skeleton JSF project for us, consisting of a single facelet file called index.xhtml, a web.xml configuration file.

web.xml is the standard, optional configuration file needed for Java web applications, this file became optional in version 3.0 of the Servlet API, which was introduced with Java EE 6. In many cases, web.xml is not needed anymore, since most of the configuration options can now be specified via annotations. For JSF applications, however, it is a good idea to add one, since it allows us to specify the JSF project stage.



xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”>



javax.faces.PROJECT_STAGE

Development





Faces Servlet

javax.faces.webapp.FacesServlet

1





Faces Servlet

/faces/*







30







faces/index.xhtml

As we can see, NetBeans automatically sets the JSF project stage to Development, setting the project stage to development configures JSF to provide additional debugging help not present in other stages. For example, one common problem when developing a page is that while a page is being developed, validation for one or more of the fields on the page fails, but the developer has not added an or tag to the page. When this happens and the form is submitted, the page seems to do nothing, or page navigation doesn’t seem to be working. When setting the project stage to Development, these validation errors will automatically be added to the page, without the developer having to explicitly add one of these tags to the page (we should, of course, add the tags before releasing our code to production, since our users will not see the automatically generated validation errors). The following are the valid values for the javax.faces.PROJECT_STAGE context parameter for the faces servlet: Development

Production

SystemTest

UnitTest The Development project stage adds additional debugging information to ease development. The Production project stage focuses on performance. The other two valid values for the project stage (SystemTest and UnitTest), allow us to implement our own custom behavior for these two phases. The javax.faces.application.Application class has a getProjectStage() method that allows us to obtain the current project stage. Based on the value of this method, we can implement the code that will only be executed in the appropriate stage. The following code snippet illustrates this: public void someMethod() {

FacesContext facesContext = FacesContext.getCurrentInstance();

Application application = facesContext.getApplication();

ProjectStage projectStage = application.getProjectStage();

if (projectStage.equals(ProjectStage.Development)) {

//do development stuff

} else if (projectStage.equals(ProjectStage.Production)) {

//do production stuff

} else if (projectStage.equals(ProjectStage.SystemTest)) {

// do system test stuff

} else if (projectStage.equals(ProjectStage.UnitTest)) {

//do unit test stuff

}

} As illustrated in the snippet above, we can implement the code to be executed in any valid project stage, based on the return value of the getProjectStage() method of the Application class. When creating a Java Web project using JSF, a facelet is automatically generated. The generated facelet file looks like this:

-//W3C//DTD XHTML 1.0 Transitional//EN”

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

>



Facelet Title





Hello from Facelets





As we can see, a facelet is nothing but an XHTML file using some facelets-specific XML name spaces. In the automatically generated page above, the following namespace definition allows us to use the “h” (for HTML) JSF component library:

The above namespace declaration allows us to use JSF specific tags such as and which are a drop in replacement for the standard HTML/XHTML and tags, respectively.

The application generated by the new project wizard is a simple, but complete JSF web application. We can see it in action by right-clicking on our project in the project window and selecting Run. At this point the application server is started (if it wasn’t already running), the application is deployed and the default system browser opens, displaying our application’s default page.

Modifying our page to capture user data

The generated application, of course, is nothing but a starting point for us to create a new application. We will now modify the generated index.xhtml file to collect some data from the user.

The first thing we need to do is add an tag to our page. The tag is equivalent to the tag in standard HTML pages. After typing the first few characters of the tag into the page, and hitting Ctrl+Space, we can take advantage of NetBeans’ excellent code completion.

After adding the tag and a number of additional JSF tags, our page now looks like this:



-//W3C//DTD XHTML 1.0 Transitional//EN”

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>



>



Registration





Registration Page



columnClasses=”rightalign,leftalign,leftalign”>



value=”#{registrationBean.salutation}” >



















required=”true”

value=”#{registrationBean.firstName}” />





required=”true”

value=”#{registrationBean.lastName}” />





value=”#{registrationBean.age}”/>





required=”true”

value=”#{registrationBean.email}”>







action=”confirmation” />





The following screenshot illustrates how our page will be rendered at runtime:

All JSF input fields must be inside an tag. The helps us to easily lay out JSF tags on our page. It can be thought of as a grid where other JSF tags will be placed. The columns attribute of the tag indicates how many columns the grid will have, each JSF component inside the component will be placed in an individual cell of the grid. When the number of components matching the value of the columns attribute (three in our example) has been placed inside , a new row is automatically started.

The following table illustrates how tags will be laid out inside an tag:

Each row in our consists of an tag, an input field, and an tag.

The columnClasses attribute of allows us to assign CSS styles to each column inside the panel grid, its value attribute must consist of a comma separated list of CSS styles (defined in a CSS stylesheet). The first style will be applied to the first column, the second style will be applied to the second column, the third style will be applied to the third column, so on and so forth. Had our panel grid had more than three columns, then the fourth column would have been styled using the first style in the columnClasses attribute, the fifth column would have been styled using the second style in the columnClasses attribute, so on and so forth.

If we wish to style rows in an , we can do so with its rowClasses attribute, which works the same way that the columnClasses works for columns.

Notice the tag inside near the top of the page, this is a new tag that was introduced in JSF 2.0. One new feature that JSF 2.0 brings to the table is standard resource directories. Resources such as CSS stylesheets, JavaScript files, images, and so on, can be placed under a top level directory named resources, and JSF tags will have access to those resources automatically. In our NetBeans project, we need to place the resources directory under the Web Pages folder.

We then need to create a subdirectory to hold our CSS stylesheet (by convention, this directory should be named css), then we place our CSS stylesheet(s) on this subdirectory.

The value of the library attribute in must match the directory where our CSS file is located, and the value of its name attribute must match the CSS file name.

In addition to CSS files, we should place any JavaScript files in a subdirectory called javascript under the resources directory. The file can then be accessed by the tag using “javascript” as the value of its library attribute and the file name as the value of its name attribute.

Similarly, images should be placed in a directory called images under the resources directory. These images can then be accessed by the JSF tag, where the value of its library attribute would be “images” and the value of its name attribute would be the corresponding file name.

Now that we have discussed how to lay out elements on the page and how to access resources, let’s focus our attention on the input and output elements on the page.

The tag generates a label for an input field in the form, the value of its for attribute must match the value of the id attribute of the corresponding input field.

generates an error message for an input field, the value of its for field must match the value of the id attribute for the corresponding input field. The first row in our grid contains an . This tag generates an HTML tag on the rendered page.

Every JSF tag has an id attribute, the value for this attribute must be a string containing a unique identifier for the tag. If we don’t specify a value for this attribute, one will be generated automatically. It is a good idea to explicitly state the ID of every component, since this ID is used in runtime error messages. Affected components are a lot easier to identify if we explicitly set their IDs.

When using tags to generate labels for input fields, or when using tags to generate validation errors, we need to explicitly set the value of the id tag, since we need to specify it as the value of the for attribute of the corresponding and tags.

Every JSF input tag has a label attribute. This attribute is used to generate validation error messages on the rendered page. If we don’t specify a value for the label attribute, then the field will be identified in the error message by its ID.

Each JSF input field has a value attribute, in the case of , this attribute indicates which of the options in the rendered tag will be selected. The value of this attribute must match the value of the itemValue attribute of one of the nested tags. The value of this attribute is usually a value binding expression, that means that the value is read at runtime from a JSF managed bean. In our example, the value binding expression #{registrationBean.salutation} is used. What will happen is at runtime JSF will look for a managed bean named registrationBean, and look for an attribute named salutation on this bean, the getter method for this attribute will be invoked, and its return value will be used to determine the selected value of the rendered HTML tag.

Nested inside the there are a number of tags. These tags generate HTML tags inside the HTML tag generated by . The value of the itemLabel attribute is the value that the user will see while the value of the itemValue attribute will be the value that will be sent to the server when the form is submitted.

All other rows in our grid contain tags, this tag generates an HTML input field of type text, which accept a single line of typed text as input. We explicitly set the id attribute of all of our fields, this allows us to refer to them from the corresponding and fields. We also set the label attribute for all of our tags, this results in more user-friendly error messages.

Some of our fields require a value, these fields have their required attribute set to true, each JSF input field has a required attribute, if we need to require the user to enter a value for this attribute, then we need to set this attribute to true. This attribute is optional, if we don’t explicitly set a value for it, then it defaults to false.

In the last row of our grid, we added an empty tag. The purpose of this tag is to allow adding several tags into a single cell of an . Any tags placed inside this tag are placed inside the same cell of the grid where is placed. In this particular case, all we want to do is to have an “empty” cell in the grid so that the next tag, , is aligned with the input fields in the rendered page.

is used to submit a form to the server. The value of its value attribute is used to generate the text of the rendered button. The value of its action attribute is used to determine what page to display after the button is pressed.

In our example, we are using static navigation. When using JSF static navigation, the value of the action attribute of a command button is hard-coded in the markup.

When using static navigation, the value of the action attribute of corresponds to the name of the page we want to navigate to, minus its .xhtml extension. In our example, when the user clicks on the button, we want to navigate to a file named confirmation.xhtml, therefore we used a value of “confirmation” for its action attribute.

An alternative to static navigation is dynamic navigation. When using dynamic navigation, the value of the action attribute of the command button is a value binding expression resolving to a method returning a String in a managed bean. The method may then return different values based on certain conditions. Navigation would then proceed to a different page depending on the value of the method.

As long as it returns a String, the managed bean method executed when using dynamic navigation can contain any logic inside it, and is frequently used to save data in a managed bean into a database.

When using dynamic navigation, the return value of the method executed when clicking the button must match the name of the page we want to navigate to (again, minus the file extension).

In earlier versions of JSF, it was necessary to specify navigation rules in facesconfig.xml, with the introduction of the conventions introduced in the previous paragraphs, this is no longer necessary.