SlideShare a Scribd company logo
© 2011 Marty Hall




      JSF 2 0: Building
          2.0:
    Composite Components
            Originals of Slides and Source Code for Examples:
              http://www.coreservlets.com/JSF Tutorial/jsf2/
              http://www.coreservlets.com/JSF-Tutorial/jsf2/
               Customized Java EE Training: http://courses.coreservlets.com/
  Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
  Developed and taught by well-known author and developer. At public venues or onsite at your location.




                                                                                               © 2011 Marty Hall




  For live training on JSF 2.x, please see
courses at htt //
          t http://courses.coreservlets.com/.
                                   l t     /
     Taught by the author of Core Servlets and JSP, More
    Servlets and JSP and this tutorial. Available at public
                 JSP,          tutorial
    venues, or customized versions can be held on-site at
                      your organization.
  •C
   Courses d
           developed and t
               l   d d taught b M t H ll
                           ht by Marty Hall
        – JSF, servlets/JSP, Ajax, jQuery, Android development, Java 6 programming, custom mix of topics
        – Ajax courses can concentrate on 1EE Training: http://courses.coreservlets.com/several
                 Customized Java library (jQuery, Prototype/Scriptaculous, Ext-JS, Dojo, etc.) or survey
  • Courses developed and taught by coreservlets.com experts (edited by Marty)
  Servlets, JSP, Hibernate/JPA, EJB3, GWT, jQuery, GWT, RESTful Web Services RESTful Web Services, Android.
        – Spring, JSF 2.0, Java 6, Ajax, SOAP-based and Spring, Hibernate,
  Developed and taught by well-known author and developer. At public venues or onsite at your location.
                                     Contact hall@coreservlets.com for details
Topics in This Section
    •   Idea
    •   Basic components
    •   Passing values to components
    •   Using ui:repeat inside components
    •   Nested components
    •   Handling relative URLs in components




5




                                                                                                © 2011 Marty Hall




                                      Overview

                       Customized Java EE Training: http://courses.coreservlets.com/
          Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
          Developed and taught by well-known author and developer. At public venues or onsite at your location.
Idea
    • JSF 2.0 added “composite components”
      – Reusable, named chunks of facelets code
      – Can pass arguments
    • Similar to JSP tag files
      – But have use of JSF tags and JSF expression language
      – Real components: can have listeners events facets etc.
                                   listeners, events, facets, etc
    • Abbreviated example
      – resources/utils/myComponent.xhtml (snippet)
                         y   p            ( pp )
           <composite:implementation>
             This is a test
           </composite:implementation>
      – some-page.xhtml (snippet)
7
           <utils:myComponent/>




    Making and Using Custom
    Components: Overview
    • Define a component
      –   Put foo.xhtml in “resources/bar” folder
      –   Define available attributes inside composite:interface
      –   Build output inside composite:implementation
      –   Use composite: namespace in component file
           • <html …
                 xmlns:composite="http://java.sun.com/jsf/composite">
    • Use component namespace in facelets page
      – <html …
           xmlns:bar="http://java.sun.com/jsf/composite/bar">
    • Use the component in facelets page
                 p                  p g
      – <bar:foo ... />
8
© 2011 Marty Hall




                     Basic Components

                            Customized Java EE Training: http://courses.coreservlets.com/
               Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
               Developed and taught by well-known author and developer. At public venues or onsite at your location.




      Super-Quick Example
        p             p
     • Component File                                           • Main File
     resources/utils/myComponent.xhtml                          some-page.xhtml
     <!DOCTYPE …>                                               <!DOCTYPE …>
     <html …                                                    <html …
        xmlns:composite=                                           xmlns:utils=
                                                                       l    til
          http://java.sun.com/jsf/composite">                        "http://java.sun.com/jsf/composite/utils">
     <head><title>…</title></head>                              <h:head><title>Composite Components</title>
     <body>
          y                                                     …
                                                                <h:body>…
     <composite:interface/>                                     <utils:myComponent/>…
                                                                </h:body></html>
     <composite:implementation>
      This is a test
     </composite:implementation>
     </body></html>


10
Component File: Summary
     • In subfolder of “resources” folder
        – E.g., resources/someDir/someFile.xhtml
           • We already used “resources” in section on relocatable
             resources in tutorial on page templating
     • Basic structure
        – Looks like xhtml file
           • head and body tags are simply to satisfy editor
           • Declares composite: namespace in <html…> start tag
              – <html … xmlns:composite="http://java.sun.com/jsf/composite">
        – Contains composite:interface
           • Defines attributes used (if any)
        – Contains composite:implementation
           • Creates final output
           • Only content of composite:implementation becomes output
11




      Component File: Outline
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …>
     <html xmlns="http://www w3 org/1999/xhtml"
           xmlns http://www.w3.org/1999/xhtml
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:composite="http://java.sun.com/jsf/composite">
     <head><title>(For validation only)</title></head>
                  (                  y)
     <body>

     <composite:interface>
       <!-- Declare attributes here -->
     </composite:interface>

     <composite:implementation>
     <      it i l      t ti >
       <!-- Create output here -->
     </composite:implementation>

     </body></html>

12
Main File: Summary
     • Regular facelets file (someFile.xhtml)
        – No special folder name.
        – Accessed with someFile.jsf (or someFile.faces, etc.) in
          normal manner.
                 manner
     • Basic structure
        – Normal facelets structure
            • h:head, h:body, and other normal JSF constructs
        – Declares component namespace in <html…> start tag
            xmlns:someDir="http://java.sun.com/jsf/composite/someDir"
              l       Di "htt //j             /j f/      it /    Di "
               – Technically, there is no requirement that the namespace (the part
                 after xmlns:) match the folder name. But, it is traditional to do so.
        – Use component
            • <someDir:someFile/> (if no attributes)
13          • <someDir:someFile att1="…" … /> (if it uses attributes)




      Main File: Outline
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …>
     <html xmlns http://www.w3.org/1999/xhtml
           xmlns="http://www.w3.org/1999/xhtml"
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:someDir="http://java.sun.com/jsf/composite/someDir">
     <h:head>
     …
     </h:head>
     <h:body>
     …
     <someDir:someFile/>
     …
     </h:body></html>




14
Example
     • Make a bulleted list of request info
        –   Requesting host
        –   User agent
        –   Current Locale
        –   Session ID
     • May want to reuse list, so make component
        – resources/utils/info1.xhtml
             • Generates list inside composite:implementation
        – composite-components.xhtml
             • Declares utils: namespace
             • Uses <utils:info1/>
                     ut s o /


15




      Component File
      (resources/utils/info1.xhtml)
      (resources/utils/info1 xhtml)
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …>
     <html xmlns="http://www.w3.org/1999/xhtml"
                      p //         g/    /
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:composite="http://java.sun.com/jsf/composite">
     <head><title>(For validation only)</title></head>
     <body>

     <composite:interface/> <!-- No attributes -->

     <composite:implementation>
      <ul>
          <li>Requesting host: #{request.remoteHost}</li>
          <li>Requesting client type:<br/>
              #{header["User-Agent"]}</li>
          <li>Locale: #{view.locale}</li>
          <li>Session id: #{session.id}</li>
       </ul>
        /
     </composite:implementation>

16   </body></html>
Main File
      (composite-components.xhtml)
      (composite-components xhtml)
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …>
     <html xmlns="http://www w3 org/1999/xhtml"
           xmlns= http://www.w3.org/1999/xhtml
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:utils="http://java.sun.com/jsf/composite/utils">
     <h:head>
     …
     </h:head>
           y
     <h:body>
     …
     <fieldset>
     <legend>Basic Composite Components</legend>
     <utils:info1/>
     </fieldset>
     …
     </h:body></html>


17




      Results




18
© 2011 Marty Hall




                           Components
                          with Attributes

                       Customized Java EE Training: http://courses.coreservlets.com/
          Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
          Developed and taught by well-known author and developer. At public venues or onsite at your location.




     Component File: Summary
     • In subfolder of “resources” folder
       – E.g., resources/someDir/someFile.xhtml
     • Basic structure
       – Looks like xhtml file as before
          • Declares composite: namespace as before
       – Contains composite:interface
          • D fi
            Defines attributes used
                     tt ib t      d
                <composite:interface>
                   <composite:attribute name="attributeName"/>
                </composite:interface>
                 /      i i      f
       – Contains composite:implementation
          • Creates final output. Refers to attributes with cc.attrs
                <composite:implementation>
                …#{cc.attrs.attributeName}…
20
                </composite:implementation>
More on cc.attrs
     • “cc” is predefined variable
        – Stands for “composite component”
        – Refers to top-level component in component file, of type
          UINamingContainer
     • Main property is “attrs”
        – This is a specially defined attribute that contains a Map of
                     p      y                                     p
          the attributes used to call the component
           • Main page: <someDir:someFile message="test"/>
           • Component: <h2>Message: #{cc.attrs.message}</h2>
                                      #{cc attrs message}</h2>
     • Other useful attributes
        – parent, children, clientId
          p     ,         ,

21




      Component File: Outline
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …>
     <html xmlns="http://www w3 org/1999/xhtml"
           xmlns= http://www.w3.org/1999/xhtml
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:composite="http://java.sun.com/jsf/composite">
     <head><title>(For validation only)</title></head>
     <body>

     <composite:interface>
       <composite:attribute name="attributeName"/>
     <composite:interface>

     <composite:implementation>
       Blah #{cc.attrs.attributeName} Blah
     </composite:implementation>
          p        p

     </body></html>
22
More on composite:attribute
     • Basic usage
          <composite:interface>
             <composite:attribute name="attributeName"/>
          </composite:interface>
     • Basic attributes
       – name
          • Attribute name as used in component in main page
       – required
          • Is attribute required? (default: false)
              – Warning! Mojarra 2.0.2 does not enforce this! Fixed in 2.0.3.
       –d f l
        default
          • For non-required attributes, value to use if none supplied
     • Advanced attributes
       – type, method-signature, targets
          • For attributes that expect methods or complex values
23




     Mini Example
                                                              Reminder: required is not properly
     • composite:interface                                    enforced in Mojarra 2.0.2 and earlier.

          <composite:attribute name="msg1" required="true"/>
          <composite:attribute name="msg2" default="Hi"/>
     • composite:implementation
             it i   l    t ti
          <h2>Message 1: "#{cc.attrs.msg1}"</h2>
          <h2>Message 2: "#{cc.attrs.msg2}"</h2>
           h2 Message     #{cc.attrs.msg2} /h2
     • Main page
          <utils:showMessages msg1="Hello, world"/>
     • Result
          Message 1: "Hello, world"
          Message 2: "Hi"

24
Example
     • Make a bulleted list of request info as before
        – Requesting host, user agent, current Locale, session ID
     • Support styleClass attribute
        – T specify CSS style of resultant <ul> li
          To    if         l f       l       l list
     • Component
        – resources/utils/info2 xhtml
          resources/utils/info2.xhtml
           • Declares styleClass inside composite:interface
           • Generates list inside composite:implementation
        – composite-components.xhtml
           • Declares utils: namespace
           • Uses <utils:info2 styleClass="some-css-name"/>
                   ut s o sty eC ass so e css a e /


25




      Component File
      (resources/utils/info2.xhtml)
      (resources/utils/info2 xhtml)
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …>
                     p //         g/    /
     <html xmlns="http://www.w3.org/1999/xhtml"
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:composite="http://java.sun.com/jsf/composite">
     …
     <composite:interface>
       <composite:attribute name="styleClass"/>
     </composite:interface>

     <composite:implementation>
       <ul class="#{cc.attrs.styleClass}">
         <li>Requesting host: #{request.remoteHost}</li>
         <li>Requesting client type:
             <br/> #{header["User-Agent"]}</li>
         <li>Locale: #{view.locale}</li>
         <li>Session id: #{session id}</li>
                         #{session.id}</li>
       </ul>
     </composite:implementation>
26   </body></html>
Main File
      (composite-components.xhtml)
      (composite-components xhtml)
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …>
     <html xmlns="http://www w3 org/1999/xhtml"
           xmlns= http://www.w3.org/1999/xhtml
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:utils="http://java.sun.com/jsf/composite/utils">
     <h:head>
     …
     </h:head>
           y
     <h:body>
     …
     <fieldset>
     <legend>Composite Components with Attributes</legend>
     <utils:info2 styleClass="blue"/>
     </fieldset>
     …
     </h:body></html>


27




      CSS File
     .italic { font-style: italic; }
     .blue { font weight: bold; color: blue; }
       blue  font-weight:
     …




28
Results




29




                                                                                             © 2011 Marty Hall




       Using ui:repeat with
      Composite Components
         p           p

                    Customized Java EE Training: http://courses.coreservlets.com/
       Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
       Developed and taught by well-known author and developer. At public venues or onsite at your location.
Component File: Summary
     • In subfolder of “resources” folder
       – E.g., resources/someDir/someFile.xhtml
     • Basic structure
       – Looks like xhtml file as before
          • Declare composite: and ui: namespaces
              – <html … xmlns:composite http://java.sun.com/jsf/composite
                 html   xmlns:composite="http://java.sun.com/jsf/composite“
                        xmlns:ui="http://java.sun.com/jsf/facelets">
       – Contains composite:interface
          • Defines attributes as before
       – Contains composite:implementation
          • Creates final output
              – Uses #{cc.attrs.attributeName} as before
              – Uses ui:repeat to build output
31




     More on ui:repeat
     • Sample usage
          <ul>
          <ui:repeat var="color" value="#{item.availableColors}">
            <li>#{color}</li>
          </ui:repeat>
           /u epea
          </ul>
     • More attributes
          <ui:repeat var="someVar"
           ui:repeat var someVar
                     value="#{someBean.someCollection}"
                     varStatus="statusVariable"
                     offset="…"
                     size="…"
                     step="…">
            …
          </ui:repeat>
     • More details on ui:repeat
32
       – See tutorial section on looping
Example
     • Pass in array or List
        – Turn it into <ul> list showing individual entries
     • Attributes
        – value (EL expression to designate collection)
             l              i      d i          ll i )
        – styleClass (to specify CSS style of resultant <ul> list)
     • Component
        – resources/utils/list.xhtml
           • Declares value and styleClass inside composite:interface
           • Generates list using ui:repeat (composite:implementation)
        – composite-components.xhtml
           • Declares utils: namespace
           • Uses <utils:list value="#{someBean.someCollection}"
                              styleClass="some-css-name"/>
33




      Component File
      (resources/utils/list.xhtml)
      (resources/utils/list xhtml)
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …>
                     p //         g/    /
     <html xmlns="http://www.w3.org/1999/xhtml"
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:composite="http://java.sun.com/jsf/composite"
           xmlns:ui="http://java.sun.com/jsf/facelets">
     …
     <composite:interface>
       <composite:attribute name="value" required="true"/>
       <composite:attribute name="styleClass" default="italic"/>
                            name styleClass default italic />
     </composite:interface>

     <composite:implementation>
       <ul class="#{cc.attrs.styleClass}">
       <ui:repeat var="listItem" value="#{cc.attrs.value}">
         <li>#{listItem}</li>
       </ui:repeat>
       </ul>
     </composite:implementation>
34   </body></html>
Main File
      (composite-components.xhtml)
      (composite-components xhtml)
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …>
     <html xmlns="http://www w3 org/1999/xhtml"
           xmlns= http://www.w3.org/1999/xhtml
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:utils="http://java.sun.com/jsf/composite/utils">
     …
     <fieldset>
     <legend>Composite Components that Use ui:repeat</legend>
                                      p
     <h2>First coreservlets.com Developer</h2>
     <ul>
       <li>Level: #{person1.level}</li>
       <li>First name: #{person1.firstName}</li>
       <li>Last name: #{person1.lastName}</li>
       <li>Languages: <utils:list value="#{person1.languages}"
                                  styleClass="blue"/></li>
     </ul>


35




      Main File: Continued
      (composite-components.xhtml)
      (composite-components xhtml)
     <hr/>
     <h2>Second coreservlets com Developer</h2>
                coreservlets.com
     <p>
     Our second developer (#{person2.level}-level) is
     #{person2.firstName} #{person2.lastName}.
     He is proficient in:</p>
     <utils:list value="#{person2.languages}"/>
     </fieldset>
     <p/>
     <fieldset>




36
Base Class for Beans
     public class Programmer {
       private String firstName lastName level;
                      firstName, lastName,
       private String[] languages;

       public Programmer(String firstName
                                firstName,
                         String lastName,
                         String level,
                         String... languages) {
         this.firstName = firstName;
         this.lastName = lastName;
         t s. e e
         this.level = level;
                       e e ;
         this.languages = languages;
       }

       // Getter methods getFirstName, getLastName, etc.
       // Note that getLanguages returns an array
37




      Bean: Person1
     @ManagedBean
     public class Person1 extends Programmer {
       public Person1() {
         super("Larry", "Ellison", "Junior",
               "SQL", "Prolog", "OCL", "Datalog");
                SQL    Prolog    OCL    Datalog );
       }
     }




38
Bean: Person2
     @ManagedBean
     public class Person2 extends Programmer {
       public Person2() {
         super("Larry", "Page", "Junior",
               "Java", "C++", "Python", "Go");
                Java    C++    Python     Go );
       }
     }




39




      Results




40
© 2011 Marty Hall




             Nested Components

                       Customized Java EE Training: http://courses.coreservlets.com/
          Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
          Developed and taught by well-known author and developer. At public venues or onsite at your location.




     Idea
     • Two component files
       – First one refers to second one
     • Design options
       – B h components could be used independently
         Both              ld b     di d     d l
          • So, provide explicit attributes for both components
          • First component passes attributes to second component
       – Second (nested) component specific to first (outer)
          • Provide explicit attributes for first component
          • Second component directly reads attributes of first
            component by using #{cc.parent.attrs.attributeName}
     • Compatibility note
       – Due to bug in earlier versions, #{cc.parent.attrs…} works
         only in Mojarra 2.0.3 or later.
42
First Component File: Summary
     • In subfolder of “resources” folder
       – E.g., resources/someDir/someFile.xhtml
     • Basic structure
       – Looks like xhtml file as before
          • But also declares namespace for second component
       – Contains composite:interface
          • Defines attributes used as before
             <composite:attribute name="attributeName"/>
       – Contains composite:implementation
          • Creates final output. Refers to attributes with cc.attrs.
          • Also refers to second component.
             …#{cc.attrs.attributeName}…
             <someNamespace:someComponent/>
43




     Second Component File:
     Summary
     • In subfolder of “resources” folder
       – E.g., resources/someDir/someFile.xhtml
     • Basic structure
       – Looks like xhtml file as before
       – Contains composite:interface
          • Does not repeat attributes defined by first component
             – But could still define some new attributes
       – Contains composite:implementation
          • Creates final output.
          • Refers to locally-defined attributes with cc.attrs.
          • Refers to attributes of surrounding component with
            cc.parent.attrs
             …#{cc.parent.attrs.attributeName}…
44
Example
     • Make a bulleted list describing programmer
        – First name, last name, etc.
     • Use nested component for languages
        – T build another <ul> list
          To b ild    h     l li
     • Components
        – resources/utils/programmerInfo xhtml
          resources/utils/programmerInfo.xhtml
           •   Declares utils: namespace
           •   Declares programmer inside composite:interface
           •   Refers to #{cc.attrs.programmer.property}
           •   Calls <utils:langList/>
        – resources/utils/langList.xhtml
                             g
           • Does not declare any attributes
           • Refers to #{cc.parent.attrs.programmer.property}
45




      Component File 1
      (resources/utils/programmerInfo.xhtml)
      (resources/utils/programmerInfo xhtml)
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …>
                     p //         g/    /
     <html xmlns="http://www.w3.org/1999/xhtml"
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:composite="http://java.sun.com/jsf/composite"
           xmlns:utils="http://java.sun.com/jsf/composite/utils">
     …
     <composite:interface>
       <composite:attribute name="programmer" required="true"/>
       <composite:attribute name="styleClass" default="italic"/>
                            name styleClass default italic />
     </composite:interface>
     <composite:implementation>
       <ul class="#{cc.attrs.styleClass}">
         <li>Level: #{cc.attrs.programmer.level}</li>
         <li>First name: #{cc.attrs.programmer.firstName}</li>
         <li>Last name: #{cc.attrs.programmer.lastName}</li>
         <li><utils:langList/></li>
       </ul>
     </composite:implementation>
46   </body></html>
Component File 2
      (resources/utils/langList.xhtml)
      (resources/utils/langList xhtml)
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …>
     <html xmlns="http://www.w3.org/1999/xhtml"
       t       s   ttp://   . 3.o g/ 999/ t
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:composite="http://java.sun.com/jsf/composite"
           xmlns:ui="http://java.sun.com/jsf/facelets">
     …
     <composite:interface/>

     <composite:implementation>
       #{cc.parent.attrs.programmer.firstName}'s languages:
       <ul>
       <ui:repeat var="language"
                  value="#{cc.parent.attrs.programmer.languages}">
         <li>#{language}</li>
       </ui:repeat>
       </ul>
       </ l>
     </composite:implementation>
     </body></html>                        Reminder: cc.parent fails in Mojarra 2.0.2 and earlier.
47




      Main File
      (composite-components.xhtml)
      (composite-components xhtml)
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …>
     <html xmlns="http://www w3 org/1999/xhtml"
           xmlns= http://www.w3.org/1999/xhtml
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:utils="http://java.sun.com/jsf/composite/utils">
     <h:head>
     …
     </h:head>
           y
     <h:body>
     …
     <fieldset>
     <legend>Nested Composite Components</legend>
     <utils:programmerInfo programmer="#{person1}"
                           styleClass="blue"/>
     </fieldset>
     …
     </h:body></html>
48
Results




49




                                                                                             © 2011 Marty Hall




      Handling Relative URLs
                p
         in Components

                    Customized Java EE Training: http://courses.coreservlets.com/
       Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
       Developed and taught by well-known author and developer. At public venues or onsite at your location.
Problems with Simple Relative
     URLs
     • Issue
       – S
         Suppose component used simple relative URLs
                          t   d i l l ti URL
       – Suppose main page was moved to sub directory
     • Example
       – Hypertext links
          • <a href="welcome.jsf"> would now refer to
            http://host/context-root/subdir/welcome.jsf
       – I
         Images
          • <img src="./images/pic.jpg"> would now refer to
            http://host/context-root/subdir/images/pic.jpg
       – Style sheets
          • <link …href="./css/styles.css"/> would now refer to
            http://host/context-root/subdir/css/styles.css
       – JavaScript files
                 p
          • <script src="./scripts/my-script.js"…> would now refer to
            http://host/context-root/subdir/scripts/my-script.js
51




     Solutions
     (Relocatable Resources)
     • Hypertext links
       – Use <a href="#{request.contextPath}/blah.jsf">
          • Or <h:outputLink value="#{request.contextPath}/blah.jsf"/>
     • Images
       – Put in images folder under “resources” and use
         <h:graphicImage name="blah.jpg" library="images"/>
            g p         g              jpg       y      g
          • Can also use <h:graphicImage url="/images/blah.jpg"/>
     • Style sheets
       – P i css f ld under “resources” and use
         Put in   folder d “            ” d
         <h:outputStylesheet name="blah.css" library="css"/>
     • JavaScript files
       – Put in scripts folder under “resources” and use
52
         <h:outputScript name="blah.js" library="scripts"/>
Component File
      (resources/utils/info3.xhtml)
      (resources/utils/info3 xhtml)
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …>
     <html … xmlns:composite="http://java.sun.com/jsf/composite"
                      p           p   j                        j             p
           xmlns:utils="http://java.sun.com/jsf/composite/utils">
     …
     <composite:interface>
       <composite:attribute name="styleClass"/>
                             name= styleClass />
     </composite:interface>
     <composite:implementation>
       <table align="right">
                                           Actual file is resources/images/duke-guitar.png
                                                          resources/images/duke guitar.png
         <tr><th>
           <h:graphicImage name="duke-guitar.png" library="images"/>
         </th></tr>
         <tr><th>
           <a href="#{request.contextPath}/composite-components.jsf">
           Return to main page</a>
         </th></tr>
       </table>
       <utils:info2 styleClass="#{cc.attrs.styleClass}"/>
     </composite:implementation>
53
     </body></html>




      Main File 1
      (relative-urls.xhtml)
      (relative-urls xhtml)
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …>
     <html xmlns="http://www w3 org/1999/xhtml"
           xmlns= http://www.w3.org/1999/xhtml
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:utils="http://java.sun.com/jsf/composite/utils">
     <h:head><title>Composite Components</title>
     <h:outputStylesheet name="styles.css" library="css"/>
     </h:head>
           y
     <h:body>
     …
     <fieldset>
     <legend>Component that Uses Relative URLs</legend>
     <utils:info3/>
     </fieldset>
     …
     </h:body></html>


54
Main File 2
      (someDirectory/relative-urls.xhtml)
      (someDirectory/relative-urls xhtml)
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …>
     <html xmlns="http://www w3 org/1999/xhtml"
           xmlns= http://www.w3.org/1999/xhtml
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:utils="http://java.sun.com/jsf/composite/utils">
     <h:head><title>Composite Components</title>
     <h:outputStylesheet name="styles.css" library="css"/>
     </h:head>
           y
     <h:body>
     …
     <fieldset>
     <legend>Component that Uses Relative URLs</legend>
     <utils:info3/>
     </fieldset>
     …                             This is an exact copy of the file from the top-level directory.
     </h:body></html>


55




      Results




56
© 2011 Marty Hall




                                       Wrap-Up

                       Customized Java EE Training: http://courses.coreservlets.com/
          Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
          Developed and taught by well-known author and developer. At public venues or onsite at your location.




     Summary
     • Define a component
       – Put foo.xhtml in “resources/bar” folder
       – Define available attributes inside composite:interface
          • Use <composite:attribute name="attributeName" … />
                                     name= attributeName
       – Build output inside composite:implementation
          • Use #{cc.attrs.attributeName}
          • Possibly use ui:repeat. Be careful with relative URLs.
     • Use component in main page
       – Define namespace
          • <html …
               xmlns:bar="http://java.sun.com/jsf/composite/bar">
       – Use the component i facelets page
              h            in f l
          • <bar:foo attributeName="…" />
58
© 2011 Marty Hall




                       Questions?

             Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.

More Related Content

What's hot

Training in Android with Maven
Training in Android with MavenTraining in Android with Maven
Training in Android with Maven
Arcadian Learning
 
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
Arun Gupta
 
Maven II
Maven IIMaven II
Building and Managing Projects with Maven
Building and Managing Projects with MavenBuilding and Managing Projects with Maven
Building and Managing Projects with Maven
Khan625
 
Architecture of Drupal - Drupal Camp
Architecture of Drupal - Drupal CampArchitecture of Drupal - Drupal Camp
Architecture of Drupal - Drupal Camp
Dipen Chaudhary
 
Migrate yourself. code -> module -> mind
Migrate yourself. code -> module -> mindMigrate yourself. code -> module -> mind
Migrate yourself. code -> module -> mind
Valentine Matsveiko
 
Building and managing java projects with maven part-III
Building and managing java projects with maven part-IIIBuilding and managing java projects with maven part-III
Building and managing java projects with maven part-III
princeirfancivil
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHP
Oscar Merida
 
Mule esb
Mule esbMule esb
Mule esb
Khan625
 
Maven in Mule
Maven in MuleMaven in Mule
Maven in Mule
Anand kalla
 
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Michael Pirnat
 
Jumpstart Django
Jumpstart DjangoJumpstart Django
Jumpstart Django
ryates
 
14 mvc
14 mvc14 mvc
14 mvc
snopteck
 
Introduction to YUI PHP Loader
Introduction to YUI PHP LoaderIntroduction to YUI PHP Loader
Introduction to YUI PHP Loader
Chad Auld
 
Intro To Sap Netweaver Java
Intro To Sap Netweaver JavaIntro To Sap Netweaver Java
Intro To Sap Netweaver Java
Leland Bartlett
 
JavaServer Faces 2.0 - JavaOne India 2011
JavaServer Faces 2.0 - JavaOne India 2011JavaServer Faces 2.0 - JavaOne India 2011
JavaServer Faces 2.0 - JavaOne India 2011
Arun Gupta
 

What's hot (16)

Training in Android with Maven
Training in Android with MavenTraining in Android with Maven
Training in Android with Maven
 
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
 
Maven II
Maven IIMaven II
Maven II
 
Building and Managing Projects with Maven
Building and Managing Projects with MavenBuilding and Managing Projects with Maven
Building and Managing Projects with Maven
 
Architecture of Drupal - Drupal Camp
Architecture of Drupal - Drupal CampArchitecture of Drupal - Drupal Camp
Architecture of Drupal - Drupal Camp
 
Migrate yourself. code -> module -> mind
Migrate yourself. code -> module -> mindMigrate yourself. code -> module -> mind
Migrate yourself. code -> module -> mind
 
Building and managing java projects with maven part-III
Building and managing java projects with maven part-IIIBuilding and managing java projects with maven part-III
Building and managing java projects with maven part-III
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHP
 
Mule esb
Mule esbMule esb
Mule esb
 
Maven in Mule
Maven in MuleMaven in Mule
Maven in Mule
 
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
 
Jumpstart Django
Jumpstart DjangoJumpstart Django
Jumpstart Django
 
14 mvc
14 mvc14 mvc
14 mvc
 
Introduction to YUI PHP Loader
Introduction to YUI PHP LoaderIntroduction to YUI PHP Loader
Introduction to YUI PHP Loader
 
Intro To Sap Netweaver Java
Intro To Sap Netweaver JavaIntro To Sap Netweaver Java
Intro To Sap Netweaver Java
 
JavaServer Faces 2.0 - JavaOne India 2011
JavaServer Faces 2.0 - JavaOne India 2011JavaServer Faces 2.0 - JavaOne India 2011
JavaServer Faces 2.0 - JavaOne India 2011
 

Similar to Jsf2 composite-components

JavaScript-Core
JavaScript-CoreJavaScript-Core
JavaScript-Core
tutorialsruby
 
JavaScript-Core
JavaScript-CoreJavaScript-Core
JavaScript-Core
tutorialsruby
 
jQuery-3-UI
jQuery-3-UIjQuery-3-UI
jQuery-3-UI
guestcf600a
 
JQuery-Tutorial
 JQuery-Tutorial JQuery-Tutorial
JQuery-Tutorial
tutorialsruby
 
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-3-UI.pptx
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-3-UI.pptxMicrosoft PowerPoint - &lt;b>jQuery&lt;/b>-3-UI.pptx
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-3-UI.pptx
tutorialsruby
 
JSF 2.0 Preview
JSF 2.0 PreviewJSF 2.0 Preview
JSF 2.0 Preview
Skills Matter
 
01 spring-intro
01 spring-intro01 spring-intro
01 spring-intro
hossein helali
 
Andy Bosch - JavaServer Faces in the cloud
Andy Bosch -  JavaServer Faces in the cloudAndy Bosch -  JavaServer Faces in the cloud
Andy Bosch - JavaServer Faces in the cloud
Andy Bosch
 
01 web-apps
01 web-apps01 web-apps
01 web-apps
snopteck
 
01 web-apps
01 web-apps01 web-apps
01 web-apps
Aravindharamanan S
 
02 servlet-basics
02 servlet-basics02 servlet-basics
02 servlet-basics
snopteck
 
JSF2 Composite Components - Ian Hlavats
JSF2 Composite Components - Ian HlavatsJSF2 Composite Components - Ian Hlavats
JSF2 Composite Components - Ian Hlavats
jaxconf
 
Ajax basics
Ajax basicsAjax basics
Ajax basics
amanrahulraj
 
Lightweight web frameworks
Lightweight web frameworksLightweight web frameworks
Lightweight web frameworks
Jonathan Holloway
 
Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268
Ramamohan Chokkam
 
Maven introduction in Mule
Maven introduction in MuleMaven introduction in Mule
Maven introduction in Mule
Shahid Shaik
 
Maven
MavenMaven
Deploy Flex with Apache Ant
Deploy Flex with Apache AntDeploy Flex with Apache Ant
Deploy Flex with Apache Ant
dctrl — studio for creativ technology
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
kamal kotecha
 
Build Automation of PHP Applications
Build Automation of PHP ApplicationsBuild Automation of PHP Applications
Build Automation of PHP Applications
Pavan Kumar N
 

Similar to Jsf2 composite-components (20)

JavaScript-Core
JavaScript-CoreJavaScript-Core
JavaScript-Core
 
JavaScript-Core
JavaScript-CoreJavaScript-Core
JavaScript-Core
 
jQuery-3-UI
jQuery-3-UIjQuery-3-UI
jQuery-3-UI
 
JQuery-Tutorial
 JQuery-Tutorial JQuery-Tutorial
JQuery-Tutorial
 
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-3-UI.pptx
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-3-UI.pptxMicrosoft PowerPoint - &lt;b>jQuery&lt;/b>-3-UI.pptx
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-3-UI.pptx
 
JSF 2.0 Preview
JSF 2.0 PreviewJSF 2.0 Preview
JSF 2.0 Preview
 
01 spring-intro
01 spring-intro01 spring-intro
01 spring-intro
 
Andy Bosch - JavaServer Faces in the cloud
Andy Bosch -  JavaServer Faces in the cloudAndy Bosch -  JavaServer Faces in the cloud
Andy Bosch - JavaServer Faces in the cloud
 
01 web-apps
01 web-apps01 web-apps
01 web-apps
 
01 web-apps
01 web-apps01 web-apps
01 web-apps
 
02 servlet-basics
02 servlet-basics02 servlet-basics
02 servlet-basics
 
JSF2 Composite Components - Ian Hlavats
JSF2 Composite Components - Ian HlavatsJSF2 Composite Components - Ian Hlavats
JSF2 Composite Components - Ian Hlavats
 
Ajax basics
Ajax basicsAjax basics
Ajax basics
 
Lightweight web frameworks
Lightweight web frameworksLightweight web frameworks
Lightweight web frameworks
 
Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268
 
Maven introduction in Mule
Maven introduction in MuleMaven introduction in Mule
Maven introduction in Mule
 
Maven
MavenMaven
Maven
 
Deploy Flex with Apache Ant
Deploy Flex with Apache AntDeploy Flex with Apache Ant
Deploy Flex with Apache Ant
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
 
Build Automation of PHP Applications
Build Automation of PHP ApplicationsBuild Automation of PHP Applications
Build Automation of PHP Applications
 

Recently uploaded

clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 

Recently uploaded (20)

clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 

Jsf2 composite-components

  • 1. © 2011 Marty Hall JSF 2 0: Building 2.0: Composite Components Originals of Slides and Source Code for Examples: http://www.coreservlets.com/JSF Tutorial/jsf2/ http://www.coreservlets.com/JSF-Tutorial/jsf2/ Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. © 2011 Marty Hall For live training on JSF 2.x, please see courses at htt // t http://courses.coreservlets.com/. l t / Taught by the author of Core Servlets and JSP, More Servlets and JSP and this tutorial. Available at public JSP, tutorial venues, or customized versions can be held on-site at your organization. •C Courses d developed and t l d d taught b M t H ll ht by Marty Hall – JSF, servlets/JSP, Ajax, jQuery, Android development, Java 6 programming, custom mix of topics – Ajax courses can concentrate on 1EE Training: http://courses.coreservlets.com/several Customized Java library (jQuery, Prototype/Scriptaculous, Ext-JS, Dojo, etc.) or survey • Courses developed and taught by coreservlets.com experts (edited by Marty) Servlets, JSP, Hibernate/JPA, EJB3, GWT, jQuery, GWT, RESTful Web Services RESTful Web Services, Android. – Spring, JSF 2.0, Java 6, Ajax, SOAP-based and Spring, Hibernate, Developed and taught by well-known author and developer. At public venues or onsite at your location. Contact hall@coreservlets.com for details
  • 2. Topics in This Section • Idea • Basic components • Passing values to components • Using ui:repeat inside components • Nested components • Handling relative URLs in components 5 © 2011 Marty Hall Overview Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
  • 3. Idea • JSF 2.0 added “composite components” – Reusable, named chunks of facelets code – Can pass arguments • Similar to JSP tag files – But have use of JSF tags and JSF expression language – Real components: can have listeners events facets etc. listeners, events, facets, etc • Abbreviated example – resources/utils/myComponent.xhtml (snippet) y p ( pp ) <composite:implementation> This is a test </composite:implementation> – some-page.xhtml (snippet) 7 <utils:myComponent/> Making and Using Custom Components: Overview • Define a component – Put foo.xhtml in “resources/bar” folder – Define available attributes inside composite:interface – Build output inside composite:implementation – Use composite: namespace in component file • <html … xmlns:composite="http://java.sun.com/jsf/composite"> • Use component namespace in facelets page – <html … xmlns:bar="http://java.sun.com/jsf/composite/bar"> • Use the component in facelets page p p g – <bar:foo ... /> 8
  • 4. © 2011 Marty Hall Basic Components Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. Super-Quick Example p p • Component File • Main File resources/utils/myComponent.xhtml some-page.xhtml <!DOCTYPE …> <!DOCTYPE …> <html … <html … xmlns:composite= xmlns:utils= l til http://java.sun.com/jsf/composite"> "http://java.sun.com/jsf/composite/utils"> <head><title>…</title></head> <h:head><title>Composite Components</title> <body> y … <h:body>… <composite:interface/> <utils:myComponent/>… </h:body></html> <composite:implementation> This is a test </composite:implementation> </body></html> 10
  • 5. Component File: Summary • In subfolder of “resources” folder – E.g., resources/someDir/someFile.xhtml • We already used “resources” in section on relocatable resources in tutorial on page templating • Basic structure – Looks like xhtml file • head and body tags are simply to satisfy editor • Declares composite: namespace in <html…> start tag – <html … xmlns:composite="http://java.sun.com/jsf/composite"> – Contains composite:interface • Defines attributes used (if any) – Contains composite:implementation • Creates final output • Only content of composite:implementation becomes output 11 Component File: Outline <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …> <html xmlns="http://www w3 org/1999/xhtml" xmlns http://www.w3.org/1999/xhtml xmlns:h="http://java.sun.com/jsf/html" xmlns:composite="http://java.sun.com/jsf/composite"> <head><title>(For validation only)</title></head> ( y) <body> <composite:interface> <!-- Declare attributes here --> </composite:interface> <composite:implementation> < it i l t ti > <!-- Create output here --> </composite:implementation> </body></html> 12
  • 6. Main File: Summary • Regular facelets file (someFile.xhtml) – No special folder name. – Accessed with someFile.jsf (or someFile.faces, etc.) in normal manner. manner • Basic structure – Normal facelets structure • h:head, h:body, and other normal JSF constructs – Declares component namespace in <html…> start tag xmlns:someDir="http://java.sun.com/jsf/composite/someDir" l Di "htt //j /j f/ it / Di " – Technically, there is no requirement that the namespace (the part after xmlns:) match the folder name. But, it is traditional to do so. – Use component • <someDir:someFile/> (if no attributes) 13 • <someDir:someFile att1="…" … /> (if it uses attributes) Main File: Outline <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …> <html xmlns http://www.w3.org/1999/xhtml xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:someDir="http://java.sun.com/jsf/composite/someDir"> <h:head> … </h:head> <h:body> … <someDir:someFile/> … </h:body></html> 14
  • 7. Example • Make a bulleted list of request info – Requesting host – User agent – Current Locale – Session ID • May want to reuse list, so make component – resources/utils/info1.xhtml • Generates list inside composite:implementation – composite-components.xhtml • Declares utils: namespace • Uses <utils:info1/> ut s o / 15 Component File (resources/utils/info1.xhtml) (resources/utils/info1 xhtml) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …> <html xmlns="http://www.w3.org/1999/xhtml" p // g/ / xmlns:h="http://java.sun.com/jsf/html" xmlns:composite="http://java.sun.com/jsf/composite"> <head><title>(For validation only)</title></head> <body> <composite:interface/> <!-- No attributes --> <composite:implementation> <ul> <li>Requesting host: #{request.remoteHost}</li> <li>Requesting client type:<br/> #{header["User-Agent"]}</li> <li>Locale: #{view.locale}</li> <li>Session id: #{session.id}</li> </ul> / </composite:implementation> 16 </body></html>
  • 8. Main File (composite-components.xhtml) (composite-components xhtml) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …> <html xmlns="http://www w3 org/1999/xhtml" xmlns= http://www.w3.org/1999/xhtml xmlns:h="http://java.sun.com/jsf/html" xmlns:utils="http://java.sun.com/jsf/composite/utils"> <h:head> … </h:head> y <h:body> … <fieldset> <legend>Basic Composite Components</legend> <utils:info1/> </fieldset> … </h:body></html> 17 Results 18
  • 9. © 2011 Marty Hall Components with Attributes Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. Component File: Summary • In subfolder of “resources” folder – E.g., resources/someDir/someFile.xhtml • Basic structure – Looks like xhtml file as before • Declares composite: namespace as before – Contains composite:interface • D fi Defines attributes used tt ib t d <composite:interface> <composite:attribute name="attributeName"/> </composite:interface> / i i f – Contains composite:implementation • Creates final output. Refers to attributes with cc.attrs <composite:implementation> …#{cc.attrs.attributeName}… 20 </composite:implementation>
  • 10. More on cc.attrs • “cc” is predefined variable – Stands for “composite component” – Refers to top-level component in component file, of type UINamingContainer • Main property is “attrs” – This is a specially defined attribute that contains a Map of p y p the attributes used to call the component • Main page: <someDir:someFile message="test"/> • Component: <h2>Message: #{cc.attrs.message}</h2> #{cc attrs message}</h2> • Other useful attributes – parent, children, clientId p , , 21 Component File: Outline <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …> <html xmlns="http://www w3 org/1999/xhtml" xmlns= http://www.w3.org/1999/xhtml xmlns:h="http://java.sun.com/jsf/html" xmlns:composite="http://java.sun.com/jsf/composite"> <head><title>(For validation only)</title></head> <body> <composite:interface> <composite:attribute name="attributeName"/> <composite:interface> <composite:implementation> Blah #{cc.attrs.attributeName} Blah </composite:implementation> p p </body></html> 22
  • 11. More on composite:attribute • Basic usage <composite:interface> <composite:attribute name="attributeName"/> </composite:interface> • Basic attributes – name • Attribute name as used in component in main page – required • Is attribute required? (default: false) – Warning! Mojarra 2.0.2 does not enforce this! Fixed in 2.0.3. –d f l default • For non-required attributes, value to use if none supplied • Advanced attributes – type, method-signature, targets • For attributes that expect methods or complex values 23 Mini Example Reminder: required is not properly • composite:interface enforced in Mojarra 2.0.2 and earlier. <composite:attribute name="msg1" required="true"/> <composite:attribute name="msg2" default="Hi"/> • composite:implementation it i l t ti <h2>Message 1: "#{cc.attrs.msg1}"</h2> <h2>Message 2: "#{cc.attrs.msg2}"</h2> h2 Message #{cc.attrs.msg2} /h2 • Main page <utils:showMessages msg1="Hello, world"/> • Result Message 1: "Hello, world" Message 2: "Hi" 24
  • 12. Example • Make a bulleted list of request info as before – Requesting host, user agent, current Locale, session ID • Support styleClass attribute – T specify CSS style of resultant <ul> li To if l f l l list • Component – resources/utils/info2 xhtml resources/utils/info2.xhtml • Declares styleClass inside composite:interface • Generates list inside composite:implementation – composite-components.xhtml • Declares utils: namespace • Uses <utils:info2 styleClass="some-css-name"/> ut s o sty eC ass so e css a e / 25 Component File (resources/utils/info2.xhtml) (resources/utils/info2 xhtml) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …> p // g/ / <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:composite="http://java.sun.com/jsf/composite"> … <composite:interface> <composite:attribute name="styleClass"/> </composite:interface> <composite:implementation> <ul class="#{cc.attrs.styleClass}"> <li>Requesting host: #{request.remoteHost}</li> <li>Requesting client type: <br/> #{header["User-Agent"]}</li> <li>Locale: #{view.locale}</li> <li>Session id: #{session id}</li> #{session.id}</li> </ul> </composite:implementation> 26 </body></html>
  • 13. Main File (composite-components.xhtml) (composite-components xhtml) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …> <html xmlns="http://www w3 org/1999/xhtml" xmlns= http://www.w3.org/1999/xhtml xmlns:h="http://java.sun.com/jsf/html" xmlns:utils="http://java.sun.com/jsf/composite/utils"> <h:head> … </h:head> y <h:body> … <fieldset> <legend>Composite Components with Attributes</legend> <utils:info2 styleClass="blue"/> </fieldset> … </h:body></html> 27 CSS File .italic { font-style: italic; } .blue { font weight: bold; color: blue; } blue font-weight: … 28
  • 14. Results 29 © 2011 Marty Hall Using ui:repeat with Composite Components p p Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
  • 15. Component File: Summary • In subfolder of “resources” folder – E.g., resources/someDir/someFile.xhtml • Basic structure – Looks like xhtml file as before • Declare composite: and ui: namespaces – <html … xmlns:composite http://java.sun.com/jsf/composite html xmlns:composite="http://java.sun.com/jsf/composite“ xmlns:ui="http://java.sun.com/jsf/facelets"> – Contains composite:interface • Defines attributes as before – Contains composite:implementation • Creates final output – Uses #{cc.attrs.attributeName} as before – Uses ui:repeat to build output 31 More on ui:repeat • Sample usage <ul> <ui:repeat var="color" value="#{item.availableColors}"> <li>#{color}</li> </ui:repeat> /u epea </ul> • More attributes <ui:repeat var="someVar" ui:repeat var someVar value="#{someBean.someCollection}" varStatus="statusVariable" offset="…" size="…" step="…"> … </ui:repeat> • More details on ui:repeat 32 – See tutorial section on looping
  • 16. Example • Pass in array or List – Turn it into <ul> list showing individual entries • Attributes – value (EL expression to designate collection) l i d i ll i ) – styleClass (to specify CSS style of resultant <ul> list) • Component – resources/utils/list.xhtml • Declares value and styleClass inside composite:interface • Generates list using ui:repeat (composite:implementation) – composite-components.xhtml • Declares utils: namespace • Uses <utils:list value="#{someBean.someCollection}" styleClass="some-css-name"/> 33 Component File (resources/utils/list.xhtml) (resources/utils/list xhtml) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …> p // g/ / <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:composite="http://java.sun.com/jsf/composite" xmlns:ui="http://java.sun.com/jsf/facelets"> … <composite:interface> <composite:attribute name="value" required="true"/> <composite:attribute name="styleClass" default="italic"/> name styleClass default italic /> </composite:interface> <composite:implementation> <ul class="#{cc.attrs.styleClass}"> <ui:repeat var="listItem" value="#{cc.attrs.value}"> <li>#{listItem}</li> </ui:repeat> </ul> </composite:implementation> 34 </body></html>
  • 17. Main File (composite-components.xhtml) (composite-components xhtml) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …> <html xmlns="http://www w3 org/1999/xhtml" xmlns= http://www.w3.org/1999/xhtml xmlns:h="http://java.sun.com/jsf/html" xmlns:utils="http://java.sun.com/jsf/composite/utils"> … <fieldset> <legend>Composite Components that Use ui:repeat</legend> p <h2>First coreservlets.com Developer</h2> <ul> <li>Level: #{person1.level}</li> <li>First name: #{person1.firstName}</li> <li>Last name: #{person1.lastName}</li> <li>Languages: <utils:list value="#{person1.languages}" styleClass="blue"/></li> </ul> 35 Main File: Continued (composite-components.xhtml) (composite-components xhtml) <hr/> <h2>Second coreservlets com Developer</h2> coreservlets.com <p> Our second developer (#{person2.level}-level) is #{person2.firstName} #{person2.lastName}. He is proficient in:</p> <utils:list value="#{person2.languages}"/> </fieldset> <p/> <fieldset> 36
  • 18. Base Class for Beans public class Programmer { private String firstName lastName level; firstName, lastName, private String[] languages; public Programmer(String firstName firstName, String lastName, String level, String... languages) { this.firstName = firstName; this.lastName = lastName; t s. e e this.level = level; e e ; this.languages = languages; } // Getter methods getFirstName, getLastName, etc. // Note that getLanguages returns an array 37 Bean: Person1 @ManagedBean public class Person1 extends Programmer { public Person1() { super("Larry", "Ellison", "Junior", "SQL", "Prolog", "OCL", "Datalog"); SQL Prolog OCL Datalog ); } } 38
  • 19. Bean: Person2 @ManagedBean public class Person2 extends Programmer { public Person2() { super("Larry", "Page", "Junior", "Java", "C++", "Python", "Go"); Java C++ Python Go ); } } 39 Results 40
  • 20. © 2011 Marty Hall Nested Components Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. Idea • Two component files – First one refers to second one • Design options – B h components could be used independently Both ld b di d d l • So, provide explicit attributes for both components • First component passes attributes to second component – Second (nested) component specific to first (outer) • Provide explicit attributes for first component • Second component directly reads attributes of first component by using #{cc.parent.attrs.attributeName} • Compatibility note – Due to bug in earlier versions, #{cc.parent.attrs…} works only in Mojarra 2.0.3 or later. 42
  • 21. First Component File: Summary • In subfolder of “resources” folder – E.g., resources/someDir/someFile.xhtml • Basic structure – Looks like xhtml file as before • But also declares namespace for second component – Contains composite:interface • Defines attributes used as before <composite:attribute name="attributeName"/> – Contains composite:implementation • Creates final output. Refers to attributes with cc.attrs. • Also refers to second component. …#{cc.attrs.attributeName}… <someNamespace:someComponent/> 43 Second Component File: Summary • In subfolder of “resources” folder – E.g., resources/someDir/someFile.xhtml • Basic structure – Looks like xhtml file as before – Contains composite:interface • Does not repeat attributes defined by first component – But could still define some new attributes – Contains composite:implementation • Creates final output. • Refers to locally-defined attributes with cc.attrs. • Refers to attributes of surrounding component with cc.parent.attrs …#{cc.parent.attrs.attributeName}… 44
  • 22. Example • Make a bulleted list describing programmer – First name, last name, etc. • Use nested component for languages – T build another <ul> list To b ild h l li • Components – resources/utils/programmerInfo xhtml resources/utils/programmerInfo.xhtml • Declares utils: namespace • Declares programmer inside composite:interface • Refers to #{cc.attrs.programmer.property} • Calls <utils:langList/> – resources/utils/langList.xhtml g • Does not declare any attributes • Refers to #{cc.parent.attrs.programmer.property} 45 Component File 1 (resources/utils/programmerInfo.xhtml) (resources/utils/programmerInfo xhtml) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …> p // g/ / <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:composite="http://java.sun.com/jsf/composite" xmlns:utils="http://java.sun.com/jsf/composite/utils"> … <composite:interface> <composite:attribute name="programmer" required="true"/> <composite:attribute name="styleClass" default="italic"/> name styleClass default italic /> </composite:interface> <composite:implementation> <ul class="#{cc.attrs.styleClass}"> <li>Level: #{cc.attrs.programmer.level}</li> <li>First name: #{cc.attrs.programmer.firstName}</li> <li>Last name: #{cc.attrs.programmer.lastName}</li> <li><utils:langList/></li> </ul> </composite:implementation> 46 </body></html>
  • 23. Component File 2 (resources/utils/langList.xhtml) (resources/utils/langList xhtml) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …> <html xmlns="http://www.w3.org/1999/xhtml" t s ttp:// . 3.o g/ 999/ t xmlns:h="http://java.sun.com/jsf/html" xmlns:composite="http://java.sun.com/jsf/composite" xmlns:ui="http://java.sun.com/jsf/facelets"> … <composite:interface/> <composite:implementation> #{cc.parent.attrs.programmer.firstName}'s languages: <ul> <ui:repeat var="language" value="#{cc.parent.attrs.programmer.languages}"> <li>#{language}</li> </ui:repeat> </ul> </ l> </composite:implementation> </body></html> Reminder: cc.parent fails in Mojarra 2.0.2 and earlier. 47 Main File (composite-components.xhtml) (composite-components xhtml) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …> <html xmlns="http://www w3 org/1999/xhtml" xmlns= http://www.w3.org/1999/xhtml xmlns:h="http://java.sun.com/jsf/html" xmlns:utils="http://java.sun.com/jsf/composite/utils"> <h:head> … </h:head> y <h:body> … <fieldset> <legend>Nested Composite Components</legend> <utils:programmerInfo programmer="#{person1}" styleClass="blue"/> </fieldset> … </h:body></html> 48
  • 24. Results 49 © 2011 Marty Hall Handling Relative URLs p in Components Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
  • 25. Problems with Simple Relative URLs • Issue – S Suppose component used simple relative URLs t d i l l ti URL – Suppose main page was moved to sub directory • Example – Hypertext links • <a href="welcome.jsf"> would now refer to http://host/context-root/subdir/welcome.jsf – I Images • <img src="./images/pic.jpg"> would now refer to http://host/context-root/subdir/images/pic.jpg – Style sheets • <link …href="./css/styles.css"/> would now refer to http://host/context-root/subdir/css/styles.css – JavaScript files p • <script src="./scripts/my-script.js"…> would now refer to http://host/context-root/subdir/scripts/my-script.js 51 Solutions (Relocatable Resources) • Hypertext links – Use <a href="#{request.contextPath}/blah.jsf"> • Or <h:outputLink value="#{request.contextPath}/blah.jsf"/> • Images – Put in images folder under “resources” and use <h:graphicImage name="blah.jpg" library="images"/> g p g jpg y g • Can also use <h:graphicImage url="/images/blah.jpg"/> • Style sheets – P i css f ld under “resources” and use Put in folder d “ ” d <h:outputStylesheet name="blah.css" library="css"/> • JavaScript files – Put in scripts folder under “resources” and use 52 <h:outputScript name="blah.js" library="scripts"/>
  • 26. Component File (resources/utils/info3.xhtml) (resources/utils/info3 xhtml) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …> <html … xmlns:composite="http://java.sun.com/jsf/composite" p p j j p xmlns:utils="http://java.sun.com/jsf/composite/utils"> … <composite:interface> <composite:attribute name="styleClass"/> name= styleClass /> </composite:interface> <composite:implementation> <table align="right"> Actual file is resources/images/duke-guitar.png resources/images/duke guitar.png <tr><th> <h:graphicImage name="duke-guitar.png" library="images"/> </th></tr> <tr><th> <a href="#{request.contextPath}/composite-components.jsf"> Return to main page</a> </th></tr> </table> <utils:info2 styleClass="#{cc.attrs.styleClass}"/> </composite:implementation> 53 </body></html> Main File 1 (relative-urls.xhtml) (relative-urls xhtml) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …> <html xmlns="http://www w3 org/1999/xhtml" xmlns= http://www.w3.org/1999/xhtml xmlns:h="http://java.sun.com/jsf/html" xmlns:utils="http://java.sun.com/jsf/composite/utils"> <h:head><title>Composite Components</title> <h:outputStylesheet name="styles.css" library="css"/> </h:head> y <h:body> … <fieldset> <legend>Component that Uses Relative URLs</legend> <utils:info3/> </fieldset> … </h:body></html> 54
  • 27. Main File 2 (someDirectory/relative-urls.xhtml) (someDirectory/relative-urls xhtml) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 …> <html xmlns="http://www w3 org/1999/xhtml" xmlns= http://www.w3.org/1999/xhtml xmlns:h="http://java.sun.com/jsf/html" xmlns:utils="http://java.sun.com/jsf/composite/utils"> <h:head><title>Composite Components</title> <h:outputStylesheet name="styles.css" library="css"/> </h:head> y <h:body> … <fieldset> <legend>Component that Uses Relative URLs</legend> <utils:info3/> </fieldset> … This is an exact copy of the file from the top-level directory. </h:body></html> 55 Results 56
  • 28. © 2011 Marty Hall Wrap-Up Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. Summary • Define a component – Put foo.xhtml in “resources/bar” folder – Define available attributes inside composite:interface • Use <composite:attribute name="attributeName" … /> name= attributeName – Build output inside composite:implementation • Use #{cc.attrs.attributeName} • Possibly use ui:repeat. Be careful with relative URLs. • Use component in main page – Define namespace • <html … xmlns:bar="http://java.sun.com/jsf/composite/bar"> – Use the component i facelets page h in f l • <bar:foo attributeName="…" /> 58
  • 29. © 2011 Marty Hall Questions? Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.