Chapter 3. Java Server Faces Support

3.1. Support for JSF Environments: JSF-RI, MyFaces, Facelets, Shale Or Any Custom

With Red Hat Developer Studio, we don't lock you into any one JavaServer Faces implementation. Select the one you want to use for your project.

When you:

  • Create a new JSF project

  • Add JSF capability to any existing Eclipse project

  • Add JSF capability to any existing project (created outside Eclipse)

You can always select which JSF implementation to use.

You can also create your own custom JSF environments.

Choosing JSF environment

Figure 3.1. Choosing JSF environment

Red Hat Developer Studio will add all the required libraries for the selected version to your project.

3.2. Adding Support for the Oracle ADF Components Library or Any Other Support

Red Hat Developer Studio is set up to work with the ADF Faces Component Library, but still requires a few extra steps to complete the support. First, you will need to download the 2 jar files for the library from the Oracle site down to your system. Then, you just need to follow these steps to add ADF Faces support to your projects:

  • Select Window/Preferences/Red Hat/Library Sets from the menu bar.

  • Select OracleADF in the upper pane.

  • Then, click Add in the lower pane and browse to the location where you have both ADF jar files:
    • adf-faces-api-eaXX.jar

    • adf-faces-impl-eaXX.jar

  • In Preferences, select Custom Capability. Notice that Custom Capability ADF has the OracleADF library set to which we just added the required Jar libraries. We don't need to do anything here.
  • Click OK and then click OK again.

  • After this is done, you can always add in the library files to a project by just right-clicking your project and selecting Red Hat Developer Studio/Add Custom Capability... from the context menu and select a needed capability.

You can add any custom capability (support) to your project using Library Sets in the same fashion.

3.3. Facelets Support

Facelets extends JavaServer Faces by providing a lightweight framework that radically simplifies the design of presentation pages for JSF. Red Hat Developer Studio provides support for Facelets in a variety of ways.

The Create New JSF Project wizard contains templates for creating Facelets-enabled projects based on version 1.1_01 of the JSF Reference Implementation:

Choosing Facelets Environment

Figure 3.2. Choosing Facelets Environment

Once you select the environment, you can select one of the two available templates:

Choosing Facelets Template

Figure 3.3. Choosing Facelets Template

The Red Hat Developer Studio Palette comes with the Facelets components ready to use. A useful tip appears when you hover the mouse cursor over the tag:

Facelets components

Figure 3.4. Facelets components

Code assist for Facelets tags is available when editing xhtml files:

XHTML file code assist

Figure 3.5. XHTML file code assist

You also get code assist for jsfc attribute in any HTML tag and then for JSF components:

Code assist for jsfc attribute

Figure 3.6. Code assist for jsfc attribute

Code assist for JSF components

Figure 3.7. Code assist for JSF components

Using Red Hat Developer Studio OpenOn feature, you can easily navigate between the Facelets templates and other parts of your projects. Just by holding down the Control key while hovering the mouse cursor over a reference to a template, the reference becomes a hyperlink to open that template.

Template hyperlink

Figure 3.8. Template hyperlink

Additionally, when hovering the mouse cursor over Facelets tag attributes, Red Hat Developer Studio provides a pop-up help tip:

Pop-up Help Tip for Facelets Tag Attributes

Figure 3.9. Pop-up Help Tip for Facelets Tag Attributes

See FAQ concerning Facelets support.

3.4. Working with Projects

3.4.1. Creating a New JSF Project

Red Hat Developer Studio provides the following when working with JSF:

  • Create new JSF projects

  • Import (open) existing JSF projects

  • Add JSF capability to any existing Eclipse project

  • Import and add JSF capability to any existing project created outside Eclipse

Red Hat Developer Studio allows you to create brand new JSF projects. A new JSF project will have all JSF libraries, tag libraries and a JSF configuration file.

Red Hat Developer Studio comes with a number of predefined project templates. These templates are flexible and easily customizable.

To create a brand new JSF project, select New/Project:

Choosing a project

Figure 3.10. Choosing a project

Select Red Hat Developer Studio/JSF/JSF Project and click Next:

Choosing a JSF project

Figure 3.11. Choosing a JSF project

On this form enter Project Name. You can also select where to create the project.

JSF Version allows you to select which JSF implementation to use:

Creating a new JSF project

Figure 3.12. Creating a new JSF project

You can also pick a different template on which to base the project. The JSFBlank template will create a standard Web project structure with all JSF capabilities.

The JSFKickStart template will create the same standard structure but will also include a sample application that is ready to run.

You can of course create your own custom templates. More information on templates creation could be found in Chapter 5.

Choosing JSF template

Figure 3.13. Choosing JSF template

On the next screen select what Servlet version to use and whether to register this application with JBoss AS (or other server) for running and testing of your application.

Context Path is the name under which the application will be deployed.

The Runtime value tells Eclipse where to find Web libraries in order to build (compile) the project. It is not possible to finish project creation without selecting Runtime. If you don't have any values, select New... to add new Runtime.

Target Server allows you specify whether to deploy the application. The Target Server corresponds to the Runtime value selected above.

If you don't want to deploy the application, uncheck this value:

Registering the project on server

Figure 3.14. Registering the project on server

When you are all done, you should have the project that has been created in the Package Explorer view:

A new project in the Package Explorer

Figure 3.15. A new project in the Package Explorer

At this point you can open faces-config.xml and start working on your application.

3.4.2. Importing Existing JSF Projects with Any Structure

For detailed information on migration projects to Red Hat Developer Studio see Migration Guide.

3.4.3. Adding JSF Capability to Any Existing Eclipse Project

With Red Hat Developer Studio you can add JSF capability (JSF libraries, tag libraries) to any existing Eclipse project in your workspace.

By adding JSF Capability to your project, you can now edit any file using Red Hat Developer Studio editors, such as JSF configuration editor, Red Hat JSP editor and any others:

Web Project

Figure 3.16. Web Project

Right click the project and select Red Hat Developer Studio/Add JSF Capabilities. This will start the process of adding all necessary libraries, files to make this a Web JSF project:

Adding JSF capabilities

Figure 3.17. Adding JSF capabilities

The wizard will first show you the web.xml file location and the project name:

Project location

Figure 3.18. Project location

On the last form you can set the different folders for your project as well as register this application with a servlet container.

Make sure to select Add JSF Libraries for Red Hat Developer Studio to add all required JSF related libraries to this project.

Context Path is the name under which the application will be deployed.

The Runtime value tells Eclipse where to find Web libraries in order to build (compile) the project. It is not possible to finish project import without selecting Runtime. If you don't have any values, select New... to add new Runtime.

Target Server allows you specify whether to deploy the application. The Target Server corresponds to the Runtime value selected above.

If you don't want to deploy the application, uncheck this value:

Project folders

Figure 3.19. Project folders

Once your project is imported you can see that JSF related libraries have been added to your project jsf-api.jar and jsf-impl.jar.

You are now ready to work with JSF by creating a new JSF configuration file:

Creating a new JSF configuration file

Figure 3.20. Creating a new JSF configuration file

3.4.4. Adding Your Own Project Templates

Red Hat Developer Studio has a powerful templating capability for creating new and importing existing Struts and JSF projects. This templating facility has a variety of aspects to consider. But, let's start with the most straightforward case.

Let's say you have a project that you want to use as the basis for a new template. Follow these steps to make a template out of it:

  • In the Project Explorer, right-click the project and select Red Hat Developer Studio/Save as Template.
  • In the first dialog box, you can choose a name for the template (defaults to the project name) and confirm what run-time implementation of the project's technology will be used.
  • Select Next and you will be sent to a dialog box with your project structure displayed with check boxes. Here you can check only those parts and files in your project directory that should be part of the template.
  • At this point, unless you want to designate some extra files as having Velocity template coding inside them, you should select Finish.

That's it. Now, you can use this template with any new or imported project that uses the same run-time implementation as the project you turned into a template.

3.5. Graphical Editor and Viewing for JSF Configuration Files

The JSF configuration file editor has three main viewers (modes):

  • Diagram

  • Tree

  • Source

The modes can be selected via the tabs at the bottom of the editor.

The JSF configuration editor also comes with a very useful OpenOn selection feature.

3.5.1. Diagram

The Diagram view displays the navigation rules in the JSF configuration file:

Diagram View

Figure 3.21. Diagram View

3.5.2. Creating New View (Page)

To create a new page (view), you can click the page icon on this toolbar and then click anywhere on the diagram. A New Page Wizard will appear.

To create a transition (rule) connecting pages:

  • Select the transition icon from the toolbar (2nd from the bottom).

  • Click the source page.

  • Click the target page.

A transition will appear between the two pages:

Transition between JSP pages

Figure 3.22. Transition between JSP pages

It is also possible to create a new page by right-clicking anywhere on the diagram and selecting New View.

Creating a new view

Figure 3.23. Creating a new view

To edit an existing transition, first select the transition line. Then, place the mouse cursor over the last black dot (on the target page). The mouse cursor will change to a big +. At this point, drag the line to a new target page:

Editing transition between views

Figure 3.24. Editing transition between views

3.5.3. Tree View

The Tree mode for the editor displays all JSF application artifacts referenced in the configuration file in a tree format. Select any node and its properties will appear in the right-hand area:

Tree view

Figure 3.25. Tree view

To edit, right-click any node and select one of the available actions in the context menu. You can also edit in the properties window to the right:

Editing in Tree View

Figure 3.26. Editing in Tree View

3.5.4. Source View

The Source mode for the editor displays a text view of the JSF configuration file. All three viewers are always synchronized, so any changes made in one of the viewers will immediately appear in the others:

Source view

Figure 3.27. Source view

3.5.5. Content Assist

Content assist is always available in the Source viewer:

Content assist in Source view

Figure 3.28. Content assist in Source view

3.5.6. Error Reporting

Errors will be reported by Red Hat Developer Studio's verification facility:

Error reporting in Source view

Figure 3.29. Error reporting in Source view

Other errors are also reported.

Others errors reporting

Figure 3.30. Others errors reporting

You can also work in the Source viewer with the help of the Outline view. The Outline views show a tree structure of the JSF configuration file. Simply select any element in the Outline view, and it will jump to the same place in the Source viewer.

Outline view

Figure 3.31. Outline view

If your diagram is large, within Outline view you can switch to a Diagram Navigator mode by selecting the middle icon at the top of the view window. It allows you to easily move around the diagram. Just move the blue area in any direction, and the diagram on the left will also move:

Outline view for diagram

Figure 3.32. Outline view for diagram

You can also edit in the Tree viewer with the help of the Properties view as shown below:

Properties view

Figure 3.33. Properties view

3.6. Managed Beans

3.6.1. Code Generation for Managed Beans

Red Hat Developer Studio gives you lots of power to work with managed beans.

  • Add and generate code for new managed beans

    • Generate code for attributes and getter/setter methods

  • Add existing managed beans to JSF configuration file

To start, create a new managed bean in JSF configuration file editor, in the Tree view.

Creation of new managed bean

Figure 3.34. Creation of new managed bean

When you define a new managed bean, make sure that Generate Source Code is checked as shown in the figure below:

Figure 3.35. 

After the Java class has been generated you can open it for additional editing. There are two ways to open the Java class:

  • Click on Managed-Bean-Class link in the editor -or-

  • Right click the managed bean and select Open Source

Opening of created managed bean

Figure 3.36. Opening of created managed bean

The generated Java source:

Java source code

Figure 3.37. Java source code

You can also generate source code for properties - this also includes getter and setter methods:

Generation of source code for properties

Figure 3.38. Generation of source code for properties

Make sure that all the check boxes are selected:

  • Add Java property
  • Generate Getter
  • Generate Setter
"Add property" form

Figure 3.39. "Add property" form

Once the generation is complete, you can open the file and see the added property with getter and setter methods:

Generated java source code for property

Figure 3.40. Generated java source code for property

3.6.2. Add Existing Java Beans to a JSF Configuration File

If you already have a Java bean you can easily add it to a JSF configuration file.

You would start the same way you would create a new managed bean.

Creation of new managed bean

Figure 3.41. Creation of new managed bean

On the next screen, type in or browse for your existing Java class.

Once the class is set, it's Name will be set as well. You can always change the name.

Notice that Generate Source Code option is not available as the Java class already exists.

New managed bean form

Figure 3.42. New managed bean form

On the next screen you will see all properties from this class. Check the ones you want to add managed properties to a JSF configuration file.

If you don't want to add any, just click Finish.

Selection of bean's properties.

Figure 3.43. Selection of bean's properties.

3.7. Create and Register a Custom Converter

  1. In the Project Explorer view open faces-config.xml and select Tree tab.

Converters

Figure 3.44. Converters

  1. Select Converters and click on Add button.

  2. Type the name of your converter in the Converter-id field and name of the class for converters. After clicking Finish button your custom converter is registered under entered name.

Add converter form

Figure 3.45. Add converter form

  1. Let's create converter class. In the Converter section you should see your Converter-id and Converter-class. Click on Converter-class to generate the source code.

Generation of source code for converter class

Figure 3.46. Generation of source code for converter class

  1. Java class will be created automatically. Leave everything without changes and click Finish.

New java class form

Figure 3.47. New java class form

  1. To open converter class click again on Converter-class link in the Converter section. Now you are able to write business logic of converter.

Figure 3.48. 

3.8. Create and Register a Custom Validator

  1. In the Project Explorer view open faces-config.xml and select Tree tab.

Figure 3.49. 

  1. Select Validators and click on Add button.

  2. Type the name of your validator in the Valifator-id field and name of the class for validators. After clicking Finish button your custom validator is registered under entered name.

Figure 3.50. 

  1. Let's create validator class. In the Validator section you should see your Validator-id and Validator-class. Click on Validator-class to generate the source code.

Figure 3.51. 

  1. Java class will be created automatically. Leave everything without changes and click Finish.

Figure 3.52. 

  1. To open validator class click again on Validator-class in the Validator section. Now you are able to write business logic of validator.

Figure 3.53. 

3.9. Create and Register Referenced Beans

  1. In the Project Explorer view open faces-config.xml and select Tree tab.

Figure 3.54. 

  1. Select Referenced Beans and click on Add button.

  2. Type in the name of your Referenced Bean and type in or select Referenced-Bean-Class by using Browse button.

Figure 3.55. 

  1. In the Referenced Bean section you should see your Referenced-Bean-Name and Referenced-Bean-Class. Click on the link to open the Java creation wizard.

Figure 3.56. 

  1. Java class will be created automatically. Leave everything without changes and click Finish.

Figure 3.57. 

  1. To open Referenced Bean class click again on Referenced-Bean-Class in the Referenced Bean section. Now you are able to write business logic of Referenced Bean.

Figure 3.58. 

3.10. Adding Custom JSF Tags to the Red Hat Palette

3.10.1. Adding Tag Libraries

There are two ways to add any custom or 3rd party tag library to the Red Hat Palette:

  • Drag-and-drop from the Web Projects view

  • The Import button on the Red Hat Palette

Before you can add your custom component library, you need to make sure it is included in your project. Either place the .tld file or the .jar that includes your tag library under the lib folder in your project. Restart Eclipse.

The best way to add any custom library is by using the Custom Capability feature. It is also possible to just copy the Jar file directly under project WEB-INF\lib directory.

3.10.2. Drag-and-Drop

Switch to the Web Projects view and expand the Tag Libraries folder. If the view not active, select Window/Show View/Web Projects from the menu bar.

Web Projects view

Figure 3.59. Web Projects view

Also make sure that the Red Hat Palette is open. Select the tag library that you want to add and simply drag-and-drop it on to the Red Hat Palette.

You will see the following dialog window. As you can see Red Hat Developer Studio takes care of all the details. You just need to set the Group name to which to add this tag library. You can either add this tag library to an existing Group or just create a new one.

Import tags from TLD file form

Figure 3.60. Import tags from TLD file form

Once you are finished, you will see the new tag library added to the Red Hat Palette.

Red Hat Palette with new tag library

Figure 3.61. Red Hat Palette with new tag library

3.10.3. Import Button

Import button

Figure 3.62. Import button

On this screen you can select Browse to locate the tag library that you want to add.

Import tags from TLD file form

Figure 3.63. Import tags from TLD file form

Edit TLD form

Figure 3.64. Edit TLD form