Personal notes on Java
For other technologies like HTML, CSS, .NET, PHP, etc. check my other blog

To add the JavaServer Faces components to your web page, you need to provide the page access to the two standard tag libraries:

  • the JavaServer Faces HTML tag library: defines tags that represent common HTML user interface components. A list of the available components with code examples can be found here: Adding Components to a Page Using HTML Tags
  • and the JavaServer Faces core tag library: defines tags that perform core actions. A list of the available tags can be found here: Using Core Tags
For example, when creating a Facelets XHTML page, include namespace directives as follows:
<html xmlns="http://www.w3.org/1999/xhtml"
         xmlns:h="http://java.sun.com/jsf/html"
         xmlns:f="http://java.sun.com/jsf/core"
         xmlns:ui="http://java.sun.com/jsf/facelets">

Templates

NOTE: if you use netbeans it can auto-generate stubs for templates and template-clients.
Just go to:

  • File -> New File...- > JavaServer Faces -> Facelets Template
  • File -> New File...- > JavaServer Faces -> Facelets Template Client


The  "A Singleton Session Bean Example: counter" example of the oficial JavaEE Tutorial has a simple example of template usage:
  • The template is a xhtml page that uses
    <ui:insert name="title"></ui:insert> tags to define placeholders. The client pages will then use the name of this placeholders to render content.

    Example (template.xhtml):

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>counter - A singleton session bean example.</title>
        <link href="./css/default.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
        <h1>
            <ui:insert name="title">Default Title</ui:insert>
        </h1>
        <p>
            <ui:insert name="body">Default Body</ui:insert>
        </p>
    </body>
    
</html>

  • The pages that use the template define
     <ui:define name="title"> </ui:define>
    tags. The content (text and expression language) of this tags will be rendered in the placeholders of the template.

    Example (template-client.xhtml):

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html">
    <body>
        
        This text above will not be displayed.
        
        <ui:composition template="/template.xhtml">
            
            This text will not be displayed.
            
            <ui:define name="title">
                This page has been accessed #{count.hitCount} time(s).
            </ui:define>
            
            This text will also not be displayed.
            
            <ui:define name="body">
                Hooray!
            </ui:define>
            
            This text will not be displayed.
            
        </ui:composition>
        
        This text below will also not be displayed.
        
    </body>
</html>

Sem comentários:

Enviar um comentário