JSF Composition Components with AliasBean

MyFaces Tomahawk Alias Bean is a powerful feature for creating generic composition components in JSF. Main idea is to set a dummy alias name and refer it to a real managed bean in context, actually it’s very similiar to the approach Facelets follow. Get a reference and replace it with the real deal.

As an example suppose we need to add Geo Location information to some of the entities in our application. First thing to do is to create the GeoLocation class with the lattitute and longitute.

GeoLocation

  
    
    

package domain;

public class GeoLocation {

  private String lattitude;
  private String longitude;
  
  public GeoLocation() {}
  
  public GeoLocation(String lattitude, String longidute) {
    this.lattitude = lattitude;
    this.longitude = longidute;
  }
  
  public String getLattitude() {
    return lattitude;
  }
  public void setLattitude(String lattitude) {
    this.lattitude = lattitude;
  }
  
  public String getLongitude() {
    return longitude;
  }
  public void setLongitude(String longitude) {
    this.longitude = longitude;
  }
}      

And an example entity that needs a GeoLocation information. Let’s say a Stadium, why not:)

Stadium

  
    
    

package domain;

public class Stadium {

  private String name;
  private String club;
  private GeoLocation geoLocation = new GeoLocation();
  
  public Stadium() {}
  
  public Stadium(String name, String club, GeoLocation geoLocation) {
    this.name = name;
    this.club = club;
    this.geoLocation = geoLocation;
  }
  
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getClub() {
    return club;
  }
  public void setClub(String club) {
    this.club = club;
  }

  public GeoLocation getGeoLocation() {
    return geoLocation;
  }
  public void setGeoLocation(GeoLocation geoLocation) {
    this.geoLocation = geoLocation;
  }

}      

After creating the domain entities, we need a JSF page and a backing bean to create a new Stadium.

StadiumEditBean

  
    
    

package pages;

import domain.Stadium;

public class StadiumEditBean {
  
  private Stadium stadium;

  public Stadium getStadium() {
    ifstadium == null)
      stadium = new Stadium();
    return stadium;
  }

  public void setStadium(Stadium stadium) {
    this.stadium = stadium;
  }
  
  public void doSaveAction() {
    //save the stadium with the underlying technology
  }
}      

Finally the JSF Page for StadiumEdit and the GeoLocation jsp fragment

StadiumEdit.jsp

<%@ taglib uri=”http://java.sun.com/jsf/html&#8221; prefix=”h”%>
<%@ taglib uri=”http://java.sun.com/jsf/core&#8221; prefix=”f”%>
<%@ taglib uri=”http://myfaces.apache.org/tomahawk&#8221; prefix=”t”%>
<%@ taglib uri=”http://myfaces.apache.org/sandbox&#8221; prefix=”s” %>
<html>
<head>
<title></title>
</head>
<body>
<f:view>
    <h:form>
        <h:panelGrid columns=”2″>
            <h:outputLabel value=”Name” for=”stadium_name”></h:outputLabel>
            <h:inputText id=”stadium_name” value=”#{StadiumEditBean.stadium.name}” />

            <h:outputLabel value=”Club” for=”stadium_club”></h:outputLabel>
            <h:inputText id=”stadium_club” value=”#{StadiumEditBean.stadium.club}” />
        </h:panelGrid>
   
        <t:aliasBeansScope>
            <t:aliasBean alias=”#{geoLocationAlias}” value=”#{StadiumEditBean.stadium.geoLocation}” />
            <f:subview id=”geoLocationView”>
                <%@ include file=”geoLocation.jspf”%>
            </f:subview>
        </t:aliasBeansScope>
   
        <h:commandButton action=”” value=”Submit” />
    </h:form>
</f:view>
</body>
</html>

GeoLocation.jspf

<%@ page pageEncoding=”utf-8″%>
<s:fieldset legend=”Geo Location”>
    <h:panelGrid columns=”2″>
        <h:outputLabel value=”Lattitude” for=”txt_lattitude”></h:outputLabel>
        <h:inputText id=”txt_lattitude” value=”#{geoLocationAlias.lattitude}” />

        <h:outputLabel value=”Longitude” for=”txt_longitude”></h:outputLabel>
        <h:inputText id=”txt_longitude” value=”#{geoLocationAlias.longitude}” />
    </h:panelGrid>
</s:fieldset>

In the end, this is how it looks;

A Facelets user probably doesn’t need Alias Bean since Facelets has already have built-in support for the same functionality but if
you’re using JSP as the view definition of JSF then Alias Bean can help you a lot for creating composite components. Although I’ve not practiced yet JSP2.1 tag files also seem promising and provide composition similar to Facelets. The other alternative way is coding the components programmatically, which looks like a torture compared to the others.

2 Responses to JSF Composition Components with AliasBean

  1. Rafael Nami says:

    Wow! I didn’t know that myfaces’s aliasBean has such immense potential… I’ll try to get this kinda stuff in my app right now. Thanks for this post, Cagatay.

  2. Dora says:

    I have tried out this demo and it works perfectly. When I on the otherhand try this out in my project then I get the following error in every other request.
    javax.servlet.ServletException: Cannot get value for expression ‘#{cardApplicationListBb.searchParameters}’
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:152)
    org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:181)
    edu.yale.its.tp.cas.client.filter.CASFilter.doFilter(CASFilter.java:317)

    Have you seen this problem?

%d bloggers like this: