Sunday, February 9, 2014

About 

“Composite Component” is a JSF 2.0 / Facelets specific term for reuseable UI components which are declared using pure XML rather than Java. The composite component XML declaration tags are available under the namespace http://java.sun.com/jsf/composite. Since JSF 2.2, the namespacehttp://xmlns.jcp.org/jsf/composite can be used instead.

Creating composite components

Prepare directory structure

First create a directory resources in the public webcontent (there where the WEB-INF directory and all regular Facelets files also are).
WebContent
 |-- WEB-INF
 |    `-- lib
 |-- resources   <--- code="" test.xhtml="">
JSF 2.2 and later allows to change the resources directory by specifying a parameter namedjavax.faces.WEBAPP_RESOURCES_DIRECTORY in the  file. It may be reasonable to change the directory to, for example, /WEB-INF/resources, since files under /WEB-INF are not readable via HTTP.
In the resources directory, create a directory exclusively for composite components. The directory name ends up as the extra path in the composite component namespace URI. You're free to choose the name. We'll take mycomponents as an example.
WebContent
 |-- WEB-INF
 |    `-- lib
 |-- resources
 |    `-- mycomponents   <--- code="" test.xhtml="">
This way the composite components in this directory are available in all templates by the following namespace:
 xmlns:my="http://java.sun.com/jsf/composite/mycomponents">
The prefix my is free to your choice. The http://java.sun.com/jsf/composite/ part is mandatory. The mycomponents part should be just the same as the directory name.
As a Hello World composite component example, we'll create composite component which shows a simple rating score with stars. We need to create a new XHTML file. The filename becomes the composite component tag name. You're free to choose the name. Let's call it rating.xhtml.
WebContent
 |-- WEB-INF
 |    `-- lib
 |-- resources
 |    `-- mycomponents
 |         `-- rating.xhtml   <--- code="" test.xhtml="">
This way the composite component is available in all templates as follows:
 />

Create composite component

Here's how a basic composite component template look like. Put this in rating.xhtml.

    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:cc="http://java.sun.com/jsf/composite"
>
    
        
    
We'd like to define the following attributes:
  • score, integer, required. The star score.
  • maxScore, integer, optional, default 100. The maximum possible score.
  • totalStars, integer, optional, default 10. The total amount of stars to display.
Now, here's how the final implementation look like. Note that the attributes are available by #{cc.attrs.attributename}.
 
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:cc="http://java.sun.com/jsf/composite"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions"
>
    
         name="score" type="java.lang.Integer" required="true" />
         name="maxScore" type="java.lang.Integer" default="100" />
         name="totalStars" type="java.lang.Integer" default="10" />
    
var="filled" value="#{fn:substringBefore(cc.attrs.score / (cc.attrs.maxScore / cc.attrs.totalStars), '.')}" /> style="font-size: 1.5em;"> begin="1" end="#{cc.attrs.totalStars}" varStatus="loop"> value="★" rendered="#{loop.index le filled}" /> value="☆" rendered="#{loop.index gt filled}" />
Here's how you can use it in test.xhtml:

 lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:my="http://java.sun.com/jsf/composite/mycomponents"
>
    
        </span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">Rating composite component demo</span><span class="tag" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">
    
score="60" />
/> score="5" maxScore="10" />
/> score="80" totalStars="5" />
/>
Here's how the result should look like (only if your browser supports the Unicode star fonts; you're free to replace them by real images or even introduce a half star, which unfortunately isn't available in Unicode):

★★★★★★☆☆☆☆

★★★★★☆☆☆☆☆

★★★★☆

Create backing component

The above implementation has one disadvantage due to use the use of JSTL  : as JSTL runs during view build time instead of view render time, the above implementation cannot be used inside a repeating component such as  or . We would like to use an instead, but it doesn't support begin and end attributes. So we'd like to attach some Java code so that it converts the totalStars to a blank object array of exactly that size so that it can be used in the value attribute. Ideally, this would be done using an EL function, but for learning/wiki purposes we'll use a so-called "backing component" instead.
To create such a backing component, we need to create a class which extends UINamingContainer. Here's a basic template:
package com.example;

import javax.faces.component.FacesComponent;
import javax.faces.component.UINamingContainer;

@FacesComponent("myCompositeComponent")
public class MyCompositeComponent extends UINamingContainer {

    // ...

}
Note the value of the @FacesComponent attribute. It's the one which you should specify in thecomponentType attribute of the  tag:
 componentType="myCompositeComponent">
This way an instance of the backing component will be used behind the #{cc} variable instead. This offers you the possibility to define getter and action methods like value="#{cc.items}"action="#{cc.doSomething}" and so on. All of the  attribues are available in the backing component by the inherited UIComponent#getAttributes() method which provides easy access to the attributes.
For our rating composite component, the backing component implementation should look like this:
package com.example;

import javax.faces.component.FacesComponent;
import javax.faces.component.UINamingContainer;

@FacesComponent("ratingComponent")
public class RatingComponent extends UINamingContainer {

    public Object[] getItems() {
        Object totalStars = getAttributes().get("totalStars");
        int size = Integer.valueOf(String.valueOf(totalStars));
        return new Object[size];
    }

}
And here's how the rating.xhtml should now look like:
 
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:cc="http://java.sun.com/jsf/composite"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions"
>
     componentType="ratingComponent">
         name="score" type="java.lang.Integer" required="true" />
         name="maxScore" type="java.lang.Integer" default="100" />
         name="totalStars" type="java.lang.Integer" default="10" />
    
    
         var="filled" value="#{fn:substringBefore(cc.attrs.score / (cc.attrs.maxScore / cc.attrs.totalStars), '.')}" />
         style="font-size: 1.5em;">
             value="#{cc.items}" varStatus="loop">
                 value="★" rendered="#{loop.index lt filled}" />            
                 value="☆" rendered="#{loop.index ge filled}" />            
            
        
    
Note the value="#{cc.items}" in the above example. It basically calls getItems() method on the instance of the backing component. Also note that we got rid of JSTL , so the above will work properly inside a repeating JSF component such as  and so on.
 value="#{bean.products}" var="product">
    #{product.name}
score="#{product.rating}" />

Online resources:

Related tag info pages:

history | show excerpt | excerpt history

3 comments:

oakleyses said...

jordan pas cher, chanel handbags, nike outlet, michael kors pas cher, kate spade outlet, replica watches, longchamp pas cher, nike free, jordan shoes, christian louboutin shoes, nike free run, louis vuitton outlet, oakley sunglasses, ray ban sunglasses, polo ralph lauren, ugg boots, christian louboutin uk, air max, louis vuitton outlet, ugg boots, tiffany jewelry, polo ralph lauren outlet online, burberry pas cher, prada outlet, nike air max, gucci handbags, sac longchamp pas cher, ray ban sunglasses, louboutin pas cher, louis vuitton, uggs on sale, tiffany and co, oakley sunglasses, louis vuitton outlet, ray ban sunglasses, longchamp outlet, louis vuitton, longchamp outlet, replica watches, nike roshe, polo outlet, oakley sunglasses, cheap oakley sunglasses, oakley sunglasses wholesale, christian louboutin, christian louboutin outlet, tory burch outlet

oakleyses said...

lululemon canada, nike air max, burberry outlet, oakley pas cher, burberry handbags, coach outlet store online, kate spade, michael kors outlet, michael kors, nike air force, true religion jeans, true religion outlet, michael kors, polo lacoste, nike tn, new balance, abercrombie and fitch uk, michael kors outlet, uggs outlet, michael kors outlet, ralph lauren uk, michael kors outlet online, replica handbags, coach outlet, true religion outlet, coach purses, nike free uk, sac vanessa bruno, mulberry uk, michael kors outlet online, michael kors outlet online, michael kors outlet online, north face, uggs outlet, converse pas cher, hogan outlet, nike air max uk, hollister pas cher, sac hermes, nike roshe run uk, hollister uk, nike air max uk, true religion outlet, timberland pas cher, vans pas cher, ray ban pas cher, guess pas cher, ray ban uk

Aniyanewjackson said...

Nationwide lockdown has been imposed in backdrop of COVID 19 outbreak. {tag: Yeezy Boost 350 V2 Black And White Price}This is a long story for a very small point, but I was in Mississippi like five years ago and I was talking to a guy who worked at the hall where I was playing. {tag: Yeezy White Cream 350}

Craig."The Avenue Pub is located at 405 23rd Avenue in Tuscaloosa, while Central Mesa is located at 519 Greensboro Avenue.The Alabama Department of Public Health has banned on premise consumption of food and beverages at restaurants, bars and breweries throughout the entire state.Citing a "rapidly developing situation," Tuscaloosa Mayor Walt Maddox last Thursday extended a city wide curfew to 24 hours. {tag: Cream White Yeezy Real Vs Fake}

As many filmmakers are doing these days, to keep people entertained during the lockdowns or social distancing days, Snyder also did a live commentary of the Ultimate Edition of the 2016 film. {tag: Womens White Yeezy Boost}

Black N Blue Jordan 12, Sometimes just taking a shower can be a challenge. Brushing your hair a struggle. Here I am with roots coming in, and can go to the salon to get my hair colored so I going to add insult to injury and not make sure my hair is groomed properly? No.

We recognize group calling is particularly critical right now. {tag: Yeezy Boost All White}Don't think about yourself. You've got to think about your team, because I let the team down.It's a family here. You've got to realize that this is a family, and whatever you do impacts the family, the team.""Another incident that impacted the Jefferson family happened over the summer with the shooting death of Deante Strickland, the nephew of Coach Strickland. "