Adding JSF as a web framework

JSP was great in its time. It revolutionized the way developers generated dynamic content on the web. But as with everything, time passed and other technologies were developed that made it even easier to generate this same kind of content, and on top of that, overcame many of the limitations that were introduced with JSP.

Enter JSF. It brings a graphical development mentality, similar to Swing's, which relies on a set of reusable UI components and event-driven approach. Instead of totally breaking away from its roots, JSF 2.0 can also be developed in the same way JSP is; in fact, a JSF application is a JSP/servlets application. Typically, Web Applications are developed using the MVC design pattern. This pattern works great with JSF since, in a typical JSF application, the View is the page we write, the Model is the data we wish to show, and the Controller is the FacesServlet. Every request passes through the FacesServlet. The configuration file for the FacesServlet is faces-config.xml.

If you wish to learn more about JSF, visit:

http://www.jcp.org/en/jsr/detail?id=314

http://www.oracle.com/technetwork/java/javaee/download-139288.html

http://www.oracle.com/technetwork/java/javaee/javaserverfaces-139869.html

Getting ready

We will use the sources of the previously created Java EE 6 project. If unsure how to do this, refer to Introduction to Java EE 6 using NetBeans in the Creating a new Project sub-section available in this chapter.

It is also possible to use available sources from your own Java web project.

Java DB must be installed and configured. It is necessary to have a connection to the sample database. Refer to Chapter 04, JDBC and NetBeans, to learn how to do it if unsure.

How to do it...

Navigate to the Projects window:

  1. Right-click on the projects name, in our case FirstJavaEE6Application, and select Properties.
  2. On the Project Properties dialog, select Frameworks, and on Used Frameworks, click on Add....
  3. Under Add a Framework dialog, select JavaServer Faces, and click OK.
  4. Leave all the default values under Libraries and Configuration unchanged.
  5. Click OK.
How to do it...

How it works...

After adding JavaServer Faces, NetBeans creates an index.xhtml under the Web Pages folder.

index.xhtml is our entry point of JSF for the time being.

There's more...

Here is how to run index.xhtml on your browser.

Running index.xhtml

To execute index.xhtml:

Right-click on index.xhtml and select Run File or use the Shift+F6 shortcut when the page is open in the editor.

The output will have GlassFish's start-up logs, and the default system browser will show a Hello from the Facelets page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.49.108