JSF Overview


JavaServer Faces technology (JSF) was created under the auspices of JSR 127, which was finalized in 2004. JSF includes a set of APIs for representing UI components and their state, event handling, input validation, navigation, internationalization, and accessibility. A custom tag library provides the capability to define a JSF interface within a JavaServer Page (JSP). The component model and the standardization of the technology have led to increasing adoption.

A typical interaction begins with a form submission being routed through the JSF servlet to a method on a managed bean (JavaBeans component). The managed bean performs the appropriate action typically using a business delegate. After invoking the appropriate delegate method, control is returned to the JSF controller. The controller then forwards to the appropriate view using a set of navigation rules specified in the configuration file.

Following is an list of some important concepts in JSF technology:

  • Managed Bean. A managed bean is a component associated with a UI component. The managed bean defines JavaBeans properties that are bound to a component value (typically a mapping between a form element and a value) or an instance. A managed bean can also be used to perform functions such as validation and event handling.

  • Configuration File. The XML configuration file provides the capability to specify behavior, such as that of page navigation and managed beans, declaratively.

  • Tag Libraries. The JavaServer Faces tag libraries provide access to the UI components and the capability to reference managed beans.

  • UI Components. The JavaServer Faces component model provides the capability to create reusable components that are independent of their rendering. The UI components can be extended by component developers to create new components.

  • Navigation. The page flow is specified in the navigation section of the configuration file. The page flow allows the next page to be specified in terms of a logical outcome. This allows the actual flow to change without affecting the application code.

This section is intended to serve as a brief overview to JSF technology; additional information can be found at (http://java.sun.com/j2ee/javaserverfaces/index.jsp).

Creating a Web Project with JSF Support

JSF support can be added during project creation when using the New Web Application wizard.

JSF support is enabled by selecting the JSF checkbox in the wizard (see Figure 9-1). This allows additional JSF-specific information to be specified:

  • JSF Servlet Name. This text field contains the name used in the servlet entry that will be generated for the FacesServlet class. The servlet entry will also contain a reference to the generated configuration file.

  • Servlet URL Mapping. This text field provides the capability to configure the set of URLs that are mapped to the Faces Servlet.

  • Validate XML. If this value is checked, the configuration file will be validated when parsed by the FacesServlet class.

  • Verify Objects. If this value is checked, all objects referenced from the configuration file will be checked to ensure successful creation. The above two properties provide early fail fast behavior and are useful to turn on at development time. Both of these properties are context parameters for the FacesServlet class, so they can easily be changed later.

Figure 9-1. New Web Application wizard with JSF framework support


After you complete the wizard, the standard web application project will be augmented to support JSF development as follows (see Figure 9-2 for the JSF enabled web application project):

  • faces-config.xml is generated in the configuration files area. The facesconfig.xml has the appropriate document type specified as well as the root element.

  • The default index.jsp is augmented to refer to the welcomeJSF.jsp. The href tag is prefixed with the URL pattern specified in the wizard so the request will be intercepted by the FacesServlet class.

  • The JSF libraries are added to the libraries directory. The libraries will be included by default when packaging the WAR file in WEB-INF/lib directory. If the libraries have already been deployed to the server or will instead be deployed as part of a Java EE application, the project's Properties dialog box can be used to set which libraries will be packaged with the WAR file.

  • The web.xml file is updated with a servlet entry referencing the FacesServlet class. The context parameters such as the location for the facesconfig.xml file (and the appropriate context parameters as specified in the wizard for verifying objects and validating the configuration file) are also generated. A servlet mapping referencing the FacesServlet class will also be generated with the URL pattern specified in the wizard.

Figure 9-2. Web Application Project using the JSF framework


After you complete the wizard, code completion will be enabled for JSF tag libraries, JSF Java classes, and the faces-config.xml file.

Support for JSF can also be added to an existing web project using the Project Properties Framework tab (see Figure 9-3).

Figure 9-3. Project Properties dialog box, Framework tab


Managed Bean Support

A managed bean provides the capability to perform data binding and other functions such as validation and serving as a controller. The IDE provides a wizard to automate creation of managed bean classes.

To create a managed bean class:

  1. Open the New JSF Managed Bean wizard by right-clicking the node of a JSF-enabled web application project, choosing New | File/Folder, selecting the Web category, and selecting JSF Managed Bean Action.

  2. In the Name and Location section of the wizard (shown in Figure 9-4), specify the name and package of the generated managed bean class. The JSF configuration file must also be specified; the default is typically correct. The scope of the managed bean must be specified. An optional description of the managed bean can be specified in the description field.

    Figure 9-4. New JSF Managed Bean wizard

  3. When the Finish button is clicked, the wizard will generate the managed bean classes along with a managed bean entry in the JSF configuration file.

The node for the managed bean class provides a wizard to help generate properties. A quick way to navigate to this node is to press Ctrl-Shift-1 from the Source Editor. The Bean Patterns node is a subnode that provides the capability to add various JavaBeans properties.


JSF Configuration File Support

In addition to code completion, the JSF configuration file provides several wizards launched from the context menu of the Source Editor. The wizards provided include:

  • Add Navigation Rule. Encapsulates the possible transitions from the specified page using the JSF navigation-rule element.

  • Add Navigation Case. Adds a single transition to an existing navigation rule or generates a navigation-rule if one does not exist. The wizard allows both action and outcome guards to be specified.

  • Add Managed Bean. Automates generation of a managed bean entry. This wizard does not generate the managed bean Java class, so this is useful when referencing existing managed beans.

When you open the JSF configuration file in the Source Editor, you can navigate directly to externally referenced artifacts (such as Java and JSP files). Jumping to the source code is achieved by holding down the Ctrl key, moving the mouse over the identifier until it is underlined in blue, and then clicking it. For example, holding down the Ctrl key and moving the mouse over a managed bean class element displays the class name underlined in blue (See Figure 9-5), and clicking the identifier opens the Java file in the Source Editor.


Figure 9-5. Hyperlinking in faces-config.xml file




NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 279

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net