Tuesday, May 04, 2010

Migrating JSF 1 composition components to JSF 2

In JSF 1, composition components are defined using <ui:composition>. The “template” attribute of ui:composition is optional. Meaning the composition component can opt to confirm to a template or not. So these components could be classified into those that confirmed to a template and those that did not.

 

Type 1: Composition components that confirm to a template:

A composition component confirms to a template by specifying the template name in the template attribute (e.g: ui:composition template=”section.xtml”).  Then it overrides certain aspects of the template. In the example 1 below, section.xhtml, which is the template, defines “contents” (at 1). The composition component defined in Student.xhtml specifies section.xhtml as its template at 2. Section.xhtml further defines-its-own or overrides “contents” at 3. Had there been no 3, 4 would get rendered.

 

Example 1

 

Section.xhtml

<ui:composition>

  <div…> …

  <ui:insert name=”contents”> 1

    This is the default text for “contents”. This text will be rendered when a component does not override this ui:insert with a ui:define name=”contents” 4

  </ui:insert>

</ui:composition>

 

Student.xhtml

<ui:composition template=”section.xhtml”> 2

  <ui:define name=”contents”> 3

    <h:outputText…..> <!—contents of the student section à

  </ui:define>

</ui:composition>

 

Type 2: Composition components that *do not* confirm to a template:

A composition component can be stand alone too. In this case the ui:composition tag will reference no template.

outputField.xhtml

<ui:composition >

  <h:outputText

    Value=”#{fieldLabel}”/>

  <h:inputText

    Value=”#{field.property}”/>

</ui:composition >

 

Company.xhtml

<ui:composition …>

  <a:outputField

    fieldLabel=”Company Name”

    field=”#{company}”

    property=”name”/>

</ui:”composition>

 

Migrating to JSF 2:

Type 2 components above are good candidates to become composite components in JSF 2 (as composite components, like type 2 composition components, are stand-alone and confirm to no templates).

No comments: