DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. Seam Faces makes JSF entity Converters a Breeze

Seam Faces makes JSF entity Converters a Breeze

Andy Gibson user avatar by
Andy Gibson
·
Aug. 24, 10 · Interview
Like (0)
Save
Tweet
Share
8.83K Views

Join the DZone community and get the full member experience.

Join For Free

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 has a s:convertentity tag just for the purpose of loading the entity from the database.
with special tags, it is often easier to just hook up lookup components to long attributes and fetch the actual entity manually on the server side.

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;
}
}

now 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.

from http://www.andygibson.net/blog/article/seam-faces-makes-jsf-conversion-a-breeze/

Database

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Agile Architecture Spikes Are Used in Shift-Left BDD
  • Getting a Private SSL Certificate Free of Cost
  • 5 Steps for Getting Started in Deep Learning
  • Unlock the Full Potential of Git

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: