JSF Composition Components with AliasBean
Posted by cagataycivici on September 25, 2006
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
|
And an example entity that needs a GeoLocation information. Let’s say a Stadium, why not:)
Stadium
|
After creating the domain entities, we need a JSF page and a backing bean to create a new Stadium.
StadiumEditBean
|
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.

September 28, 2006 at 1:54 pm
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.
April 2, 2007 at 6:21 pm
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?