JSF Tutorial - JSF Composite Components Example








JSF can define custom components to render custom contents.

In order to create a custom component, we need to create a resources folder. and put a xhtml file in resources folder with a composite namespace.

We need to use composite tags composite:interface, composite:attribute and composite:implementation, to define content of the composite component.

Then use cc.attrs in composite:implementation to get variable defined using composite:attribute in composite:interface.

The following code shows how to use the composite:interface and composite:implementation.

    <composite:interface>
    
      <composite:attribute name="nameLable" />
      <composite:attribute name="nameValue" />
      <composite:attribute name="emailLable" />
      <composite:attribute name="emailValue" />
      
       <composite:attribute name="registerButtonText" />
      <composite:attribute name="registerButtonAction" 
        method-signature="java.lang.String action()" />
        
    </composite:interface>
  
  <composite:implementation>
  
    <h:form>
      
      <h:message for="textPanel" />
      
      <h:panelGrid columns="2" id="textPanel">
      
        #{cc.attrs.nameLable} : 
        <h:inputText id="name" value="#{cc.attrs.nameValue}" />
        
        #{cc.attrs.emailLable} : 
        <h:inputText id="email" value="#{cc.attrs.emailValue}" />
        
      </h:panelGrid>
      
      <h:commandButton action="#{cc.attrs.registerButtonAction}" 
        value="#{cc.attrs.registerButtonText}"/>
    </h:form>
  </composite:implementation>
  




Example

The following code is from result.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 xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      >
 
    <h:body>
 
      <h1>Composite Components in JSF 2.0</h1>
 
      Name : #{user.name}
      
      <br />
      
      E-mail : #{user.email}
      
    </h:body>
 
</html>

The following code is from demo.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 xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:java2s="http://java.sun.com/jsf/composite/java2s"
      >
 
    <h:body>
 
    <java2s:register 
      nameLable="Name" 
      nameValue="#{user.name}" 
      emailLable="E-mail" 
      emailValue="#{user.email}"

      registerButtonText="Register" 
      registerButtonAction="#{user.registerAction}"
       />
  
    </h:body>
 
</html>

The following code is from UserBean.java.

package com.java2s.common;


import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="user")
@SessionScoped
public class UserBean{
 
  public String name;
  public String email;
  
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getEmail() {
    return email;
  }
  public void setEmail(String email) {
    this.email = email;
  }

  public String registerAction(){
    return "result";
  }
}

The following code is from register.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 xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:composite="http://java.sun.com/jsf/composite">
    
    <composite:interface>
    
      <composite:attribute name="nameLable" />
      <composite:attribute name="nameValue" />
      <composite:attribute name="emailLable" />
      <composite:attribute name="emailValue" />
      
       <composite:attribute name="registerButtonText" />
      <composite:attribute name="registerButtonAction" 
        method-signature="java.lang.String action()" />
        
    </composite:interface>
  
  <composite:implementation>
  
    <h:form>
      
      <h:message for="textPanel" />
      
      <h:panelGrid columns="2" id="textPanel">
      
        #{cc.attrs.nameLable} : 
        <h:inputText id="name" value="#{cc.attrs.nameValue}" />
        
        #{cc.attrs.emailLable} : 
        <h:inputText id="email" value="#{cc.attrs.emailValue}" />
        
      </h:panelGrid>
      
      <h:commandButton action="#{cc.attrs.registerButtonAction}" 
        value="#{cc.attrs.registerButtonText}"/>
    </h:form>
  </composite:implementation>
</html>


Download Composite-Components.zip





To RUN

Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.

After Tomcat finish starting, type the following URL in the browser address bar.

http://localhost:8080/simple-webapp/demo.xhtml