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” prefix=”h”%>
<%@ taglib uri=”http://java.sun.com/jsf/core” prefix=”f”%>
<%@ taglib uri=”http://myfaces.apache.org/tomahawk” prefix=”t”%>
<%@ taglib uri=”http://myfaces.apache.org/sandbox” 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.

Posted in Java. 2 Comments »

Life at Home Office

At the end of august, I’ve left the company and the project that I worked for a year and nowadays I’m working as a freelancer for a totally new project. The idea of working remotely at home seems cool at first glance, I’ll see how it goes but the truth is I’m currently enjoying it a lot. Good things about working remotely are that you can drink beer and play games whenever you want. Sometimes I stuck at somewhere and give a break to play Pro Evolution Soccer 5. After kicking some Real Madrid ass with F.C.Barcelona, it feels so good and I come back with a fresh mind. The thing I could not do at first days was ending a break. Can’t remember how many hours I’ve spent on ProEvo5 doing tricks with Ronaldinho and on NeedForSpeedU2 tuning my Mazda RX-8.

Free Image Hosting at www.ImageShack.us Free Image Hosting at www.ImageShack.us

The current project I’m working for is a JSF(MyFaces)-Hibernate project that uses maven2 for building. My previous project was JSF(RI+IBM)-Spring-Hibernate project with Ant. At first I’ve missed Spring but now I’m thinking that it is no big deal. I’ll blog about it as “Life without Spring”. Anyway at last I’m using MyFaces in production and have a chance to stay away from IBM’s components. One of the new stuff I’ve learned is using Hibernate Annotations as the metadata for hibernate mapping, my previous project was using xdoclet first and then a plugin to create hbm.xml from uml diagrams. Using annotations is fun and very easy too by the way. For the build, I prefer maven2 over ant because of the fact that the maven guys say in their book, “Building the build”. I’m also using maven2 for MyFaces so I’m quite familiar with mvn. By the way, at last I’ve more time to do open source work and work on the MyFaces jira issues assigned to me :) . Long story short, it’s totally a new experience for me and I hope the current joy goes on.

Posted in Java. 5 Comments »