<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>REVERT TO CONSOLE &#187; JSF</title>
	<atom:link href="http://www.reverttoconsole.com/blog/category/jsf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.reverttoconsole.com</link>
	<description>for f in *;do echo &#124; sed &#039;i\rtc&#039; &#62;&#62; $f;done; java programming et al</description>
	<lastBuildDate>Thu, 25 Aug 2011 15:02:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Generic Entity Converter for JSF / Spring / JPA</title>
		<link>http://www.reverttoconsole.com/blog/jsf/generic-entity-converter-for-jsf-spring-jpa/</link>
		<comments>http://www.reverttoconsole.com/blog/jsf/generic-entity-converter-for-jsf-spring-jpa/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 13:27:01 +0000</pubDate>
		<dc:creator>Priyatam</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://www.reverttoconsole.com/?p=330</guid>
		<description><![CDATA[If you&#8217;ve come across Seam&#8217;s &#60;s:convertEntity&#62; and wondered why isn&#8217;t one there for JSF, here you go: Before using this, you should&#8217;ve designed your domain model to extend a Base Class (do it, it&#8217;s a good practice). In my case, I have BaseIdentityEntity which has an @Id, @Version properties import java.io.Serializable; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve come across Seam&#8217;s &lt;s:convertEntity&gt; and wondered why isn&#8217;t one there for JSF, here you go:</p>
<p>Before using this, you should&#8217;ve designed your domain model to extend a Base Class (do it, it&#8217;s a good practice). In my case, I have BaseIdentityEntity which has an @Id, @Version properties</p>
<pre>
<pre>
import java.io.Serializable;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import xxx.model.BaseIdentityEntity;

/**
* Generic Entity Converter for any Entity that extends BaseIdentityEntity
*
*/
@Service("entityConverter")
public class EntityConverter implements Converter, Serializable {
	static Logger log = Logger.getLogger(EntityConverter.class);
	private static final long serialVersionUID = -5137676309479323480L;

	@PersistenceContext
	protected EntityManager entityManager;

	@SuppressWarnings("unchecked")
	public Object getAsObject(FacesContext facesContext, UIComponent component,
			String value) throws ConverterException {
		BaseIdentityEntity entity;
		if (value == null) {
			entity = null;
		} else {
			Integer id = new Integer(value);
			entity = (BaseIdentityEntity) entityManager.find(getClazz(
					facesContext, component), id);
			if (entity == null) {
				log.debug("There is no entity with id:  " + id);
				//throw new IllegalArgumentException("There is no entity with id:  " + id);
			}
		}
		return entity;
	}

	public String getAsString(FacesContext facesContext, UIComponent component,
			Object value) throws ConverterException {
		if (value != null &#038;&#038; !(value instanceof BaseIdentityEntity)) {
			throw new IllegalArgumentException(
					"This converter only handles instances of BaseIdentityEntity");
		}
		if (value == null) {
			return "";
		}
		if (value instanceof String) {
			return (String) value;
		}
		BaseIdentityEntity entity = (BaseIdentityEntity) value;
		return entity.getId() == null ? "" : entity.getId().toString();
	}

	// Gets the class corresponding to the context in jsf page
	@SuppressWarnings("unchecked")
	private Class getClazz(FacesContext facesContext, UIComponent component) {
		return component.getValueExpression("value").getType(facesContext.getELContext());
	}

}
</pre>
</pre>
<p>You don&#8217;t have to add any configuration in the taglibs since Spring&#8217;s @Service tag makes the converter a managed bean and you can reference it directly in jsf.</p>
<p>Usage: (Using JSF tomahawk here, but works for any jsf component library)</p>
<pre>
<pre>
&lt;h:selectOneMenu value=&quot;#{mycontroller.foo}&quot; converter=&quot;#{enumConverter}&quot;&gt;
	&lt;h:selectItems value=&quot;#{mycontroller.foos}&quot; var=&quot;element&quot;
	itemLabel=&quot;#{element}&quot; itemValue=&quot;#{element}&quot; /&gt;
&lt;/h:selectOneMenu&gt;
</pre>
</pre>

<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fgeneric-entity-converter-for-jsf-spring-jpa%2F&amp;title=Generic+Entity+Converter+for+JSF+%2F+Spring+%2F+JPA" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fgeneric-entity-converter-for-jsf-spring-jpa%2F&amp;title=Generic+Entity+Converter+for+JSF+%2F+Spring+%2F+JPA" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fgeneric-entity-converter-for-jsf-spring-jpa%2F&amp;title=Generic+Entity+Converter+for+JSF+%2F+Spring+%2F+JPA" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fgeneric-entity-converter-for-jsf-spring-jpa%2F&amp;title=Generic+Entity+Converter+for+JSF+%2F+Spring+%2F+JPA" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fgeneric-entity-converter-for-jsf-spring-jpa%2F&amp;title=Generic+Entity+Converter+for+JSF+%2F+Spring+%2F+JPA', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fgeneric-entity-converter-for-jsf-spring-jpa%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fgeneric-entity-converter-for-jsf-spring-jpa%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fgeneric-entity-converter-for-jsf-spring-jpa%2F&amp;title=Generic+Entity+Converter+for+JSF+%2F+Spring+%2F+JPA" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fgeneric-entity-converter-for-jsf-spring-jpa%2F&amp;title=Generic+Entity+Converter+for+JSF+%2F+Spring+%2F+JPA" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/jsf/generic-entity-converter-for-jsf-spring-jpa/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Custom taglibs with facelets</title>
		<link>http://www.reverttoconsole.com/blog/jsf/custom-taglibs-with-facelets/</link>
		<comments>http://www.reverttoconsole.com/blog/jsf/custom-taglibs-with-facelets/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 10:54:37 +0000</pubDate>
		<dc:creator>Priyatam</dc:creator>
				<category><![CDATA[JSF]]></category>

		<guid isPermaLink="false">http://reverttoconsole.com/?p=220</guid>
		<description><![CDATA[Creating tag libraries using facelets might be cool but needs more redundant infrastructure than the new JSP tag libs (which doesn&#8217;t need tld declaration or web.xml context params). However once your done, building reusable tags on the fly (with no java code) can be pretty slick. Here are the steps for creating a simple tag [...]]]></description>
			<content:encoded><![CDATA[<p>Creating tag libraries using facelets might be cool but needs more redundant infrastructure than the new JSP tag libs (which doesn&#8217;t need tld declaration or web.xml context params). However once your done, building reusable tags on the fly (with no java code) can be pretty slick.</p>
<p>Here are the steps for creating a simple tag &#8211; iconButton, which decorates a button with an icon image and other misc useful attributes.</p>
<p>taglib descriptor: your-taglib.xml (place it in WEB-INF)</p>
<pre>
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;!DOCTYPE facelet-taglib PUBLIC &quot;-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN&quot; &quot;facelet-taglib_1_0.dtd&quot;&gt;
&lt;facelet-taglib&gt;
    &lt;namespace&gt;http://yourUrlHere&lt;/namespace&gt;
    &lt;tag&gt;
       &lt;tag-name&gt;iconButton&lt;/tag-name&gt;
       &lt;source&gt;view/tags/iconButton.xhtml&lt;/source&gt;
     &lt;/tag&gt;
&lt;/facelet-taglib&gt;
</pre>
<p>iconButton.xhtml</p>
<pre>
&lt;ui:composition xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
    xmlns:ui=&quot;http://java.sun.com/jsf/facelets&quot;
    xmlns:h=&quot;http://java.sun.com/jsf/html&quot;
    xmlns:f=&quot;http://java.sun.com/jsf/core&quot;&gt;

&lt;button title=&quot;#{title}&quot; class=&quot;#{styleClass}&quot;
    onclick=&quot;#{empty onclick ? &#039;&#039; : onclick}&quot;&gt;
    &lt;img src=&quot;#{iconSrc}&quot; border=&quot;0&quot; align=&quot;top&quot;
    style=&quot;padding-top:1px;padding-bottom:1px;&quot;/&gt;
   #{text}
&lt;/button&gt;
&lt;/ui:composition&gt;
</pre>
<p>Usage (in other facelets)<br />
somepage.xhtml</p>
<pre>
&lt;ui:composition xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
    xmlns:ui=&quot;http://java.sun.com/jsf/facelets&quot;
    xmlns:h=&quot;http://java.sun.com/jsf/html&quot;
    xmlns:f=&quot;http://java.sun.com/jsf/core&quot;
    xmlns:myTags=&quot;http://yourUrlHere&quot;

    &lt;myTags:iconButton title=&quot; Search &quot;
                                styleClass=&quot;iconButton&quot; style=&quot;vertical-align:top;&quot;
                                iconSrc=&quot;./img/magnifying-glass.png&quot;
                                text=&quot;Search &quot;/&gt;
</pre>
<p>Note the inclusion of the namespace (&#8220;yoururl&#8221;) above.</p>
<p>web.xml</p>
<pre>
&lt;context-param&gt;
    &lt;param-name&gt;facelets.LIBRARIES&lt;/param-name&gt;
    &lt;param-value&gt;/WEB-INF/tomahawk-taglib.xml;/WEB-INF/my-taglib.xml&lt;/param-value&gt;
&lt;/context-param&gt;
</pre>
<p>You may get the following error: -</p>
<pre>Caused by: java.io.FileNotFoundException: &lt;jboss-home&gt;\bin\facelet-taglib_1_0.dtd</pre>
<p>That&#8217;s probably because you haven&#8217;t declared the taglib in WEB-INF, as shown above.</p>

<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fcustom-taglibs-with-facelets%2F&amp;title=Custom+taglibs+with+facelets" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fcustom-taglibs-with-facelets%2F&amp;title=Custom+taglibs+with+facelets" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fcustom-taglibs-with-facelets%2F&amp;title=Custom+taglibs+with+facelets" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fcustom-taglibs-with-facelets%2F&amp;title=Custom+taglibs+with+facelets" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fcustom-taglibs-with-facelets%2F&amp;title=Custom+taglibs+with+facelets', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fcustom-taglibs-with-facelets%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fcustom-taglibs-with-facelets%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fcustom-taglibs-with-facelets%2F&amp;title=Custom+taglibs+with+facelets" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fcustom-taglibs-with-facelets%2F&amp;title=Custom+taglibs+with+facelets" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/jsf/custom-taglibs-with-facelets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A simple java.util.Date to java.sql.Date converter</title>
		<link>http://www.reverttoconsole.com/blog/jsf/a-simple-javautildate-to-javasqldate-converter/</link>
		<comments>http://www.reverttoconsole.com/blog/jsf/a-simple-javautildate-to-javasqldate-converter/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 08:26:39 +0000</pubDate>
		<dc:creator>Priyatam</dc:creator>
				<category><![CDATA[JSF]]></category>

		<guid isPermaLink="false">http://reverttoconsole.com/?p=206</guid>
		<description><![CDATA[If you&#8217;re using JSF and need a converter for converting UI elements (typically java.util.Date) to your domain object&#8217;s date field (typically java.sql.Date), here&#8217;s the code snippet, public class DateConverter implements Converter, Serializable { public Object getAsObject(FacesContext context, UIComponent component, String value) { java.sql.Date sqlDate = null; Calendar cal = null; if (value != null &#038;&#038; [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re using JSF and need a converter for converting UI elements (typically java.util.Date) to  your domain object&#8217;s date field (typically java.sql.Date), here&#8217;s the code snippet,</p>
<pre>
public class DateConverter implements Converter, Serializable {
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
	    java.sql.Date sqlDate = null;
	    Calendar cal = null;

	    if (value != null &#038;&#038; value.length() > 0) {
	      DateFormat df = new SimpleDateFormat("dd-MMM-yy");

	      try {
	    	  Date utilDate = df.parse(value);
	          cal = new GregorianCalendar();
	          cal.setTime(utilDate);
	    	  sqlDate = new java.sql.Date(utilDate.getTime());
	      }
	      catch (ParseException e) {
	        throw new ConverterException(e);
	      }
	    }
	    return sqlDate;
	  }

	  public String getAsString(FacesContext context, UIComponent component, Object value) {
	    String dateString = null;
	    if (value != null) {
	      if (value instanceof String) {
	        dateString = (String) value;
	      }
	      else {
	        DateFormat df = new SimpleDateFormat("dd-MMM-yy");
	        if (value instanceof java.sql.Date) {
	        	Date date = new Date(((java.sql.Date)value).getTime());
	 	        dateString = df.format(date);
	        }
	      }
	    }
	     return dateString;
	  }
}
</pre>

<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fa-simple-javautildate-to-javasqldate-converter%2F&amp;title=A+simple+java.util.Date+to+java.sql.Date+converter" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fa-simple-javautildate-to-javasqldate-converter%2F&amp;title=A+simple+java.util.Date+to+java.sql.Date+converter" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fa-simple-javautildate-to-javasqldate-converter%2F&amp;title=A+simple+java.util.Date+to+java.sql.Date+converter" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fa-simple-javautildate-to-javasqldate-converter%2F&amp;title=A+simple+java.util.Date+to+java.sql.Date+converter" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fa-simple-javautildate-to-javasqldate-converter%2F&amp;title=A+simple+java.util.Date+to+java.sql.Date+converter', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fa-simple-javautildate-to-javasqldate-converter%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fa-simple-javautildate-to-javasqldate-converter%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fa-simple-javautildate-to-javasqldate-converter%2F&amp;title=A+simple+java.util.Date+to+java.sql.Date+converter" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjsf%2Fa-simple-javautildate-to-javasqldate-converter%2F&amp;title=A+simple+java.util.Date+to+java.sql.Date+converter" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/jsf/a-simple-javautildate-to-javasqldate-converter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

