Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

JavaServer Faces Technology

JavaServer Faces Technology (page 781) is a user interface framework for building Web applications. The main components of JavaServer Faces technology are:

In support of the GUI components are the following features:

All of this functionality is available via standard Java APIs and XML-based configuration files, and is thus available to applications that aren't based on JSP technology. For the JavaServer Faces applications that do employ JSP technology, the following support is included in JavaServer Faces technology:

You can also create more complex components like grids, tree controls, and the like. One way to accomplish this is by nesting JavaServer Faces component tags inside each other, just like you nest HTML input elements inside a form element. You can also define complex components using the JavaServer Faces API.

The GUI components and well-defined programming model significantly ease the burden of building and maintaining Web applications with server-side GUIs. With minimal effort, you can:

The following code examples contain the JSP page and JavaServer Faces configuration file for the JavaServer Faces version of the example discussed in previous sections:

//greeting.jsp

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head><title>Hello</title></head>
<body bgcolor="white">
<f:use_faces>
  <h2>My name is Duke.  What is yours?</h2>
  <h:graphic_image id="wave_img" url="/duke.waving.gif" /> 
  <h:form id="helloForm" formName="helloForm" >
    <h:input_text id="username"
      columns="25" valueRef="userNameBean.name">
      <f:validate_required />
    </h:input_text>
    <p></p>
    <h:command_button id="submit" label="Submit"
      action="success" commandName="submit" />
    <h:command_button id="reset" label="Reset"
      action="success" commandName="reset" />
  </h:form>
</f:use_faces>
</body>
</html> 

Notice how JavaServer Faces custom tags have replaced HTML elements. The user name set as a request parameter is handled by the h:input text tag in a model object UserNameBean which can be accessed by the response page. The JavaServer Faces tag f:validate_required nested inside the h:input tag causes a standard validator to be invoked which checks that the user name was entered. The Submit and Reset buttons are linked to commands with h:command_button tags.

// faces-config.xml
...
<navigation-rule>
  <from-tree-id>/greeting.jsp</from-tree-id>
  <navigation-case>
    <from-outcome>success</from-outcome>
    <to-tree-id>/response.jsp</to-tree-id>
  </navigation-case>
</navigation-rule>
<managed-bean>
  <managed-bean-name>userNameBean</managed-bean-name>
  <managed-bean-class>hello.UserNameBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
</managed-bean>
... 

The JavaServer Faces configuration file specifies when the application should navigate from the greeting to the response page and creates a bean that contains the user name and stores it in the request scope.

Divider
Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.