Pages

Saturday, August 13, 2011

Just a mini pragmatical example of how to pass parameters from a composite component back to a template


The template layout:
 <!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:ui="http://java.sun.com/jsf/facelets"    
    xmlns:h="http://java.sun.com/jsf/html"  
    xmlns:f="http://java.sun.com/jsf/core">  
 <h:head>  
  <title><ui:insert name="pagetitle">Default page title</ui:insert></title>  
 </h:head>  
 <h:body>  
  <ui:insert name="content">  
                 Default page content  
  </ui:insert>  
  <!-- USE THE PARAMETER NAME TO CALL VARIABLES THAT ARE PASSED AS PARAMETERS FROM SOME COMPONENTS -->  
  #{someParam}  
 </h:body>  
 </html>  
 The composite component  
 <!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:ui="http://java.sun.com/jsf/facelets"  
      xmlns:h="http://java.sun.com/jsf/html"  
      xmlns:f="http://java.sun.com/jsf/core">  
 <ui:composition template="WEB-INF/templates/masterLayout.xhtml">  
      <ui:define name="pagetitle">  
                     This is the index page  
      </ui:define>  
      <ui:define name="content">  
           <p>A value can be passed from the composition to a template using the ui:param tag</p>  
           <ui:param name="someParam" value="#{someBB.something}"/>  
      </ui:define>       
 </ui:composition>  
 </html>  
And now the backing bean that holds the value:
 package backingbeans;  
 import javax.enterprise.context.RequestScoped;  
 import javax.inject.Named;  
 @Named("someBB")  
 @RequestScoped  
 public class SomeBB {  
      private String something;  
      public SomeBB() {  
           something = "THIS IS SOME DEFAULT VALUE";  
      }  
      public String getSomething() {  
           return something;  
      }  
      public void setSomething(String something) {  
           this.something = something;  
      }  
 }  


No comments:

Post a Comment

Share with your friends