{"id":1429,"date":"2010-08-23T11:31:00","date_gmt":"2010-08-23T16:31:00","guid":{"rendered":"http:\/\/www.andygibson.net\/blog\/?p=1429"},"modified":"2010-08-24T09:01:41","modified_gmt":"2010-08-24T14:01:41","slug":"seam-faces-makes-jsf-conversion-a-breeze","status":"publish","type":"post","link":"https:\/\/www.andygibson.net\/old-blog\/article\/seam-faces-makes-jsf-conversion-a-breeze\/","title":{"rendered":"Seam Faces makes JSF entity converters a breeze"},"content":{"rendered":"<p>One of the first Seam 3 Modules to appear is the Seam Faces module which provides additional functionality to JSF. While there aren&#8217;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.<br \/>\n<!--more--><\/p>\n<div class=\"sidenote_right\">\n<h1>Download<\/h1>\n<p>Download the <a href='http:\/\/www.andygibson.net\/blog\/wp-content\/uploads\/2010\/08\/seamfaces.zip'>Seam Faces Demo Source<\/a> for Maven\n<\/div>\n<p>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. <\/p>\n<p>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&#8217;t allow any kind of resource injection in converters so it was always a problem.  Seam 2 used a <code>s:convertEntity<\/code> tag just for the purpose of loading the entity from the database. With Seam 3 however, you&#8217;ll be able to write your own converter using standard JSF rather than using specialized tags.<\/p>\n<p>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. <\/p>\n<p>Here&#8217;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.<\/p>\n<p>I created a new project and in the <code>DataFactory.java<\/code> class I added a producer method to return a list of teachers.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Produces\t\r\n@Named(&quot;teachers&quot;)\r\n@ApplicationScoped\r\npublic List&lt;Teacher&gt; getTeachers() {\r\n\tList&lt;Teacher&gt; teachers = entityManager.createQuery(\r\n\t\t\t&quot;select t from Teacher t&quot;)\r\n\t\t\t.getResultList();\r\n\treturn teachers;\r\n}\r\n<\/pre>\n<p>This makes an application scoped list of teachers available in our application, where they will be used to provide a lookup list.<\/p>\n<p>Now we&#8217;ll create a backing bean that has a teacher attribute that we want to set via the lookup list.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage org.knappsack.demo.seam.seamfaces.bean;\r\n\r\nimport javax.enterprise.context.RequestScoped;\r\nimport javax.inject.Named;\r\n\r\nimport org.knappsack.demo.seam.seamfaces.model.Teacher;\r\n\r\n@Named\r\n@RequestScoped\r\npublic class PageBean {\r\n\r\n\tprivate Teacher selected;\r\n\t\r\n\tpublic Teacher getSelected() {\r\n\t\treturn selected;\r\n\t}\r\n\tpublic void setSelected(Teacher selected) {\r\n\t\tthis.selected = selected;\r\n\t}\r\n}\r\n<\/pre>\n<p>No we&#8217;ll write our converter which implements the <code>javax.faces.convert.Converter<\/code> interface. <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@SessionScoped\r\n@FacesConverter(&quot;convert.teacher&quot;)\r\npublic class TeacherConverter implements Converter,Serializable {\r\n\r\n\t@Inject\r\n\t@DataRepository\r\n\tprivate EntityManager entityManager; \r\n\t\r\n\t@Override\r\n\tpublic Object getAsObject(FacesContext context, UIComponent component, String value) {\r\n\t   Long id = Long.valueOf(value);\t   \r\n\t\treturn entityManager.find(Teacher.class, id);\r\n\t}\r\n \r\n\t@Override \t \r\n\tpublic String getAsString(FacesContext context, UIComponent component, Object value) {\t\t\r\n\t\treturn ((Teacher)value).getId().toString();\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>The <code>@FacesConverter<\/code> 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.<br \/>\nTo convert the object to text, we just return the <code>id<\/code> of the object. To convert from text to an object (a <code>Teacher<\/code> instance) we load the instance from the database using the injected entity manager. <\/p>\n<p>To use this converter we just add this content of the <code>home.xhtml<\/code> page :<\/p>\n<pre class=\"brush: java; highlight: [5,6,7,8]; title: ; notranslate\" title=\"\">\r\n\t&lt;ui:define name=&quot;content&quot;&gt;\r\n\t\t&lt;h:form id=&quot;form&quot;&gt;\r\n\t\t\t&lt;h:outputText value=&quot;Selected : #{pageBean.selected.name}&quot; id=&quot;msg&quot; \/&gt;\r\n\t\t\t&lt;br \/&gt;\r\n\t\t\t&lt;h:selectOneListbox value=&quot;#{pageBean.selected}&quot; id=&quot;selector&quot;\r\n\t\t\t\tconverter=&quot;convert.teacher&quot;&gt;\r\n\t\t\t\t&lt;f:selectItems value=&quot;#{teachers}&quot; id=&quot;items&quot; var=&quot;v_teacher&quot;\r\n\t\t\t\t\titemLabel=&quot;#{v_teacher.name}&quot; \/&gt;\t\t\t\t\r\n\t\t\t&lt;\/h:selectOneListbox&gt;\r\n\r\n\t\t\t&lt;h:commandButton value=&quot;Update&quot; action=&quot;update&quot; \/&gt;\r\n\t\t&lt;\/h:form&gt;\r\n\t&lt;\/ui:define&gt;\r\n<\/pre>\n<div class=\"contentBox alignCenter\" >\n<img src=\"http:\/\/www.andygibson.net\/blog\/wp-content\/uploads\/2010\/08\/seamfaces.png\" alt=\"Seam Faces Screenshot\" title=\"Seam Faces Screenshot\" \/>\n<\/div>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>Download the <a href='http:\/\/www.andygibson.net\/blog\/wp-content\/uploads\/2010\/08\/seamfaces.zip'>Seam Faces Demo Source<\/a> for Maven, unzip it and see the <code>readme.txt<\/code> file for deployment instructions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the first Seam 3 Modules to appear is the Seam Faces module which provides additional functionality to JSF. While there aren&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[63],"tags":[49,32,8],"_links":{"self":[{"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/posts\/1429"}],"collection":[{"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/comments?post=1429"}],"version-history":[{"count":24,"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/posts\/1429\/revisions"}],"predecessor-version":[{"id":1454,"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/posts\/1429\/revisions\/1454"}],"wp:attachment":[{"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/media?parent=1429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/categories?post=1429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.andygibson.net\/old-blog\/wp-json\/wp\/v2\/tags?post=1429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}