Enterprise Java Development@TOPIC@

Chapter 104. JavaServer Faces

104.1. Controller Bean
104.2. JSF Page
104.3. WAR Structure and Descriptors
104.4. Summary

Figure 104.1. JSF Basic Elements

JSF Basic Elements


  • CDI @Named used to specify name for page to use

  • Class required to implement Serializable unless RequestScoped


  • Page refers to @Name.property and accesses using associated methods (SellerController.getProperty() and Product.setName() in this case)

  • Controller holds this state until action called


  • Page refers to action using @Name.methodName

  • Action methods take no arguments. They act on state supplied by setters.

  • Action methods return a String result used for navigation

  • Action methods may interact with business methods/resources to perform task



  • Useful in interacting with back-end resources and other page controllers

  • ProductCatalog is a Java business interface that is implemented by a DAO and EJB.

  • @Inject tells the provider to inject an implementation that matches the specified type

  • @Tx is defined within the application to further qualify which provider of the ProductCatalog interface is used

  • ErrorController is a sibling PageController for crudely displaying error messages and stack traces


  • Simple example contains business data state and data access methods

Many mark-up languages can be used (e.g., JSP, XHTML, etc.)


  • Page uses XHTML markup -- must be a legacy XML document

  • Page uses JSF types xmlns:h="http://xmlns.jcp.org/jsf/html" to interact with framework and controller bean



  • Standard html begin/end tags and basic HTML document formatting

  • JSF provides replacement h:head and h:body tags


  • Form bound to UIForm instance supplied by provider. Permits controller bean to manage page form.

  • Form will display the starting state of Product

  • Form will update the properties of Product

  • commandButton registered to action method in controller


  • selectMenu calls getCategories() to obtain choice list

  • selectMenu will store that value in SellerController.getProduct().setCatagory()


  • commandButton calls SellerController.add() action

  • SellerController.add() action acts on the state and provides a navigation result

Figure 104.14. Example Table with Row Actions


    <h:form>
     <h:dataTable value="#{sellerController.products}" var="p">
...         
            <h:column>
                <f:facet name="header">
                    <h:column>
                        <h:outputText value="Name"></h:outputText>
                    </h:column>
                </f:facet>
                <h:outputText value="#{p.name}"/>
            </h:column>
...         
         <h:column>
             <f:facet name="header">
                 <h:column>
                     <h:outputText value="Actions"></h:outputText>
                 </h:column>
             </f:facet>
             <h:panelGrid columns="2">
                 <h:commandButton value="save" action="#{sellerController.save}">
                     <f:setPropertyActionListener 
                         target="#{sellerController.product}" 
                         value="#{p}"/>
                 </h:commandButton>
             </h:panelGrid>
         </h:column>
     </h:dataTable>
    </h:form>
    public String save() {

        product.setSeller(user.getMember());
        product = catalog.addProduct(product);  //EJB call
        Collections.sort(products, new Product.ProductASC());
        return null;
    }

  • Table displays column and row data

  • Example shows columns for each property of the product

  • Example adds commandButton to each row to control actions with back-end


  • index.xhtml and seller-products.xhtml meant to be bookmarked and called at any time -- please in public web space

  • error.xhtml meant to be navigated to from other controllers. Placed in non-public WEB-INF directory

  • faces-config.xml can be used for navigation flow and pre-dates @Annotations

  • beans.xml enables CDI processing


  • FacesServlet declared to process all JSF pages

  • PROJECT_STAGE=Development enables extra debug in output


  • Provides a top level entry page

  • Example provides link(s) to more detailed pages

  • This and follow-on pages may or may not reference a controller


  • Any "error" output from seller-products will navigate to error page


  • CDI activated when file exists (blank, empty, or populated)