One of the first Seam 3 Modules to appear is the Seam Faces module which provides additional functionality to JSF. While there aren’t many pain points left in JSF, one of the biggest is the issue of data converters for entity objects. This article will take a look at how Seam Faces takes the pain out of writing JSF converters.

Download

Download the Seam Faces Demo Source for Maven

Typically, JSF converters come in two different types, the first converts strings to other strings, numbers or dates and vice-versa. Examples might be dates, formatted phone numbers or social security numbers. These converter are usually isolated and rely on internal logic defined in the converter.

The second kind, which is far more problematic, involves using information outside the converter to perform the conversion. The simplest case being an entity lookup which is represented in the view layer by the primary key value and upon posting back to the server, converted to the entity based on that id. These converters require some mechanism to get the entity from the database based on the Id. JSF doesn’t allow any kind of resource injection in converters so it was always a problem. Seam 2 used a s:convertEntity tag just for the purpose of loading the entity from the database. With Seam 3 however, you’ll be able to write your own converter using standard JSF rather than using specialized tags.

The Seam 3 module provides features that augment the existing JSF feature set and provides among other things, converters that can have beans injected to them like they are CDI beans. With this, we can implement converters that take the Id and load the entity from the database.

Here’s some quick demo code to demonstrate the process. The application uses the jee6-sandbox Knappsack archetype and requires either Glassfish or JBoss AS 6 to run.

I created a new project and in the DataFactory.java class I added a producer method to return a list of teachers.

@Produces	
@Named("teachers")
@ApplicationScoped
public List<Teacher> getTeachers() {
	List<Teacher> teachers = entityManager.createQuery(
			"select t from Teacher t")
			.getResultList();
	return teachers;
}

This makes an application scoped list of teachers available in our application, where they will be used to provide a lookup list.

Now we’ll create a backing bean that has a teacher attribute that we want to set via the lookup list.

package org.knappsack.demo.seam.seamfaces.bean;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

import org.knappsack.demo.seam.seamfaces.model.Teacher;

@Named
@RequestScoped
public class PageBean {

	private Teacher selected;
	
	public Teacher getSelected() {
		return selected;
	}
	public void setSelected(Teacher selected) {
		this.selected = selected;
	}
}

No we’ll write our converter which implements the javax.faces.convert.Converter interface.

@SessionScoped
@FacesConverter("convert.teacher")
public class TeacherConverter implements Converter,Serializable {

	@Inject
	@DataRepository
	private EntityManager entityManager; 
	
	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
	   Long id = Long.valueOf(value);	   
		return entityManager.find(Teacher.class, id);
	}
 
	@Override 	 
	public String getAsString(FacesContext context, UIComponent component, Object value) {		
		return ((Teacher)value).getId().toString();
	}

}

The @FacesConverter annotation marks this class as a converter when it is processed by CDI and gives it a converter Id for use in a JSF page.
To convert the object to text, we just return the id of the object. To convert from text to an object (a Teacher instance) we load the instance from the database using the injected entity manager.

To use this converter we just add this content of the home.xhtml page :

	<ui:define name="content">
		<h:form id="form">
			<h:outputText value="Selected : #{pageBean.selected.name}" id="msg" />
			<br />
			<h:selectOneListbox value="#{pageBean.selected}" id="selector"
				converter="convert.teacher">
				<f:selectItems value="#{teachers}" id="items" var="v_teacher"
					itemLabel="#{v_teacher.name}" />				
			</h:selectOneListbox>

			<h:commandButton value="Update" action="update" />
		</h:form>
	</ui:define>
Seam Faces Screenshot

This page presents a list of teachers in which you can select one, click the update button and the selected teacher message at the top will change. The the only additional code we have is the converter which is a very simple class to write and can be re-used everywhere we have a teacher entity by just specifying the converter name. You could even create a generic entity converter to be used for all your entities.

Injection is not just limited to entity managers, we can inject any kind of bean in there so our options are limitless. One good use case is the injection and re-use of application scoped lists of cached data instead of re-hitting the database all the time.

Download the Seam Faces Demo Source for Maven, unzip it and see the readme.txt file for deployment instructions.