Join Patrick Royal for an in-depth discussion in this video Setting up a Faces application, part of Java EE: Servlets and JavaServer Faces.
In this chapter, I'm going to demonstrate setting up and using a JavaServer Faces application. This will be based on much of the same software as we downloaded in chapter one. So if you skipped that chapter, be sure to review the video Setting Up a Runtime Architecture to download the software. We're going to create a new project for this application, so right-click on the Project menu and choose New, then go to Other, then go to the Web, and choose Dynamic Web Project. Click Next and then on the second screen you'll be asked to specify the project name, runtime, web module version, and configuration.
I'll name my Faces application SampleApplication. The runtime should be Apache Tomcat with the most recent version that you downloaded, which is 7.0 for me, as should the dynamic web module version. For the configuration though, you should change it from the default to the latest version of JavaServer Faces. This will ensure that your web project is set up to use JavaServer Faces. With that out of the way, I will click Next again and keep clicking Next until I get to this page. On this page, I'm asked to specify the JavaServer Faces implementation library.
Here, we will need to choose User Library under Type. If you haven't used this function in other projects, you'll most likely get an error message saying there are no user libraries available. Click on the Download Library button on the right side and Eclipse will automatically search for compatible libraries with your JavaServer Faces implementation. For JSF 2.2, the library is Mojarra 2.2.0. So I'll go ahead and click on that and choose Next.
Accept the terms and click Finish to download this library now. When the library is downloaded, ensure that the box is checked next to its name on the list and click Finish. With that, the SampleApplication project will be created. Now let's create a sample page to test that the server is working properly. So far, the process of setting up a Faces application has been similar to that of setting up a servlet, but this is the point at which things diverge. Unlike a servlet, a Faces application is primarily a series of webpages, so it actually requires no Java code to run.
Instead, all of the code will come in the form of XHTML and JSP files. An XHTML file is very similar to an HTML file, but instead of being based on the Standard Generalized Markup Language, or SGML, XHTML is based off of Extensible Markup Language, or XML. Practical differences are fairly small, but XHTML does support shorthand tags for empty element. So you could type something like br and then just close the tag within that one tag.
XHTML also gives error messages when asked to display improperly formatted materials, whereas HTML will simply try to display the file no matter how poorly formatted it is. With that in mind, I'll create a new item in the web content folder from SampleApplication. In the wizard, go to New > Other > Web and then HTML File. Then when you are asked for a name, give it whatever name you want, but change the extension to XHTML. I'll call this one TestPage.xhtml.
When you click Next again, you'll be asked what type of HTML you want to use. Since this is an XHTML file, go ahead and scroll down and choose the XHTML traditional template, and click Finish. Now you'll have a new XHTML file with a preformatted header. To test that this works, let's add a line in the body and we'll give it the syntax h1, This is a header, and it automatically closes the tag for you. Now save your document, right-click on it, and chose Run As > Run on Server.
You'll be asked to specify the server, so chose the Tomcat server that we deployed in chapter one. If everything is set up correctly, then a new page should appear that looks like this. The header text is in bold and the URL is Localhost8080/SampleApplication/Faces/TestPage.xtml. Now that we have a basic Faces application set up, we can start coding. Before we get too far, however, the next video contains a quick refresher on XML coding, so be sure to check it out if you're unfamiliar with XML or just want a review.
- Creating simple Java EE servlets
- Setting up a JavaServer Faces application
- Developing managed beans
- Working with web context
- Handling servlet life-cycle events
- Developing parallel-capable servlets
- Optimizing Java EE code
- Enabling security
- Creating custom components and renderers