<?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>Source Allies Blog &#187; JSF</title>
	<atom:link href="http://blogs.sourceallies.com/tag/jsf/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.sourceallies.com</link>
	<description>Technical and process thinking from Source Allies employees</description>
	<lastBuildDate>Fri, 03 Feb 2012 17:45:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Rendering Global t:messages After Redirect</title>
		<link>http://blogs.sourceallies.com/2010/03/rendering-global-tmessages-after-redirect/</link>
		<comments>http://blogs.sourceallies.com/2010/03/rendering-global-tmessages-after-redirect/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 19:24:46 +0000</pubDate>
		<dc:creator>Max  Kuipers</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Facelets]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[JSF messages]]></category>
		<category><![CDATA[tomahawk]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1219</guid>
		<description><![CDATA[A common problem when working with JSF is getting global info messages  via &#60;t:messages globalOnly="true"&#62; or &#60;f:messages globalOnly="true"&#62; to display messages set in the previous request when you have a &#60;redirect/&#62; in your faces-config for a particular page You will not see your &#60;t:messages&#62; that are set on the previous page.
The Problem
For instance, say you [...]]]></description>
			<content:encoded><![CDATA[<p>A common problem when working with JSF is getting global info messages  via <code>&lt;t:messages globalOnly="true"&gt;</code> or <code>&lt;f:messages globalOnly="true"&gt;</code> to display messages set in the previous request when you have a <code>&lt;redirect/&gt;</code> in your faces-config for a particular page You will not see your <code>&lt;t:messages&gt;</code> that are set on the previous page.</p>
<h4><span style="text-decoration: underline"><strong>The Problem</strong></span></h4>
<p>For instance, say you have two pages &#8211; page1.xhtml and page2.xhtml. In your faces-config.xml, you will have 2 entries.</p>
<p><span id="more-1219"></span></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;navigation-case<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;from-outcome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>page1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/from-outcome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;to-view-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/pages/page1.xhtml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/to-view-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;redirect</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/navigation-case<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;navigation-case<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;from-outcome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>page2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/from-outcome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;to-view-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/pages/page2.xhtml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/to-view-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;redirect</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/navigation-case<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Let&#8217;s say page1Bean is called in page1 and you want it to display messages in page2.  So your bean would have something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> page1Bean <span style="color: #009900;">&#123;</span>
	...
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> page1Action<span style="color: #009900;">&#123;</span>
		...
		<span style="color: #008000; font-style: italic; font-weight: bold;">/** Add message */</span>
		FacesMessage facesMessage <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FacesMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		...
		<span style="color: #006633;">FacesContext</span>.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, facesMessage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		...
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;page2&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Your page2.xhtml has like the following somewhere in it.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;t:messages globalOnly=&quot;true&quot;&gt;</pre></div></div>

<p>One would expect that this would display the message on the next page, however these messages are request scoped and so are not available in this second request and you do not see your message unless you remove the <code>&lt;redirect/&gt;</code> on page2.</p>
<p>Fortunately, there is a nifty and quick solution.  Basically, it is a phase listener that saves the messages from the previous request and then restores them just before the RENDER_RESPONSE phase of the second request.</p>
<h4><span style="text-decoration: underline"><strong>The Solution</strong></span></h4>
<p>To solve the problem, save the following class into some package where your faces-config will be able to access it</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.myproject.web.jsf.phaselistener</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Iterator</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.application.FacesMessage</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.context.FacesContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.event.PhaseEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.event.PhaseId</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.event.PhaseListener</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Enables messages to be rendered on different pages from which they were set.
 * To produce this behaviour, this class acts as a &lt;code&gt;PhaseListener&lt;/code&gt;.
 *
 * This is performed by moving the FacesMessage objects:
 *
	&lt;li&gt;After each phase where messages may be added, this moves the messages from
 * the page-scoped FacesContext to the session-scoped session map.
 *&lt;/li&gt;
	&lt;li&gt;Before messages are rendered, this moves the messages from the session-scoped
 * session map back to the page-scoped FacesContext.
 *&lt;/li&gt;
 * Only messages that are not associated with a particular component are ever
 * moved. These are the only messages that can be rendered on a page that is different
 * from where they originated.
 *
 * To enable this behaviour, add a &lt;code&gt;lifecycle&lt;/code&gt; block to your
 * faces-config.xml file. That block should contain a single &lt;code&gt;phase-listener&lt;/code&gt;
 * block containing the fully-qualified classname of this file.
 *
 * EDIT: This code was minimally modified by Max Kuipers to address some of the Java 1.6
 * compiler warnings.  All code was originally written by Jesse Wilson.
 *
 * @author &lt;a href=&quot;mailto:jesse@odel.on.ca&quot;&gt;Jesse Wilson&lt;/a&gt;
 * @author &lt;a href=&quot;mailto:mkuipers@sourceallies.com&quot;&gt;Max Kuipers&lt;/a&gt;
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MessageHandler <span style="color: #000000; font-weight: bold;">implements</span> PhaseListener <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * a name to save messages in the session under
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> sessionToken <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MULTI_PAGE_MESSAGES_SUPPORT&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Return the identifier of the request processing phase during which this
	 * listener is interested in processing PhaseEvent events.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> PhaseId getPhaseId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> PhaseId.<span style="color: #006633;">ANY_PHASE</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Handle a notification that the processing for a particular phase of the
	 * request processing lifecycle is about to begin.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> beforePhase<span style="color: #009900;">&#40;</span>PhaseEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getPhaseId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> PhaseId.<span style="color: #006633;">RENDER_RESPONSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			FacesContext facesContext <span style="color: #339933;">=</span> event.<span style="color: #006633;">getFacesContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			restoreMessages<span style="color: #009900;">&#40;</span>facesContext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Handle a notification that the processing for a particular phase has just
	 * been completed.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> afterPhase<span style="color: #009900;">&#40;</span>PhaseEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getPhaseId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> PhaseId.<span style="color: #006633;">APPLY_REQUEST_VALUES</span> <span style="color: #339933;">||</span>
				event.<span style="color: #006633;">getPhaseId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> PhaseId.<span style="color: #006633;">PROCESS_VALIDATIONS</span> <span style="color: #339933;">||</span>
				event.<span style="color: #006633;">getPhaseId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> PhaseId.<span style="color: #006633;">INVOKE_APPLICATION</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			FacesContext facesContext <span style="color: #339933;">=</span> event.<span style="color: #006633;">getFacesContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			saveMessages<span style="color: #009900;">&#40;</span>facesContext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Remove the messages that are not associated with any particular component
	 * from the faces context and store them to the user's session.
	 *
	 * @return the number of removed messages.
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> saveMessages<span style="color: #009900;">&#40;</span>FacesContext facesContext<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// remove messages from the context</span>
		<span style="color: #003399;">List</span> messages <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Iterator</span> i <span style="color: #339933;">=</span> facesContext.<span style="color: #006633;">getMessages</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			messages.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>i.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			i.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #666666; font-style: italic;">// store them in the session</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>messages.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #003399;">Map</span> sessionMap <span style="color: #339933;">=</span> facesContext.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSessionMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// if there already are messages</span>
		@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #003399;">List</span> existingMessages <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">List</span><span style="color: #009900;">&#41;</span> sessionMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>sessionToken<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>existingMessages <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			existingMessages.<span style="color: #006633;">addAll</span><span style="color: #009900;">&#40;</span>messages<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			sessionMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>sessionToken, messages<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// if these are the first messages</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> messages.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Remove the messages that are not associated with any particular component
	 * from the user's session and add them to the faces context.
	 *
	 * @return the number of removed messages.
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> restoreMessages<span style="color: #009900;">&#40;</span>FacesContext facesContext<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// remove messages from the session</span>
		<span style="color: #003399;">Map</span> sessionMap <span style="color: #339933;">=</span> facesContext.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSessionMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #003399;">List</span> messages <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">List</span><span style="color: #009900;">&#41;</span>sessionMap.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>sessionToken<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// store them in the context</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>messages <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">int</span> restoredCount <span style="color: #339933;">=</span> messages.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Iterator</span> i <span style="color: #339933;">=</span> messages.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			facesContext.<span style="color: #006633;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, i.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> restoredCount<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And then add something like the following in your faces-config.xml. Be sure to replace the example package location with your own.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lifecycle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phase-listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.myproject.web.jsf.phaselistener.MessageHandler<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phase-listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/lifecycle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The forum post that documented this solution is <a href="http://forums.sun.com/thread.jspa?forumID=427&amp;threadID=657727">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/03/rendering-global-tmessages-after-redirect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java EE 6 and Scala</title>
		<link>http://blogs.sourceallies.com/2010/02/java-ee-6-and-scala/</link>
		<comments>http://blogs.sourceallies.com/2010/02/java-ee-6-and-scala/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 17:36:31 +0000</pubDate>
		<dc:creator>Zach  Cox</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[EJB]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1057</guid>
		<description><![CDATA[Last weekend while pondering the question &#8220;Is Scala ready for the enterprise?&#8221; I decided to write a simple Java EE 6 app entirely in Scala, without using any Java. I had three main reasons for doing this: one was just to see how easy/difficult it would be to write everything in Scala (it was easy).  [...]]]></description>
			<content:encoded><![CDATA[<p>Last weekend while pondering the question &#8220;<a href="http://www.google.ca/search?q=is+scala+ready+for+the+enterprise%3F">Is Scala ready for the enterprise?</a>&#8221; I decided to write a simple <a href="http://java.sun.com/javaee/technologies/">Java EE 6</a> app entirely in <a href="http://www.scala-lang.org">Scala</a>, without using any Java. I had three main reasons for doing this: one was just to see how easy/difficult it would be to write everything in Scala (it was easy).  Another was to document the process for others journeying down the same road (<a href="http://github.com/zcox/javaee6-books-scala">the entire project is on github</a>).  Finally, I wanted to identify advantages of using Scala instead of Java that are specific to Java EE apps (I found several).<br />
<span id="more-1057"></span></p>
<h2>Background</h2>
<p>The specific app I created was an adaptation of the Books example from Chapter 10 of <a href="http://apress.com/book/view/1430219548">Beginning Java™ EE 6 Platform with GlassFish™ 3</a>. It&#8217;s a simple web app that displays a list of books in a database and lets you add new books. Although it&#8217;s a pretty trivial app, it does touch on several important Java EE 6 technologies: JPA 2.0, EJB 3.1 and JSF 2.0.</p>
<h2>Results</h2>
<p>As a baseline, I first created the example from the Book using Java (<a href="http://github.com/zcox/javaee6-books">and put it on github</a>). The three Java classes are mostly identical to the book: <a href="http://github.com/zcox/javaee6-books/blob/master/src/main/java/com/sourceallies/model/Book.java">Book.java</a>, <a href="http://github.com/zcox/javaee6-books/blob/master/src/main/java/com/sourceallies/ejb/BookEjb.java">BookEjb.java</a> and <a href="http://github.com/zcox/javaee6-books/blob/master/src/main/java/com/sourceallies/controller/BookController.java">BookController.java</a>. Aside from those, I refactored common elements of the two JSF views into a <a href="http://github.com/zcox/javaee6-books/blob/master/src/main/webapp/template.xhtml">template</a>, included the <a href="http://blueprintcss.org/">Blueprint CSS</a> framework and created a better Maven <a href="http://github.com/zcox/javaee6-books/blob/master/pom.xml">pom.xml</a>.</p>
<p>Next I rewrote the Java parts in Scala (<a href="http://github.com/zcox/javaee6-books-scala">and put it on github</a>). Initially I did a one-to-one mapping of the Java classes to their Scala counterparts. Once that was complete I did a bit of refactoring to Book.scala and BookEjb.scala, so the versions you&#8217;ll see on github now look a bit different.  During this process I did use some hints from a few other <a href="http://vikasrao.wordpress.com/2009/02/03/scala-with-jpa/">related</a> <a href="http://www.hars.de/2009/03/jpa-with-scala.html">blog</a> <a href="http://vikasrao.wordpress.com/2009/06/">posts</a>.</p>
<h3>JPA</h3>
<p>First up, here&#8217;s Book.java:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.model</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.*</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399;">Entity</span>
@NamedQuery<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #003399;">Book</span>.<span style="color: #006633;">FindAllBooks</span>, query <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT b FROM Book b&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Book</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> FindAllBooks <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;findAllBooks&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	@Id
	@GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">IDENTITY</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id<span style="color: #339933;">;</span>
&nbsp;
	@Column<span style="color: #009900;">&#40;</span>nullable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> title<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Float</span> price<span style="color: #339933;">;</span>
&nbsp;
	@Column<span style="color: #009900;">&#40;</span>length <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2000</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> description<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> isbn<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> nbOfPage<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Boolean</span> illustrations<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Long</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getTitle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> title<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTitle<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> title<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">title</span> <span style="color: #339933;">=</span> title<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Float</span> getPrice<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> price<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setPrice<span style="color: #009900;">&#40;</span><span style="color: #003399;">Float</span> price<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">price</span> <span style="color: #339933;">=</span> price<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getDescription<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> description<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDescription<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> description<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">description</span> <span style="color: #339933;">=</span> description<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getIsbn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> isbn<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setIsbn<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> isbn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">isbn</span> <span style="color: #339933;">=</span> isbn<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Integer</span> getNbOfPage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> nbOfPage<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setNbOfPage<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> nbOfPage<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nbOfPage</span> <span style="color: #339933;">=</span> nbOfPage<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Boolean</span> getIllustrations<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> illustrations<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setIllustrations<span style="color: #009900;">&#40;</span><span style="color: #003399;">Boolean</span> illustrations<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">illustrations</span> <span style="color: #339933;">=</span> illustrations<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> getFindallbooks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> FindAllBooks<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The Java version has 58 lines of completely useless getters &amp; setters.  You need at least 9 lines per field and more for additional annotations.  IDEs like Eclipse can generate these getters/setters for you, but they still take up space and must be maintained over time.</p>
<p>Now let&#8217;s look at the Book entity in Scala:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">sourceallies</span>.<span style="color: #000000;">model</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> javax.<span style="color: #000000;">persistence</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">reflect</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #000080;">@</span>Entity
<span style="color: #000080;">@</span>NamedQuery<span style="color: #F78811;">&#123;</span><span style="color: #0000ff; font-weight: bold;">val</span> name <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;findAllBook&quot;</span>, <span style="color: #0000ff; font-weight: bold;">val</span> query <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;SELECT b FROM Book b&quot;</span><span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">class</span> Book <span style="color: #0000ff; font-weight: bold;">extends</span> Id <span style="color: #0000ff; font-weight: bold;">with</span> Description <span style="color: #F78811;">&#123;</span>
  <span style="color: #000080;">@</span>Column<span style="color: #F78811;">&#123;</span><span style="color: #0000ff; font-weight: bold;">val</span> nullable <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">false</span><span style="color: #F78811;">&#125;</span>
  <span style="color: #000080;">@</span>BeanProperty
  <span style="color: #0000ff; font-weight: bold;">var</span> title<span style="color: #000080;">:</span> String <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
&nbsp;
  <span style="color: #000080;">@</span>BeanProperty
  <span style="color: #0000ff; font-weight: bold;">var</span> price<span style="color: #000080;">:</span> Float <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
&nbsp;
  <span style="color: #000080;">@</span>BeanProperty
  <span style="color: #0000ff; font-weight: bold;">var</span> isbn<span style="color: #000080;">:</span> String <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
&nbsp;
  <span style="color: #000080;">@</span>BeanProperty
  <span style="color: #0000ff; font-weight: bold;">var</span> nbOfPage<span style="color: #000080;">:</span> Int <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
&nbsp;
  <span style="color: #000080;">@</span>BeanProperty
  <span style="color: #0000ff; font-weight: bold;">var</span> illustrations<span style="color: #000080;">:</span> Boolean <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #00ff00; font-style: italic;">/** Defines an id field for entities to use as a primary key. */</span>
<span style="color: #0000ff; font-weight: bold;">trait</span> Id <span style="color: #F78811;">&#123;</span>
  <span style="color: #000080;">@</span>javax.<span style="color: #000000;">persistence</span>.<span style="color: #000000;">Id</span>
  <span style="color: #000080;">@</span>GeneratedValue<span style="color: #F78811;">&#123;</span><span style="color: #0000ff; font-weight: bold;">val</span> strategy <span style="color: #000080;">=</span> GenerationType.<span style="color: #000000;">IDENTITY</span><span style="color: #F78811;">&#125;</span>
  <span style="color: #000080;">@</span>BeanProperty
  <span style="color: #0000ff; font-weight: bold;">var</span> id<span style="color: #000080;">:</span> Long <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #00ff00; font-style: italic;">/** Defines a description field for entities. */</span>
<span style="color: #0000ff; font-weight: bold;">trait</span> Description <span style="color: #F78811;">&#123;</span>
  <span style="color: #000080;">@</span>Column<span style="color: #F78811;">&#123;</span><span style="color: #0000ff; font-weight: bold;">val</span> length <span style="color: #000080;">=</span> <span style="color: #F78811;">2000</span><span style="color: #F78811;">&#125;</span>
  <span style="color: #000080;">@</span>BeanProperty
  <span style="color: #0000ff; font-weight: bold;">var</span> description<span style="color: #000080;">:</span> String <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>In the code base these are actually three separate files: <a href="http://github.com/zcox/javaee6-books-scala/blob/master/src/main/scala/com/sourceallies/model/Book.scala">Book.scala</a>, <a href="http://github.com/zcox/javaee6-books-scala/blob/master/src/main/scala/com/sourceallies/model/Id.scala">Id.scala</a> and <a href="http://github.com/zcox/javaee6-books-scala/blob/master/src/main/scala/com/sourceallies/model/Description.scala">Description.scala</a>, I just included them all in one place here for convenience.</p>
<p>The Scala version needs only two lines per field: one for the @BeanProperty annotation to generate getters/setters in the compiled class and one to declare the field itself.  This is seven lines less than the Java version <em>per field</em>.</p>
<p>Fields are declared as vars so they are public by default, mutable and every var that is a non-private member of some object implicitly defines a getter and a setter method with it.  So &#8220;var x&#8221; defines a getter just named &#8220;x&#8221; and a setter named &#8220;x_=&#8221;.  This lets you get the field using object.x and set it using object.x = y.</p>
<p>If JPA supported Scala-style accessors &amp; mutators, we&#8217;d be done, but it requires the use of get/set methods.  This is why we use <a href="http://www.scala-lang.org/docu/files/api/scala/reflect/BeanProperty.html">@BeanProperty</a> on each field.  This instructs the Scala compiler to generate Java-style get/set methods in the compiled class file so it looks just like a Java entity to JPA.</p>
<p>You may also be wondering why each field is assigned a value of _ in its declaration.  This is just the default value in Scala, just like omitting an initializer in Java: 0 for numeric types, false for booleans, and null for reference types.</p>
<p>The annotations also look a bit different; in Java they&#8217;re @Column(nullable = false) but in Scala are @Column{val nullable = false}.  This is just how you do it in Scala.</p>
<p>I also pulled the id and description fields out into their own traits. This makes the entity class shorter and makes common fields reusable and more consistent across entities.  I could have pulled all fields out into their own traits, but in practice there&#8217;s probably a limit to how much you&#8217;d want to do this, some fields will be specific to a single entity class and not reusable.</p>
<h3>EJB</h3>
<p>Now let&#8217;s look at BookEjb.java:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.ejb</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ejb.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sourceallies.model.*</span><span style="color: #339933;">;</span>
&nbsp;
@Stateless
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BookEjb <span style="color: #009900;">&#123;</span>
	@PersistenceContext<span style="color: #009900;">&#40;</span>unitName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;book&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> EntityManager manager<span style="color: #339933;">;</span>
&nbsp;
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Book<span style="color: #339933;">&gt;</span> findBooks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> manager.<span style="color: #006633;">createNamedQuery</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Book</span>.<span style="color: #006633;">FindAllBooks</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResultList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Book</span> createBook<span style="color: #009900;">&#40;</span><span style="color: #003399;">Book</span> book<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		manager.<span style="color: #006633;">persist</span><span style="color: #009900;">&#40;</span>book<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> book<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The Java version is very simple: it basically just provides two Book-specific CRUD methods using a JPA EntityManager.</p>
<p>And here is the Scala version:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">sourceallies</span>.<span style="color: #000000;">ejb</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> javax.<span style="color: #000000;">ejb</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> javax.<span style="color: #000000;">persistence</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> com.<span style="color: #000000;">sourceallies</span>.<span style="color: #000000;">model</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #000080;">@</span>Stateless
<span style="color: #000080;">@</span>LocalBean
<span style="color: #0000ff; font-weight: bold;">class</span> BookEjb <span style="color: #0000ff; font-weight: bold;">extends</span> CrudEjb<span style="color: #F78811;">&#91;</span>Book<span style="color: #F78811;">&#93;</span>
&nbsp;
<span style="color: #00ff00; font-style: italic;">/** Provides basic CRUD support using an injected JPA entity manager. */</span>
<span style="color: #0000ff; font-weight: bold;">trait</span> CrudEjb<span style="color: #F78811;">&#91;</span>E<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #000080;">@</span>PersistenceContext
  <span style="color: #0000ff; font-weight: bold;">protected</span> <span style="color: #0000ff; font-weight: bold;">var</span> manager<span style="color: #000080;">:</span> EntityManager <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> create<span style="color: #F78811;">&#40;</span>entity<span style="color: #000080;">:</span> E<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> E <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    manager persist entity
    entity
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> readAll<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">implicit</span> m<span style="color: #000080;">:</span> Manifest<span style="color: #F78811;">&#91;</span>E<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> manager createNamedQuery <span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;findAll&quot;</span> + m.<span style="color: #000000;">erasure</span>.<span style="color: #000000;">getSimpleName</span><span style="color: #F78811;">&#41;</span> getResultList
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> read<span style="color: #F78811;">&#40;</span>id<span style="color: #000080;">:</span> Long<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">implicit</span> manifest<span style="color: #000080;">:</span> Manifest<span style="color: #F78811;">&#91;</span>E<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> E <span style="color: #000080;">=</span> manager.<span style="color: #000000;">find</span><span style="color: #F78811;">&#40;</span>manifest.<span style="color: #000000;">erasure</span>, id<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">asInstanceOf</span><span style="color: #F78811;">&#91;</span>E<span style="color: #F78811;">&#93;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> update<span style="color: #F78811;">&#40;</span>entity<span style="color: #000080;">:</span> E<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> E <span style="color: #000080;">=</span> manager merge entity
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> delete<span style="color: #F78811;">&#40;</span>entity<span style="color: #000080;">:</span> E<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span> manager remove entity <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Again, these are two separate files in the actual code base: <a href="http://github.com/zcox/javaee6-books-scala/blob/master/src/main/scala/com/sourceallies/ejb/BookEjb.scala">BookEjb.scala</a> and <a href="http://github.com/zcox/javaee6-books-scala/blob/master/src/main/scala/com/sourceallies/ejb/CrudEjb.scala">CrudEjb.scala</a>.</p>
<p>In the Scala version I created the CrudEjb trait that provides full CRUD support for any JPA entity, so the concrete EJB can be defined simply as BookEjb extends CrudEjb[Book].  In several places we need the Class object of the entity, which is difficult to obtain due to Java&#8217;s erasure of generic types at runtime.  Scala&#8217;s <a href="http://daily-scala.blogspot.com/2009/11/manifests-s.html">Manifests</a> are a great solution to this problem.</p>
<p>The Scala version requires the @LocalBean annotation while the Java version does not (not sure why).  Also the EntityManager is defined as a var without the Java-style getters and setters.  The container is able to inject the EntityManager using the Scala-style mutator (bean.manager = x) but it does log an error about it not having a set method.</p>
<h3>JSF</h3>
<p>And now the BookController.java:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.controller</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ejb.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.bean.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sourceallies.ejb.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sourceallies.model.*</span><span style="color: #339933;">;</span>
&nbsp;
@ManagedBean
@RequestScoped
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BookController <span style="color: #009900;">&#123;</span>
	@EJB
	<span style="color: #000000; font-weight: bold;">private</span> BookEjb ejb<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Book</span> book<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Book<span style="color: #339933;">&gt;</span> bookList<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Book</span> getBook<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>book <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
			book <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Book</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> book<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setBook<span style="color: #009900;">&#40;</span><span style="color: #003399;">Book</span> book<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">book</span> <span style="color: #339933;">=</span> book<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Book<span style="color: #339933;">&gt;</span> getBookList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>bookList <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
			bookList <span style="color: #339933;">=</span> ejb.<span style="color: #006633;">findBooks</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> bookList<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setBookList<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>Book<span style="color: #339933;">&gt;</span> bookList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">bookList</span> <span style="color: #339933;">=</span> bookList<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> doNew<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;newBook.xhtml&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> doCreateBook<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		book <span style="color: #339933;">=</span> ejb.<span style="color: #006633;">createBook</span><span style="color: #009900;">&#40;</span>book<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		bookList <span style="color: #339933;">=</span> ejb.<span style="color: #006633;">findBooks</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;listBooks.xhtml&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This Java version is simply a Book-specific CRUD wrapper around BookEjb, and provides Book and List&lt;Book&gt; objects to the JSF views.</p>
<p>And finally, BookController.scala:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">sourceallies</span>.<span style="color: #000000;">controller</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> javax.<span style="color: #000000;">ejb</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> javax.<span style="color: #000000;">faces</span>.<span style="color: #000000;">bean</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> com.<span style="color: #000000;">sourceallies</span>.<span style="color: #000000;">model</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> com.<span style="color: #000000;">sourceallies</span>.<span style="color: #000000;">ejb</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #000080;">@</span>ManagedBean
<span style="color: #000080;">@</span>RequestScoped
<span style="color: #0000ff; font-weight: bold;">class</span> BookController <span style="color: #F78811;">&#123;</span>
  <span style="color: #000080;">@</span>EJB
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">var</span> ejb<span style="color: #000080;">:</span> BookEjb <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">val</span> book <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Book
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> getBook <span style="color: #000080;">=</span> book
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> getBookList <span style="color: #000080;">=</span> ejb.<span style="color: #000000;">readAll</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> doNew<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;newBook.xhtml&quot;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> doCreateBook<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    ejb create book
    <span style="color: #6666FF;">&quot;listBooks.xhtml&quot;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>The Scala version is basically just the Java version converted to Scala, no major changes, just looks a bit cleaner due to Scala&#8217;s concise syntax.  As with the EJB, we get an error logged about not having a set method for injecting BookEjb, but the container does actually inject an instance properly.</p>
<h2>Conclusion</h2>
<p>Even though this was a simple example, I think it shows a few advantages of using Scala instead of Java in Java EE apps.  You can forget about useless getters/setters in JPA entity classes, use traits to reuse common JPA entity field definitions and mixin common behavior in EJBs (example: BookEjb could also mixin Logging trait and other traits).  And advanced Scala features like Manifests help you write better code faster.</p>
<p>This example only scratched the surface of what Scala can do, and didn&#8217;t even use things like closures, case classes, pattern matching or Scala&#8217;s functional programming capabilities (using Scala collections of JPA entities would help a lot).</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/02/java-ee-6-and-scala/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Keep your dataTable clean with a custom popup</title>
		<link>http://blogs.sourceallies.com/2010/01/keep-your-datatable-clean-with-a-custom-popup/</link>
		<comments>http://blogs.sourceallies.com/2010/01/keep-your-datatable-clean-with-a-custom-popup/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 14:05:57 +0000</pubDate>
		<dc:creator>David  Casleton</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[tomahawk]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=864</guid>
		<description><![CDATA[The basic idea is to output some data to a user in a table and allow them to take an action on each row individually.  A fairly straightforward solution is to create a separate page to link to, passing the necessary row information along.  If the action is simple enough, like a single [...]]]></description>
			<content:encoded><![CDATA[<p>The basic idea is to output some data to a user in a table and allow them to take an action on each row individually.  A fairly straightforward solution is to create a separate page to link to, passing the necessary row information along.  If the action is simple enough, like a single checkbox, you could just embed the necessary component(s) in each row of the table.  Too many components, however, can bloat the table and make the UI cumbersome to the user.  Instead we can create a popup window to overlay our page, containing whatever components are needed, and activate it by a link embedded in our table.  Passing the row information is a little trickier,  but the result is a cleaner interface and a better user experience.<br />
<span id="more-864"></span></p>
<p>The example implementation below is done using Java with JSF, a little JavaScript, a little jQuery, and some CSS.  The tag libraries being referenced are:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">xmlns:h=&quot;http://java.sun.com/jsf/html&quot;
xmlns:f=&quot;http://java.sun.com/jsf/core&quot;
xmlns:t=&quot;http://myfaces.apache.org/tomahawk&quot;</pre></div></div>

<p>The page is constructed using two separate forms, one for the main components of the page, including the datatable, and the other for the popup.  A little JavaScript and jQuery are used to show/hide the popup form and pass around the pertinent row information.  Some CSS is used to shadow the main page components while the popup is active.</p>
<p>Our simple main form is:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:form</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;someForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;actionDetailsDiv&quot;</span> <span style="color: #000066;">styleClass</span>=<span style="color: #ff0000;">&quot;info&quot;</span> </span>
<span style="color: #009900;">			<span style="color: #000066;">rendered</span>=<span style="color: #ff0000;">&quot;#{not empty myAction.actionDetails}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:outputText</span> <span style="color: #000066;">escape</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{myAction.actionDetails}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/t:div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:dataTable</span> <span style="color: #000066;">var</span>=<span style="color: #ff0000;">&quot;currentRow&quot;</span> </span>
<span style="color: #009900;">			<span style="color: #000066;">rowIndexVar</span>=<span style="color: #ff0000;">&quot;rowNum&quot;</span> </span>
<span style="color: #009900;">			<span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{myAction.myData}&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;f:facet</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;header&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:outputText</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;row&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/f:facet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			#{rowNum}
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/t:column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;f:facet</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;header&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:outputText</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;some colummn&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/f:facet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			#{currentRow.someProperty}
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/t:column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;f:facet</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;header&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:outputText</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;action links&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/f:facet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript:showHiddenForm('#{rowNum}')&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>action link<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/t:column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/t:dataTable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>	
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Here the row number (rowNum) has been passed to a piece of JS, but you could just as easily pass a property from the current row object.  Also, actionDetails is a string on our backing action bean used to inform the user that the action has been taken on the appropriate data.  This should be declared in the backing bean as an empty string and thus the containing div is not initially rendered.</p>
<p>Our hidden form is:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:form</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;hiddenForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;backgroundDiv&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;fullscreen outer&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;foregroundDiv&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;centerPopup inner&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Action Heading<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:inputHidden</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;hiddenInfo&quot;</span> <span style="color: #000066;">forceId</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:commandButton</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Backing Action&quot;</span> <span style="color: #000066;">actionListener</span>=<span style="color: #ff0000;">&quot;#{myAction.backingAction}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>		
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:commandButton</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;hideHiddenForm()&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Cancel&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>To prove that the data in the hidden field is being populated correctly, change the inputHidden component to inputText.</p>
<p>The CSS referenced above is:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">div<span style="color: #6666ff;">.fullscreen</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
div<span style="color: #6666ff;">.centerPopup</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">fixed</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">50</span>%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">50</span>%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">500px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-100px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-250px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
div<span style="color: #6666ff;">.outer</span> <span style="color: #00AA00;">&#123;</span>
	filter<span style="color: #00AA00;">:</span> alpha<span style="color: #00AA00;">&#40;</span>opacity<span style="color: #00AA00;">=</span><span style="color: #cc66cc;">50</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
	-moz-opacity<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0.5</span><span style="color: #00AA00;">;</span>
	opacity<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0.5</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#D8D8D8</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">2000</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
div<span style="color: #6666ff;">.inner</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#000000</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">2001</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
p<span style="color: #6666ff;">.inner</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">white</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
div<span style="color: #6666ff;">.info</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">bold</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Most of the CSS is pretty straightforward.  The &#8220;outer&#8221; class is setup to make a div gray and transparent in most browsers and &#8220;centerPopup&#8221; creates a statically sized div in the middle of the screen.  The crucial part of the CSS is the z-index used by the &#8220;inner&#8221; and &#8220;outer&#8221; classes.  The higher the z-index, the higher on the stack the element is, thus to show the &#8220;inner&#8221; popup over the somewhat transparent &#8220;outer&#8221; fullscreen overlay, the popup&#8217;s z-index must be higher.  Both z-indexes are set arbitrarily high to ensure other elements don&#8217;t interfere.</p>
<p>The JavaScript/jQuery to show and hide the hidden form is:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> hideHiddenForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#hiddenForm&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> showHiddenForm<span style="color: #009900;">&#40;</span>selection<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">//set the value of the hidden field</span>
	<span style="color: #003366; font-weight: bold;">var</span> thingy <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;hiddenInfo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	thingy.<span style="color: #660066;">value</span><span style="color: #339933;">=</span>selection<span style="color: #339933;">;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#hiddenForm&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">//initially hide the popup form</span>
	hideHiddenForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note showHiddenForm takes a single input and sets this as the value of the hiddenInfo element.  Also, we need to hide the popup form when the page is first loaded.</p>
<p>In our backing bean (MyAction) the backingAction method needs to find the hidden field and do something with the value.  Here the value is passed to the actionDetails property forcing actionDetailsDiv above the table to render when the page is rerendered.  Typically we would actually act on the value instead of merely redisplaying it.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> backingAction<span style="color: #009900;">&#40;</span><span style="color: #003399;">ActionEvent</span> event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	UIComponent link <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>UIComponent<span style="color: #009900;">&#41;</span> event.<span style="color: #006633;">getSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>UIComponent divChild <span style="color: #339933;">:</span> link.<span style="color: #006633;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>divChild.<span style="color: #006633;">getAttributes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hiddenInfo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">String</span> rowNum <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> divChild.<span style="color: #006633;">getAttributes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			setActionDetails<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Action taken on row &quot;</span> <span style="color: #339933;">+</span> rowNum<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The hidden form (popup) can become as complicated as you need and allows your main form to remain clean.  If anyone knows an easier way to pull the value out of the hidden element, let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/01/keep-your-datatable-clean-with-a-custom-popup/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

