<?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>J-Development</title>
	<atom:link href="http://jdevelopment.nl/feed/" rel="self" type="application/rss+xml" />
	<link>http://jdevelopment.nl</link>
	<description>designing a new generation of software</description>
	<lastBuildDate>Thu, 02 Feb 2012 23:03:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>What&#8217;s new in JSF 2.2?</title>
		<link>http://jdevelopment.nl/jsf-22/</link>
		<comments>http://jdevelopment.nl/jsf-22/#comments</comments>
		<pubDate>Sun, 22 Jan 2012 10:00:27 +0000</pubDate>
		<dc:creator>arjan tijms</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jsf]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=1078</guid>
		<description><![CDATA[JSF 2.2 is currently in its early stages of development, and originally had an anticipated release of the final draft of the spec in Q4 2011 (see Jsf 2 2-bof, slide 3 and JSR 344: JavaServer Faces 2.2). This date slipped, but we might still see the actual release somewhere in the first half of [...]]]></description>
			<content:encoded><![CDATA[<p>JSF 2.2 is currently in its early stages of development, and originally had an anticipated release of the final draft of the spec in Q4 2011 (see <a href="http://www.slideshare.net/edburns/jsf-2-2bof">Jsf 2 2-bof, slide 3</a> and <a href="http://jcp.org/en/jsr/detail?id=344">JSR 344: JavaServer Faces 2.2</a>). This date slipped, but we might still see the actual release somewhere in the <a href="http://weblogs.java.net/blog/edburns/archive/2011/11/11/jsf-22-early-draft-review-available" target="_blank">first half of 2012</a>.</p>
<p>So what&#8217;s going to be in 2.2? The JSR gives an overview of ideas, but explicitly makes the reservation that not all of those will be in the final spec. So, another source worth looking at is the actual <a href="https://svn.java.net/svn/mojarra~svn/trunk/">trunk</a> of the reference implementation and its associated <a href="http://java.net/jira/browse/JAVASERVERFACES">implementation JIRA</a> as well as that of the <a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC">public specification JIRA</a>.</p>
<p>In this post I&#8217;ll try to highlight some items from that activity that I found interesting. Note that all of this is preliminary and just reflects the activity in the trunk, not what is absolutely promised to be in the final JSF 2.2 specification. Of course, if there&#8217;s a lot of activity and commits for some item, there&#8217;s a rather high chance that it will indeed end up in the spec.<br />
<br/></p>
<h3> Contents </h3>
<ul>
<li> <a href="#get">GET Support </a>
<ul>
<li><a href="#758">View Actions</a></li>
</ul>
</li>
<li> <a href="#ajax">AJAX</a>
<ul>
<li><a href="#1050">Queue control for AJAX requests</a></li>
<li><a href="#802">AJAX file upload component</a></li>
</ul>
</li>
<li> <a href="#injection">Injection / Annotations </a>
<ul>
<li><a href="#763">Injection in all JSF artifacts</a></li>
<li><a href="#1038">Facelets ResourceResolver can be declared via annotation</a></li>
</ul>
</li>
<li> <a href="#injection">Security / Type-safety </a>
<ul>
<li><a href="#869">Cross Site Request Forgery protection</a></li>
<li><a href="#745">More thorough type checking for composite component attributes</a></li>
</ul>
</li>
<li> <a href="#java">Java API</a>
<ul>
<li><a href="#611">FaceletFactory in the standard API</a></li>
<li><a href="#599">Instantiating composite component in Java</a></li>
<li><a href="#479">Support for the Collection interface in UIData</a></li>
</ul>
</li>
<li> <a href="#lifecycle">Lifecycle</a>
<ul>
<li><a href="#787">Restoring view scope before view is build</a></li>
<li><a href="#766">System events for The Flash</a></li>
<li><a href="#1061">Publish PostRestoreStateEvent</a></li>
<li><a href="#1028">Mandate tree visiting for partial state saving</a></li>
</ul>
</li>
<li> <a href="#components">Components</a>
<ul>
<li><a href="#984">Component modification management</a></li>
</ul>
</li>
<li> <a href="#xml">XML configuration </a>
<ul>
<li><a href="#1010">Case insensitivity for state saving method</a></li>
</ul>
</li>
<li> <a href="#standards">Standards compliance</a>
<ul>
<li><a href="#220">Using only name attribute for view state field</a></li>
</ul>
</li>
</ul>
<p><a name="get"></a></p>
<h3> GET Support </h3>
<p><a name="758"></a><br />
<strong>View Actions</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-758">spec issue 758</a>) <small>(mentioned in JSR)</small></p>
<p>JSF 2.0 made GET requests a first class citizen in JSF and brought it almost on par with the existing POST support. Just as after a post-back, request values from a GET request can be bound to a model and converters and validators can be applied to that binding.</p>
<p>However, JSF 2.0 didn&#8217;t specify an associated action method. A <em>preRenderView</em> event listener can be used instead and that actually does work for a lot of use-cases, but it&#8217;s not at the same level as normal action methods that are invoked after a POST request. Users have to manually interact with the <em>NavigationHandler</em> and since the listener is invoked before every rendering, users have to programmatically check if the request was a GET request or a post-back.</p>
<p>View actions are going to allow the same kind of action methods that are used for post-backs to be useable via a new metadata element called <em>&lt;f:viewAction&gt;</em>. <a href="http://docs.jboss.org/seam/3/faces/reference/snapshot/en-US/html_single/#viewaction">The Seam 3 component</a> with the same name was an important inspiration for this.</p>
<p>An additional advantage of a view action is that it can be processed before the entire component tree is being build. Only the meta data needs to be present, which is normally build separately from the rest of the tree.</p>
<p>The feature is discussed in more detail together with sample code at: <a href="http://www.oracle.com/technetwork/articles/java/jsf22-1377252.html">New JavaServer Faces 2.2 Feature: The viewAction Component</a></p>
<p>A very simple example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;f:metadata<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>    
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;f:viewAction</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;#{someBean.someAction}&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:metadata<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Commits for this feature have been done between 16/jun/11 and 18/aug/11.<br />
<br/></p>
<p><a name="ajax"></a></p>
<h3> AJAX </h3>
<p><a name="1050"></a><br />
<strong>Queue control for AJAX requests</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1050">spec issue 1050</a>)</p>
<p>The standard AJAX support in JSF causes AJAX requests to be queued client-side to make sure a page has only 1 such request in transit at the same time.</p>
<p>This is normally beneficial, as it prevents the server from being overloaded and responses arriving out of order. Typical features here are specifying the maximum size of the queue or the amount of time a request is allowed to sit waiting in the queue.</p>
<p>However, JSF lacked those features. Werner Punz wrote about this recently: <a href="http://werpublogs.blogspot.com/2011/07/apache-myfaces-jsfjs-queue-control.html" target="_blank">Apache MyFaces jsf.js queue control</a>.</p>
<p>JSF will add support for controlling the queue by means of a <em>delay</em> attribute on the <code>&lt;f:ajax></code> tag. This attribute takes a value in milliseconds and signals that if multiple requests arrive within this delay period, only the most recent one is sent to the server. The default is 300 milliseconds, but the special value of <em>none</em> can be specified to disable this mechanism.</p>
<p>Commits for this feature have been done on 10/jun/11 and the associated issue has been marked as resolved.<br />
<br/></p>
<p><a name="802"></a><br />
<strong>AJAX file upload component</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-802">spec issue 802</a>) <small>(mentioned in JSR)</small></p>
<p>Having a (AJAX) file upload component in JSF goes back a long time. There is much existing discussion on this issue, and many component libraries have in the mean time implemented something themselves.</p>
<p>An AJAX variant is even more difficult, not in the least since it&#8217;s not directly supported in HTML. There&#8217;s an iframe based trick available, but this by itself does not work directly with JSF. To support this, changes are required to the mechanism that triggers a JSF request/response cycle.</p>
<p>A prerequisite to an AJAX file upload mechanism is having a standard file upload in JSF. For this a separate issue was created: <a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1062" target="_blank">Non-Ajax File Upload</a>. Interesting here is that this implementation is based on <a href="http://blog.caucho.com/2009/10/22/servlet-30-tutorial-uploading-files" title="Servlet 3.0 Tutorial: Uploading files" target="_blank">Servlet 3.0 multipart support</a>, like e.g. <a href="http://balusc.blogspot.com/2009/12/uploading-files-with-jsf-20-and-servlet.html" title="Uploading files with JSF 2.0 and Servlet 3.0" target="_blank">this implementation</a> by my co-worker Bauke. This means among other that the venerable <em>FacesServlet</em> is now annotated with the <em>@MultipartConfig</em> annotation and that JSF now requires Servlet 3.0.</p>
<p>The standard file upload component can be used on a view as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td 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;">prependId</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">enctype</span>=<span style="color: #ff0000;">&quot;multipart/form-data&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- The new regular file upload component --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:inputFile</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;fileUpload&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{someBean.file}&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;h:commandButton</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Upload&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</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></td></tr></table></div>

<p>Commits for the sub-issue have been done on 15/dec/11.</p>
<p><br/></p>
<p><a name="injection"></a></p>
<h3> Injection / Annotations </h3>
<p><a name="763"></a><br />
<strong>Injection in all JSF artifacts</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-763">spec issue 763</a>) <small>(mentioned in JSR)</small></p>
<p>In JSF 2.1, relatively few JSF artifacts are injection targets for EJB (@EJB, @Resource), CDI (@Inject) or JSF&#8217;s native injection mechanism. Basically only managed beans (backing beans) are injection targets.</p>
<p>This poses a problem for e.g. converters and validators, which not rarely need an EJB service to do their work. For instance, an incoming user ID from a GET request might needs to be converted to a User instance via the Stateless EJB <em>UserService</em>. There are some workarounds for this as outlined in <a href="http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#GettingAnEJBInFacesConverterAndFacesValidator">this excellent guide</a> from my co-worker Bauke, but they ain&#8217;t pretty.</p>
<p>In JSF 2.2 injection will be possible in converters, validators, components, behaviors and much more artifacts.</p>
<p>Commits for this feature have been done between 27/aug/11 and 31/oct/11 and the associated issue had been marked as resolved, but on 11/jan/12 an additional commit was done that changed how the <em>InjectionProvider</em> is internally obtained by the <em>FactoryFinder</em>.<br />
<br/></p>
<p><a name="1038"></a><br />
<strong>Facelets ResourceResolver can be declared via annotation</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1038">spec issue 1038</a>)</p>
<p>In Facelets it has always been possible to let users provide a hook to influence the way that Facelets loads template files. This is done via a <em>ResourceResolver</em> that users can register in web.xml via the environment parameter <em>javax.faces.FACELETS_RESOURCE_RESOLVER</em>.</p>
<p>With such a resolver, users can let Facelets load templates from the classpath or from any location that is expressible via a <em>URL</em>.</p>
<p>The web.xml mechanism however pre-dates the time that Facelets was officially part of JSF itself and it of course requires the presence of such a web.xml file. Since Facelets is now officially part of JSF and there is a strong trend going to make XML optional in favor of annotations, the Facelets resource resolver has now gotten an official annotation in the JSF spec:</p>
<p><em>@FaceletsResourceResolver</em>.</p>
<p>The presence of that annotation on a class automatically registers it as a Facelets resource resolver. If a resolver is specified in both XML and via an annotation, the XML overrides the annotation as per the usual Java EE conventions. If multiple annotated classes are found, the first one encountered is used and a warning is logged.</p>
<p>Commits for this feature have been done between 06/oct/11 and 12/oct/11 and the associated issue has been marked as resolved.<br />
<br/></p>
<p><a name="security"></a></p>
<h3> Security / Type-safety </h3>
<p><a name="869"></a><br />
<strong>Cross Site Request Forgery protection</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-869">spec issue 869</a>) <small>(mentioned in JSR)</small></p>
<p><a href="http://en.wikipedia.org/wiki/Cross-site_request_forgery">Cross Site Request Forgery</a> is an attack that lets users unknowingly do a request to a site where they are supposed to be logged-in, that has some side-effect that is most likely in some way beneficial to the attacker. GET based requests are most obvious here, but POST requests are just as vulnerable.</p>
<p>JSF seems to have some implicit protection against this when state is saved on the server, since a post-back must contain a valid <em>javax.faces.ViewState</em> hidden parameter. Contrary to earlier versions, this value seems sufficiently random in modern JSF implementations.</p>
<p>Nevertheless, JSF 2.2 will contain additional/even stricter protection against this. Among others client state encryption is now on by default and there&#8217;s a token parameter for protection of non-postback views.</p>
<p>Commits for this feature have been done between 13/sep/11 and 21/sep/11.<br />
<br/></p>
<p><a name="745"></a><br />
<strong>More thorough type checking for composite component attributes</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-745">spec issue 745</a>)</p>
<p>In JSF 2.1, it&#8217;s not always easy to defer the exact type of a value expression bound to an attribute of a composite component, if said expression resolves to null. Finding the right type just by asking <em>ElResolver</em> is complicated by the fact that <em>ELResolver#getType</em> is supposed to return the most general type that is accepted by the corresponding <em>ELResolver#setValue</em>. It&#8217;s also not always equal to <em>getValue().getClass()</em> as explained by the <a href="http://download.oracle.com/docs/cd/E17802_01/products/products/jsp/2.1/docs/jsp-2_1-pfd2/javax/el/ELResolver.html#getType%28javax.el.ELContext,%20java.lang.Object,%20java.lang.Object%29">javadoc</a>.</p>
<p>In JSF 2.1, <em>CompositeComponentAttributesELResolver#getType</em> returned null when asked for the type, since it&#8217;s a read only resolver. </p>
<p>For JSF 2.2 this is replaced with an algorithm that first checks if the base is an <em>ExpressionEvalMap</em> and if so simply gets the value expression from that and asks it for its type. Then the metadata for the composite component is consulted to see if there&#8217;s a type being set for the given attribute (this corresponds to the <em>type</em> attribute on the <em>cc:attribute</em> tag). Finally, if a type is found via the metadata it&#8217;s only used if the type obtained from the value expression is either null, or if the metadata type is more specific (narrower).</p>
<p>Commits for this feature have been done between 20/jul/11 and 10/aug/11.<br />
<br/></p>
<p><a name="java"></a></p>
<h3> Java API </h3>
<p><a name="611"></a><br />
<strong>FaceletFactory in the standard API</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-611">spec issue 611</a>)</p>
<p>JSF has a facility for obtaining factories for various things via the <em>FactoryFinder</em>. Even though Facelets is a standard part of JSF since 2.0, the FaceletFactory could not be obtained this way as it was an implementation detail.</p>
<p>In JSF 2.2, the status of Facelets as a default technology within JSF has been given another boost by making this factory obtainable from the <em>FactoryFinder</em> and moving it to the API part of JSF.</p>
<p>Commits for this feature have been done on 15/sep/11 and the associated issue has been marked as resolved.<br />
<br/></p>
<p><a name="599"></a><br />
<strong>Instantiating composite component in Java</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-599">spec issue 599</a>)</p>
<p>JSF 2.0 introduced the concept of the composite component: a component that is created via an .xhtml template instead of Java code. Even though the composite component is defined in .xhtml, it&#8217;s a first class component and ends up in the component tree as a Java component.</p>
<p>Up till now however there wasn&#8217;t any official API to instantiate a composite component as a Java instance via user code. So when building a component tree programmatically (see e.g. <a href="http://jdevelopment.nl/authoring-jsf-pages-pure-java/">Authoring JSF pages in pure Java</a>), incorporating those composite components was challenging to say the least.</p>
<p>JSF 2.2 will provide an explicit API for this.</p>
<p>Commits for this feature have been done between 02/oct/11 and 04/oct/11 and the associated issue has been marked as resolved.<br />
<br/></p>
<p><a name="479"></a><br />
<strong>Support for the Collection interface in UIData</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-479">spec issue 479</a>) <small>(mentioned in JSR)</small></p>
<p>From the beginning of JSF, the UIData component (known from e.g. <em>&lt;h:dataTable></em>) only realistically supports the List, native array and JSF specific DataModel as input for its value binding*. This means other collection types always have to be expressed as any of these types. While this is not particular difficult, it&#8217;s a tedious thing to do and makes the code more verbose.</p>
<p>This issue has a rather long history, but it&#8217;s now finally addressed.</p>
<p>JSF 2.2 will introduce a <em>javax.faces.model.CollectionDataModel</em> in which <em>UIData</em> wraps an incoming value if it&#8217;s of type <em>Collection</em>. <em>Collection</em> is one of the last types being checked, so if the incoming value is a <em>List</em> the <em>ListDataModel</em> takes precedence.</p>
<p>The following are now the supported types:</p>
<ul>
<li>null (becomes empty list)</li>
<li>javax.faces.model.DataModel</li>
<li>java.util.List</li>
<li>java.lang.Object[]</li>
<li>java.sql.ResultSet</li>
<li>javax.servlet.jsp.jstl.sql.Result</li>
<li>java.util.Collection <em>new!</em></li>
<li>java.lang.Object (becomes ScalarDataModel)</li>
</ul>
<p>Commits for this feature have been done on 31/jan/12 and the associated issue has been marked as resolved.</p>
<p><small>* java.sql.ResultSet and javax.servlet.jsp.jstl.sql.Result are officially also supported, but it seems usage of those are rather rare in practice. Also, only DataModel is internally supported, other types are automatically adapted into a DataModel.</small><br />
<br/></p>
<p><a name="lifecycle"></a></p>
<h3> Lifecycle </h3>
<p><a name="787"></a><br />
<strong>Restoring view scope before view is build</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-787">spec issue 787</a>)</p>
<p>In JSF 2.0 a new scope called the view scope was introduced. The life-time of this scope is coupled to that of the view state associated with the component tree for a given view. As such it&#8217;s easy to just store the data in this scope along with the view state and restore it whenever view state is restored, which happens right after the view has been build.</p>
<p>However, for some advanced usages it&#8217;s more convenient to have the view scope restored before the view is build again. As a very contrived example, the bean <em>Intro</em> from the code shown in <a href="http://jdevelopment.nl/single-class-pure-java-jsf-application/">Single class pure Java JSF application</a> could not be view scoped, since the view scope doesn&#8217;t exists at the time the view needs to be build.</p>
<p>Disentangling the view scope from the general view state data actually has other benefits as well, e.g. users could provide alternative handlers for the view scope, something which is not possible or at least very difficult and not portable at the moment.</p>
<p>Currently a new method (<em>UIViewRoot#restoreViewScopeState(FacesContext context, Object state)</em>) to restore only ViewScope has been committed. A <a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1034">separate effort</a> is planned for JSF 2.2 to also create the corresponding <em>saveViewScopeState</em>, but as of yet nothing has been committed for that.</p>
<p>Commits for this feature have been done between 9/jun/11 and 27/jul/11 and the associated issue has been marked as resolved.<br />
<br/></p>
<p><a name="766"></a><br />
<strong>System events for The Flash</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-766">spec issue 766</a>)</p>
<p>JSF 2 introduced the concept of <a href="http://javaserverfaces.java.net/nonav/docs/2.1/javadocs/javax/faces/event/SystemEvent.html" title="SystemEvent (JavaServer Faces API (2.1))">system events</a>, which are events that can be fired by arbitrary objects at arbitrary points during the request processing lifecycle.</p>
<p>In the current version JSF 2.1 there are some 16 events defined, e.g. <em>PostAddToViewEvent</em>, <em>PostConstructViewMapEvent</em>, <em>PreRenderViewEvent</em>, <em>PreValidateEvent</em>, etc.</p>
<p>JSF 2.2 will fire 4 additional events specifically for usage with The Flash. These are:</p>
<ol>
<li><em>PostKeepFlashValueEvent</em> &#8211; Fired when a value is kept in The Flash</li>
<li><em>PostPutFlashValueEvent</em> &#8211; Fired when a value is stored in The Flash</li>
<li><em>PreClearFlashEvent</em> &#8211; Fired before The Flash is cleared</li>
<li><em>PreRemoveFlashValueEvent</em> &#8211; Fired when a value is removed from the Flash</li>
</ol>
<p>Commits for this feature have been done between 28/oct/11 and 31/oct/11.<br />
<br/></p>
<p><a name="1061"></a><br />
<strong>Publish PostRestoreStateEvent</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1061">spec issue 1061</a>)</p>
<p>Normally system events in JSF are fired/published via the <a href="http://docs.oracle.com/javaee/6/api/javax/faces/application/Application.html#publishEvent(javax.faces.context.FacesContext,%20java.lang.Class,%20java.lang.Object)">Application#publishEvent</a> API.</p>
<p>However in JSF 2.1 and before, <em>PostRestoreStateEvent</em> has been an exception to this rule. The problem is that this event must be delivered to all UIComponents. Requiring the use of the publish API for this is sub-optimal with respect to memory requirements, since it would also require every component to return a list with itself as a listener when <em>UIComponent.getListenersForEventClass()</em> is called.</p>
<p>Earlier versions of JSF therefor delivered the event directly to the components using a tree traversal, but this meant it was not possible to install any listeners for this event.</p>
<p>In JSF 2.2 this problem was solved by simply doing both the event publishing (without making UIComponents default listeners) AND doing the traditional tree traversal. In Mojarra, the event is published first and then the tree traversal starts.</p>
<p>Commits for this feature have been done on 15/dec/11.<br />
<br/></p>
<p><a name="1028"></a><br />
<strong>Mandate tree visiting for partial state saving</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1028">spec issue 1028</a>)</p>
<p>In order to implement the partial state saving feature of JSF, an implementation must somehow look at all components in the component tree and ask them for their (partial) state.</p>
<p>The exact way in which this is done was implementation specific in JSF 2.1 and earlier. Mojarra used a tree visiting algorithm for this, while MyFaces used a so-called &#8220;facets + children&#8221; traversal. </p>
<p>The differences between those two are somewhat technical, but one of them is that tree visiting allows the algorithm for the actual traversal to be plugged-in (<a href="http://en.wikipedia.org/wiki/Strategy_pattern" title="Strategy pattern - Wikipedia" target="_blank">strategy pattern</a>), while the &#8220;facets + children&#8221; traversal doesn&#8217;t allow this (the code accesses the list of children of a component directly and iterates over it).</p>
<p>Another major difference is that tree visiting is &#8220;in context&#8221; (parent component can set up a context/scope before its children are visited), while &#8220;facets + children&#8221; traversals just follow a &#8216;dumb&#8217; pointer structure.</p>
<p>Finally, tree visits have the ability to visit &#8220;virtual components&#8221; created by iterating components like <em>UIData</em>. (for a typical JSF data table, there is no component per row in the tree, but the <em>UIData</em> creates this illusion by swapping state in and out for each row).</p>
<p>For state saving, this are somewhat conflicting properties. Setting up a context and doing iterating over virtual children is unwanted, but influencing the traversal can be important. <a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-545" title="New visit hints: EXECUTE_STATE_SAVING, SKIP_ITERATION" target="_blank">JSF 2.1 introduced</a> the <em>IS_SAVING_STATE</em> and <em>SKIP_ITERATION</em> hints, which can undo the unwanted effects, while keeping the ability to influence the traversal.</p>
<p>In JSF 2.2, tree visiting will now be mandated for partial state saving. <em>StateManager#saveView</em> and <em>StateManager#restoreView</em> have been deprecated in favor of methods with the same name in <em>StateManagementStrategy</em>, for which implementations are now required to use the visit API (<em>UIComponent#visitTree</em>), e.g. as-in the simplified example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">viewRoot.<span style="color: #006633;">visitTree</span><span style="color: #009900;">&#40;</span>visitContext, <span style="color: #000000; font-weight: bold;">new</span> VisitCallback<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;">public</span> VisitResult visit<span style="color: #009900;">&#40;</span>VisitContext context, UIComponent target<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><span style="color: #339933;">!</span>target.<span style="color: #006633;">isTransient</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>          
            savedState.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>
                target.<span style="color: #006633;">getClientId</span><span style="color: #009900;">&#40;</span>context.<span style="color: #006633;">getFacesContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>,
                target.<span style="color: #006633;">saveState</span><span style="color: #009900;">&#40;</span>context.<span style="color: #006633;">getFacesContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>        
        <span style="color: #000000; font-weight: bold;">return</span> ACCEPT<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><i>(This is a rather advanced feature that the average JSF application developer will not likely come in contact with.)</i></p>
<p>Commits for this feature have been done on 22/dec/11.<br />
<br/></p>
<p><a name="components"></a></p>
<h3> Components </h3>
<p><a name="984"></a><br />
<strong>Component modification management</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-984">spec issue 984</a>)</p>
<p>In JSF, components can introduce variables in a kind of &#8220;component scope&#8221; or &#8220;component context&#8221;. The <em>var</em> attribute of iterating components like <em>UIData</em> or <em>UIRepeat</em> is a well known example of this. E.g.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ui:repeat</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{someBean.items}&quot;</span> <span style="color: #000066;">var</span>=<span style="color: #ff0000;">&quot;item&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- item is in scope here --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ui:repeat<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- item no longer in scope --&gt;</span></pre></td></tr></table></div>

<p>In JSF 2.1 and before there is no explicit API that components can use to manage this context. This is potentially troublesome for components that need to do a full tree visit within a clean context.</p>
<p>To assist with managing this context, JSF 2.2 will introduce a <em>ComponentModificationManager</em> that can be obtained via a call to <em>FacesContext#getComponentModificationManager</em></p>
<p>Although details are scarce at the moment (only an API draft has been committed), it&#8217;s possible the API might be used by components as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td 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;">boolean</span> visitTree<span style="color: #009900;">&#40;</span>VisitContext visitContext, VisitCallback callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ComponentModificationManager manager <span style="color: #339933;">=</span> visitContext.<span style="color: #006633;">getFacesContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getComponentModificationManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    ComponentModification modifications <span style="color: #339933;">=</span> manager.<span style="color: #006633;">suspend</span><span style="color: #009900;">&#40;</span>visitContext.<span style="color: #006633;">getFacesContext</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: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">visitTree</span><span style="color: #009900;">&#40;</span>visitContext, callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
        manager.<span style="color: #006633;">resume</span><span style="color: #009900;">&#40;</span>visitContext.<span style="color: #006633;">getFacesContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, modifications<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><i>(This is a rather advanced feature that the average JSF application developer will not likely come in contact with.)</i></p>
<p>Commits for this feature have been done on 16/dec/11.<br />
<br/></p>
<p><a name="xml"></a></p>
<h3> XML configuration </h3>
<p><a name="1010"></a><br />
<strong>Case insensitivity for state saving method</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1010">spec issue 1010</a>)</p>
<p>In JSF one can set whether state is saved on the server or on the client via the context parameter <em>javax.faces.STATE_SAVING_METHOD</em> in web.xml which can be set to &#8220;server&#8221; or &#8220;client&#8221;. The values had to be used exactly as given. Using &#8220;Client&#8221; or &#8220;CLIENT&#8221; would actually result in server state saving being used. As of JSF 2.2 the value is made case insensitive, so those preferring to use &#8220;Client&#8221; can now do so.</p>
<p>Commits for this feature have been done on 26/may/11 and the associated issue has been marked as resolved.<br />
<br/></p>
<p><a name="standards"></a></p>
<h3> Standards compliance </h3>
<p><a name="220"></a><br />
<strong>Using only name attribute for view state field</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-220">spec issue 220</a>)</p>
<p>For keeping track of state associated with forms, JSF implementations write a hidden input field in each form that&#8217;s used on a page. </p>
<p>In JSF 1.2 this field was standardized and the specification demanded that <strong>both</strong> the <em>id</em> and <em>name</em> attributes were set to <em>javax.faces.ViewState</em>*.</p>
<p>The rule however breaks &#8220;W3C XHTML 1.0 Transitional&#8221; and above standards compliance. Namely, per that standard the <em>id</em> attribute should be globally unique within a document. If the JSF spec mandates that it should always have a fixed value, it clearly can&#8217;t be unique if multiple forms are being used on a single page.</p>
<p>Very early on, Mojarra introduced the parameter <em>com.sun.faces.enableViewStateIdRendering</em>, that when set to false simply omitted rendering the <em>id</em> attribute (thus leaving only the <em>name</em> present). Since this is an implementation specific parameter, not all third party libraries necessarily take its effects into account.</p>
<p>Therefor from JSF 2.2 on only the <em>name</em> attribute is mandated and JSF 2.2 compatible libraries should no longer assume anything about an <em>id</em> attribute being available. In addition, Mojarra has removed the <em>com.sun.faces.enableViewStateIdRendering</em> parameter completely, so setting this explicitly to true will not bring the <em>id</em> attribute back.</p>
<p>Commits for this feature have been done on 30/jan/12 and the associated issue has been marked as resolved.</p>
<p>*<small>In addition the spec strongly recommends, though doesn&#8217;t enforce, that the value of this input field is difficult to predict in order to prevent cross site scripting attacks, see <a href="#869">Cross Site Request Forgery protection</a> for more details.</small></p>
<p><br/></p>
<p>That&#8217;s it for now! I&#8217;ll periodically update this page when new information comes in.</p>
<p>Keep in mind that there are quite some interesting other things under discussion or even in development for possible inclusion in JSF 2.2. Such development is only currently taking place outside of the JSF trunk, which is the focus of this article. For instance see the discussion about a <a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-949" target="_blank">Window-id</a> which is prototyped in <a href="https://cwiki.apache.org/confluence/display/EXTCDI/JSF+Usage" target="_blank">MyFaces/CODI</a> where a <a href="https://cwiki.apache.org/confluence/display/MYFACES/WindowId+Proposal" target="_blank">proposal</a> is being setup as well, or the one about a <a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-971" target="_blank"><strong>multi-templating system</strong></a> (skinning), which is prototyped by <a href="http://weblogs.java.net/blog/lamineba/archive/2011/08/28/multi-templating-jsf-2-prototype" target="_blank">lamine ba</a> and mentioned by spec lead Ed Burns in an <a href="http://www.infoq.com/news/2012/01/jsf-update-2.x" target="_blank">interview</a> (among with several other interesting things, like <strong>HTML5</strong> support).</p>
<p><i>Arjan Tijms</i></p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/jsf-22/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Easily disable sorting in PrimeFaces 3&#8242;s DataTable</title>
		<link>http://jdevelopment.nl/easily-disable-sorting-primefaces-3s-datatable/</link>
		<comments>http://jdevelopment.nl/easily-disable-sorting-primefaces-3s-datatable/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 17:42:04 +0000</pubDate>
		<dc:creator>development</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=1272</guid>
		<description><![CDATA[PrimeFaces provides a convenient and easy to use sorting facility for its DataTable. Together with Facelets, this facility allows us to create re-usable columns that are natural sortable by default. E.g.: 1 2 3 4 5 6 7 8 9 &#60;ui:composition xmlns=&#34;http://www.w3.org/1999/xhtml&#34; xmlns:h=&#34;http://java.sun.com/jsf/html&#34; xmlns:ui=&#34;http://java.sun.com/jsf/facelets&#34; xmlns:p=&#34;http://primefaces.org/ui&#34; &#62; &#60;p:column headerText=&#34;#{headerText}&#34; sortBy=&#34;#{value}&#34;&#62; &#60;h:outputText id=&#34;#{id}&#34; value=&#34;#{value}&#34; /&#62; &#60;/p:column&#62; &#60;/ui:composition&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>PrimeFaces provides a convenient and easy to use sorting facility for its <a href="http://www.primefaces.org/showcase/ui/pprDataTable.jsf" target="_blank">DataTable</a>. Together with Facelets, this facility allows us to create re-usable columns that are natural sortable by default. E.g.:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ui:composition</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:h</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/html&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:ui</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/facelets&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:p</span>=<span style="color: #ff0000;">&quot;http://primefaces.org/ui&quot;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p:column</span> <span style="color: #000066;">headerText</span>=<span style="color: #ff0000;">&quot;#{headerText}&quot;</span> <span style="color: #000066;">sortBy</span>=<span style="color: #ff0000;">&quot;#{value}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:outputText</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;#{id}&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{value}&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:column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>    
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ui:composition<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Such a column can be used on a page inside a <code>DataTable</code> as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;my:sortableColumn</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;foo&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{someBean.someValue}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<h3> The problem </h3>
<p>The above is only a simple example, and real-life usage can be more complex with e.g. default styles and cell editing capabilities added. With such complex tags, it might be desirable to turn off sorting for certain columns programmatically. By default this does not seem to be possible in PrimeFaces. Setting the <code>value</code> attribute in the above example to null or the empty string via an expression prevents sorting, but still renders the sorting icon.</p>
<p>The reason for this is that the PrimeFaces renderer (<code>org.primefaces.component.datatable.DataTableRenderer</code>) checks for the presence of a value expression to determine whether the icon should be rendered, regardless of whether this expression evaluates to something non-empty. So nothing you pass into the <code>value</code> attribute of the above showed tag can make the value expression itself null, nor can any extra attribute accomplish this in the tag&#8217;s definition.</p>
<h3> Custom helper tag </h3>
<p>One solution is to build a simple helper tag that takes a parameter and based on that sets the value expression of its parent component to null. This tag can be nested inside a column. Taking it one step further, we can nest the tag inside a table and programmatically disable sorting on all columns. The following shows the implementation of such a tag:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td 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> ColumnSorterDisabler <span style="color: #000000; font-weight: bold;">extends</span> TagHandler <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> ColumnSorterDisabler<span style="color: #009900;">&#40;</span>TagConfig config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> apply<span style="color: #009900;">&#40;</span>FaceletContext ctx, UIComponent parent<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Boolean</span> disableSorting <span style="color: #339933;">=</span> getRequiredAttribute<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;disableSorting&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getBoolean</span><span style="color: #009900;">&#40;</span>ctx<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>disableSorting<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>parent <span style="color: #000000; font-weight: bold;">instanceof</span> Column<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                parent.<span style="color: #006633;">setValueExpression</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sortBy&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span><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: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>parent <span style="color: #000000; font-weight: bold;">instanceof</span> DataTable<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>UIComponent child <span style="color: #339933;">:</span> parent.<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>child <span style="color: #000000; font-weight: bold;">instanceof</span> Column<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        child.<span style="color: #006633;">setValueExpression</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sortBy&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>        
    <span style="color: #009900;">&#125;</span>    
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>After declaring this in a <code>*-taglib.xml</code>, we can use it as follows inside a Facelets tag:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ui:composition</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:h</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/html&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:ui</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/facelets&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:p</span>=<span style="color: #ff0000;">&quot;http://primefaces.org/ui&quot;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;my:columnSorterDisabler</span> <span style="color: #000066;">disableSorting</span>=<span style="color: #ff0000;">&quot;#{disableSorting}&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;p:column</span> <span style="color: #000066;">headerText</span>=<span style="color: #ff0000;">&quot;#{headerText}&quot;</span> <span style="color: #000066;">sortBy</span>=<span style="color: #ff0000;">&quot;#{value}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:outputText</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;#{id}&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{value}&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:column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>    
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ui:composition<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>We can now disable sorting on a per-column basis as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;my:sortableColumn</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;foo&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{someBean.someValue}&quot;</span> <span style="color: #000066;">disableSorting</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>or for an entire table:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p:dataTable</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{someBean.someValues}&quot;</span> <span style="color: #000066;">var</span>=<span style="color: #ff0000;">&quot;something&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;my:sortableColumn</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;foo&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{something.foo}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;my:sortableColumn</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;bar&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{something.bar}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;my:columnSorterDisabler</span> <span style="color: #000066;">disableSorting</span>=<span style="color: #ff0000;">&quot;true&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:dataTable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>One caveat to remember, <code>TagHandler</code>s exist only at &#8216;build-time&#8217;, so when using expressions to do the disabling the data these point to has to be available during build-time.</p>
<h3> The f:attributes tag </h3>
<p>Another solution, contributed by my co-worker Bauke Scholtz, is taking advantage of the <code>&lt;f:attribute></code> tag. This tag does the same thing as an actual tag attribute, but contrary to an actual attribute this one can be conditionally excluded using <code>&lt;c:if></code>. In that case the implementation of our re-usable column becomes this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ui:composition</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:h</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/html&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:ui</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/facelets&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:p</span>=<span style="color: #ff0000;">&quot;http://primefaces.org/ui&quot;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p:column</span> <span style="color: #000066;">headerText</span>=<span style="color: #ff0000;">&quot;#{headerText}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:if</span> <span style="color: #000066;">test</span>=<span style="color: #ff0000;">&quot;#{empty sortable or sortable}&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:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;sortBy&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{value}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/c:if<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:outputText</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;#{id}&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{value}&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:column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>    
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ui:composition<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Notice how the tag attribute <code>sortBy</code> has been replaced by a child tag. This approach is really convenient since it simply uses JSF&#8217;s build-in functionality. To disable sorting for all columns at once, the helper tag based approach is still useful though. Note that just like the helper tag, a <code>&lt;f:attribute></code> tag only exists at build-time, so here too only data that&#8217;s available during that time can be used.</p>
<p><i>Arjan Tijms</i></p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/easily-disable-sorting-primefaces-3s-datatable/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Passing action methods into Facelets tags</title>
		<link>http://jdevelopment.nl/passing-action-methods-facelets-tags/</link>
		<comments>http://jdevelopment.nl/passing-action-methods-facelets-tags/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 00:14:40 +0000</pubDate>
		<dc:creator>development</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jsf]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=1207</guid>
		<description><![CDATA[The problem JSF, via Facelets, has various mechanisms to easily reuse view content. One of those is the Facelets tag, which allows one to reuse markup and/or components. One notorious problem with these Facelets tags is that you can&#8217;t directly pass action methods (method expressions) into them. There&#8217;s a workaround where you break the expression [...]]]></description>
			<content:encoded><![CDATA[<h3> The problem </h3>
<p>JSF, via Facelets, has various mechanisms to easily reuse view content. One of those is the Facelets tag, which allows one to <a href="https://www.ibm.com/developerworks/java/library/j-facelets/#N1021A" title="Composition Components" target="_blank">reuse markup and/or components</a>.</p>
<p>One notorious problem with these Facelets tags is that you can&#8217;t directly pass action methods (method expressions) into them. There&#8217;s a workaround where you break the expression into two parts, the base (which should be a value expression) and the name of the method as a plain string. Inside the tag these two are then combined again, e.g. via the array notation syntax. See e.g.</p>
<ul>
<li><a href="http://digitaljoel.nerd-herders.com/2009/08/25/passing-action-methods-in-facelets-using-array-notation">Passing action methods in Facelets using array notation</a></li>
<li><a href="http://seamframework.org/Community/FaceletsParamForActionMethod" target="_blank">Facelets Param for action method</a></li>
</ul>
<p>Another approach is to create a small helper tag that converts the value expression in some way into a method expression. There have been a couple of implementations of this idea:</p>
<ul>
<li><a href="http://drewdev.blogspot.com/2006/06/creating-composite-controls-with-jsf.html" target="_blank">Creating composite controls with JSF and facelets</a></li>
<li><a href="http://docs.jboss.org/richfaces/latest_3_3_X/en/realworld/html/Button.html" target="_blank">How the button is created and how it acts</a> <small>(see ActionMapperTagHandler at the bottom of the page)</small></li>
</ul>
<h3> Finding a solution </h3>
<p>In this blog I would like to present another variant on the tag handler idea, which doesn&#8217;t require nesting the content and also doesn&#8217;t parse any embedded EL expression manually. It supports EL parameters provided by the calling view. It&#8217;s not perfect however, since it does not support parameters being passed into the method from the target component (e.g. an <code>ActionEvent</code> for an <code>actionListener</code>).</p>
<p>The idea is to wrap the value expression (which represents the original method expression passed into the Facelets tag) inside a custom method expression. This method expression simply gets the value from the embedded value expression, which as &#8220;side-effect&#8221; executes this method.</p>
<p>The resulting method expression has to be stored somewhere. The request scope would be an option, but then it would be directly available outside the Facelets tag, which isn&#8217;t a good example of encapsulation.</p>
<p>An alternative is the <code>javax.el.VariableMapper</code>, a &#8220;magic&#8221; little thing that is able to scope value expressions to the duration of a Facelets tag (it&#8217;s magical since remember that the tag doesn&#8217;t exist any more after the component tree has been build). This however requires a value expresion again, so in an odd twist we wrap the method expression that we just created in a value expression again.</p>
<p>So, how can this work? Wasn&#8217;t the entire point of this exercise to go to a method expression? Well, as it turns out a value expression can be accepted wherever a method expression is required, <em>iff</em> this value expression directly returns a method expression.</p>
<p>E.g. the Apache EL implementation&#8217;s <a href="http://grepcode.com/file/repository.jboss.org/nexus/content/repositories/releases/org.jboss.web/jbossweb/3.0.0-CR2/org/apache/el/parser/AstIdentifier.java#AstIdentifier" title="org.apache.el.parser.AstIdentifier" target="_blank">org.apache.el.parser.AstIdentifier</a> contains the following code fragment that accomplishes this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>141
142
143
144
145
146
147
148
149
150
151
152
153
154
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// case A: ValueExpression exists, getValue which must</span>
<span style="color: #666666; font-style: italic;">// be a MethodExpression</span>
VariableMapper varMapper <span style="color: #339933;">=</span> ctx.<span style="color: #006633;">getVariableMapper</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ValueExpression ve <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>varMapper <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>
    ve <span style="color: #339933;">=</span> varMapper.<span style="color: #006633;">resolveVariable</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">image</span><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>ve <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>
        obj <span style="color: #339933;">=</span> ve.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span>ctx<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// (case B omitted)</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj <span style="color: #000000; font-weight: bold;">instanceof</span> MethodExpression<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>MethodExpression<span style="color: #009900;">&#41;</span> obj<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3> Code </h3>
<p>Without further ado, here&#8217;s the code for the TagHandler that implements all the wrapping:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td 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> MethodParamHandler <span style="color: #000000; font-weight: bold;">extends</span> TagHandler <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> TagAttribute name<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> TagAttribute value<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> MethodParamHandler<span style="color: #009900;">&#40;</span>TagConfig config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getRequiredAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">value</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getRequiredAttribute</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>
    <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> apply<span style="color: #009900;">&#40;</span>FaceletContext ctx, UIComponent parent<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> nameStr <span style="color: #339933;">=</span> name.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span>ctx<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// The original value expression we get inside the Facelets tag, that's actually the method expression passed-in by the user.</span>
        ValueExpression valueExpression <span style="color: #339933;">=</span> value.<span style="color: #006633;">getValueExpression</span><span style="color: #009900;">&#40;</span>ctx, <span style="color: #003399;">Object</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// A method expression that wraps the value expression and uses its own invoke method to get the value from the wrapped expression.</span>
        MethodExpression methodExpression <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MethodExpressionValueExpressionAdapter<span style="color: #009900;">&#40;</span>valueExpression<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Using the variable mapper so the expression is scoped to the body of the Facelets tag. Since the variable mapper only accepts</span>
        <span style="color: #666666; font-style: italic;">// value expressions, we once again wrap it by a value expression that directly returns the method expression.</span>
        ctx.<span style="color: #006633;">getVariableMapper</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setVariable</span><span style="color: #009900;">&#40;</span>nameStr, ctx.<span style="color: #006633;">getExpressionFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">createValueExpression</span><span style="color: #009900;">&#40;</span>methodExpression, MethodExpression.<span style="color: #000000; font-weight: bold;">class</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: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The rather trivial adapter that wraps the value expression is shown below. The main method of interest here is <code>invoke()</code>. Note how unfortunately the <code>params</code> parameter has to be ignored.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td 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> MethodExpressionValueExpressionAdapter <span style="color: #000000; font-weight: bold;">extends</span> MethodExpression <span style="color: #009900;">&#123;</span>
&nbsp;
    <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: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> ValueExpression valueExpression<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> MethodExpressionValueExpressionAdapter<span style="color: #009900;">&#40;</span>ValueExpression valueExpression<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;">valueExpression</span> <span style="color: #339933;">=</span> valueExpression<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> invoke<span style="color: #009900;">&#40;</span>ELContext context, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> params<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> valueExpression.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> MethodInfo getMethodInfo<span style="color: #009900;">&#40;</span>ELContext context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>                
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> MethodInfo<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, valueExpression.<span style="color: #006633;">getExpectedType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isLiteralText<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: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> hashCode<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> valueExpression.<span style="color: #006633;">hashCode</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;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getExpressionString<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> valueExpression.<span style="color: #006633;">getExpressionString</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;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> obj<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>obj <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">this</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: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj <span style="color: #000000; font-weight: bold;">instanceof</span> MethodExpressionValueExpressionAdapter<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>MethodExpressionValueExpressionAdapter<span style="color: #009900;">&#41;</span>obj<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getValueExpression</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>valueExpression<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> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> ValueExpression getValueExpression<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> valueExpression<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The tag handler has to be registered in a *-taglib.xml file, e.g.:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tag<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tag-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>methodParam<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tag-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;handler-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.example.MethodParamHandler<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/handler-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>name<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;required<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/required<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>java.lang.String<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>value<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;required<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/required<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>java.lang.String<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tag<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h3> Using the tag handler </h3>
<p>So the above takes care of implementing the converting tag handler. Let&#8217;s take a look at how we would use this.</p>
<p>Suppose we have a Facelets tag in the following using-view fragment, where the action attribute binds to a method in a backing bean taking an EL parameter:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p:dataTable</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;table&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{someBean.someValues}&quot;</span> <span style="color: #000066;">var</span>=<span style="color: #ff0000;">&quot;someValue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;my:deleteActionColumn</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;#{someBean.delete(someValue)}&quot;</span> <span style="color: #000066;">update</span>=<span style="color: #ff0000;">&quot;table&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:dataTable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The tag could then be defined as shown below. The interesting bit is the <code>&lt;my:methodParam></code> tag, which converts the value expression &#8220;action&#8221; to the method expression &#8220;actionMethod&#8221;. The tag can be placed at many locations, but reasonable choices would be as the first child of the root or directly above the component using it. I choose the latter here.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ui:composition</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:ui</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/facelets&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:h</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/html&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:p</span>=<span style="color: #ff0000;">&quot;http://primefaces.org/ui&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:my</span>=<span style="color: #ff0000;">&quot;http://example.com/my&quot;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p:column</span> <span style="color: #000066;">styleClass</span>=<span style="color: #ff0000;">&quot;some_style&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;my:methodParam</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;actionMethod&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{action}&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;p:commandLink</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;someId&quot;</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;#{actionMethod}&quot;</span> <span style="color: #000066;">process</span>=<span style="color: #ff0000;">&quot;@this&quot;</span> <span style="color: #000066;">update</span>=<span style="color: #ff0000;">&quot;#{update}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:graphicImage</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/icons/delete.png&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;#{i18n['something.delete']}&quot;</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;#{i18n['something.delete.tooltip']}&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;15&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;15&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:commandLink<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p: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;/ui:composition<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>This Facelets tag too again has to be explicitly declared in a *-taglib.xml, e.g.:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tag<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tag-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>deleteActionColumn<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tag-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>faces/tags/deleteActionColumn.xhtml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>			
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>action<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>        
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.el.MethodExpression<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tag<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h3> Any other alternatives? </h3>
<p>At first sight the composite component mechanism would seem to be an alternative, as it explicitly supports passing in method references. However, the composite component isn&#8217;t a true substitute for Facelets tags. Namely, it inserts a parent component in the tree instead of simply its content. This doesn&#8217;t work for tables, panel groups, and all other kinds of components that require children of a specific type or in a specific order to be present. </p>
<p>You can sometimes do some magic with composite components by using the <code>componentType</code> attribute (see e.g. <a href="http://jugojava.blogspot.com/2011/09/jsf-composite-component-binding-to.html" target="_blank">JSF composite component binding to custom UIComponent</a>), but this only gets you so far and sometimes you really simply need the raw body of a tag to be inserted into the using page.</p>
<p>Another alternative solution, arguably the one and only Real Solution™, is having this fixed by Facelets. Indeed, many years ago there was an issue created for this at the Facelets JIRA: <a href="http://java.net/jira/browse/FACELETS-263" target="_blank">http://java.net/jira/browse/FACELETS-263</a> with a similar issue even having a patch submitted for it: <a href="http://java.net/jira/browse/FACELETS-273" target="_blank">http://java.net/jira/browse/FACELETS-273</a>. Unfortunately, neither of those issues ever got resolved.</p>
<p>Hopefully the alternative solution provided here is useful for those situations where the existing workarounds are not completely satisfactory. Do note again that the solution presented here is also not ideal, but seems to work nicely in a rather straightforward way for action methods that don&#8217;t use framework provided parameters, but can optionally take user supplied EL parameters.</p>
<p><i>Arjan Tijms</i></p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/passing-action-methods-facelets-tags/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automatic to-Object conversion in JSF selectOneMenu &amp; Co.</title>
		<link>http://jdevelopment.nl/automatic-toobject-conversion-jsf-selectonemenu/</link>
		<comments>http://jdevelopment.nl/automatic-toobject-conversion-jsf-selectonemenu/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 20:17:52 +0000</pubDate>
		<dc:creator>development</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jsf]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=1167</guid>
		<description><![CDATA[When creating a web UI, there is often the need to let a user make a selection from e.g. a drop-down. The items in such a drop-down are not rarely backed by domain objects. Since these items in the HTML rendering are just simple character identifiers, we need some way to encode the object version [...]]]></description>
			<content:encoded><![CDATA[<p>When creating a web UI, there is often the need to let a user make a selection from e.g. a drop-down. The items in such a drop-down are not rarely backed by domain objects. Since these items in the HTML rendering are just simple character identifiers, we need some way to encode the object version of an item to this simple character identifier (its string representation) and back again.</p>
<p>A well established pattern in JSF is the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>selectOneMenu value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#{bean.selectedUser}&quot;</span> converter<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#{userConverter}&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>selectItems value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#{bean.selectableUsers}&quot;</span> var<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;user&quot;</span> itemValue<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#{user}&quot;</span> itemLabel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#{user.name}&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>h<span style="color: #339933;">:</span>selectOneMenu<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>With <code>userConverter</code> defined as something like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">@ManagedBean<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;userConverter&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> UserConverter <span style="color: #000000; font-weight: bold;">implements</span> Converter <span style="color: #009900;">&#123;</span>
&nbsp;
    @EJB
    <span style="color: #000000; font-weight: bold;">private</span> UserDAO userDAO<span style="color: #339933;">;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> getAsObject<span style="color: #009900;">&#40;</span>FacesContext context, UIComponent component, <span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> userDAO.<span style="color: #006633;">getById</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getAsString<span style="color: #009900;">&#40;</span>FacesContext context, UIComponent component, <span style="color: #003399;">Object</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>User<span style="color: #009900;">&#41;</span> value<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</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: #009900;">&#125;</span></pre></td></tr></table></div>

<p><em>(note that the converter implementation is overly simplified, in practice null checks and checks for &#8220;<code>String value</code>&#8221; representing a number would be needed)</em></p>
<p>This is functional, but in its full form not really convenient to program nor efficient. Upon every post back, there will be a call to the DAO to convert the string representation of an item back to its Object form. When no caching is used or the object isn&#8217;t in the cache, this will likely result in a call to a back-end database, which is never a positive contribution to the overall performance of a page. It gets worse when there are more of such conversions being done and it can go through the roof when we have e.g. editable tables where each row contains multiple drop-downs. A table with only 10 rows and 4 drop-downs might result in 40 separate calls to the back-end being done.</p>
<p>But is using a DAO here really needed?</p>
<p>After all, after the converter does its work, JSF checks whether the Object is <em>equal</em> to any of the Objects in the collection that was used to render the drop-down. In other words, the target Object is already there! And it must be there by definition, since without it being present validation will simply never pass.</p>
<p>So, by taking advantage of this already present collection we can prevent the DAO calls. The only question is, how do we get to this from a converter? A simple way is to use a parameter of some kind and provide it with a binding to this collection. This is however not really DRY, as it means we have to bind to the same collection twice right after each other (once for the selectItems, once for the converter parameter). Additionally, we still have to iterate over this and need custom code that knows to which property of the Object we need to compare the String value. For instance, for our <code>User</code> object we need to convert the String value to a Long first and then compare it with the <code>Id</code> property of each instance.</p>
<p>Another approach that I would like to present here is basically emulating how a <code>UIDataTable</code> detects on which row a user did some action. After the post back it iterates over the data that was used to render the table in the same way again. This will cause the same IDs to be generated as in the original rendering. The ID of the component that is posted back is compared to the newly generated one and when they match iteration stops and we know the row.</p>
<p>In this case, after the post back we&#8217;ll iterate over all select item values, convert these to their string representation and compare that to the &#8216;to-be-converted&#8217; string value. If they match, the unconverted object is the one we&#8217;re after and we return that. For iterating over the select item values, I took advantage of a private utility class that&#8217;s in Mojarra: <code>com.sun.faces.renderkit.SelectItemsIterator</code> (for a proof of concept, I just copied it since it&#8217;s package private).</p>
<p>The implementation is done via a Converter base class from which user code can inherit. That way, only a <code>getAsString</code> method needs to be implemented:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">@ManagedBean<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;userConverter&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> UserConverter <span style="color: #000000; font-weight: bold;">extends</span> SelectItemsBaseConverter <span style="color: #009900;">&#123;</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getAsString<span style="color: #009900;">&#40;</span>FacesContext context, UIComponent component, <span style="color: #003399;">Object</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>User<span style="color: #009900;">&#41;</span> value<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</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: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The base class is implemented as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td 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;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> SelectItemsBaseConverter <span style="color: #000000; font-weight: bold;">implements</span> Converter <span style="color: #009900;">&#123;</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> getAsObject<span style="color: #009900;">&#40;</span>FacesContext context, UIComponent component, <span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>        
        <span style="color: #000000; font-weight: bold;">return</span> SelectItemsUtils.<span style="color: #006633;">findValueByStringConversion</span><span style="color: #009900;">&#40;</span>context, component, value, <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
    <span style="color: #009900;">&#125;</span>    
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And the <code>SelectItemsUtils</code> class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
</pre></td><td 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;">final</span> <span style="color: #000000; font-weight: bold;">class</span> SelectItemsUtils <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> SelectItemsUtils<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;">Object</span> findValueByStringConversion<span style="color: #009900;">&#40;</span>FacesContext context, UIComponent component, <span style="color: #003399;">String</span> value, Converter converter<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> findValueByStringConversion<span style="color: #009900;">&#40;</span>context, component, <span style="color: #000000; font-weight: bold;">new</span> SelectItemsIterator<span style="color: #009900;">&#40;</span>context, component<span style="color: #009900;">&#41;</span>, value, converter<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;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Object</span> findValueByStringConversion<span style="color: #009900;">&#40;</span>FacesContext context, UIComponent component, Iterator<span style="color: #339933;">&lt;</span>SelectItem<span style="color: #339933;">&gt;</span> items, <span style="color: #003399;">String</span> value, Converter converter<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>items.<span style="color: #006633;">hasNext</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>
            SelectItem item <span style="color: #339933;">=</span> items.<span style="color: #006633;">next</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;">if</span> <span style="color: #009900;">&#40;</span>item <span style="color: #000000; font-weight: bold;">instanceof</span> SelectItemGroup<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                SelectItem subitems<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>SelectItemGroup<span style="color: #009900;">&#41;</span> item<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSelectItems</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;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>isEmpty<span style="color: #009900;">&#40;</span>subitems<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003399;">Object</span> object <span style="color: #339933;">=</span> findValueByStringConversion<span style="color: #009900;">&#40;</span>context, component, <span style="color: #000000; font-weight: bold;">new</span> ArrayIterator<span style="color: #009900;">&#40;</span>subitems<span style="color: #009900;">&#41;</span>, value, converter<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>object <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> object<span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>item.<span style="color: #006633;">isNoSelectionOption</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> value.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>converter.<span style="color: #006633;">getAsString</span><span style="color: #009900;">&#40;</span>context, component, item.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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;">return</span> item.<span style="color: #006633;">getValue</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: #009900;">&#125;</span>        
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</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: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">boolean</span> isEmpty<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> array<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> array <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> array.<span style="color: #006633;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>    
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * This class is based on Mojarra version
     */</span>
    <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> ArrayIterator <span style="color: #000000; font-weight: bold;">implements</span> Iterator<span style="color: #339933;">&lt;</span>SelectItem<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> ArrayIterator<span style="color: #009900;">&#40;</span>SelectItem items<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><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;">items</span> <span style="color: #339933;">=</span> items<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> SelectItem items<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> index <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> hasNext<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: #009900;">&#40;</span>index <span style="color: #339933;">&lt;</span> items.<span style="color: #006633;">length</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> SelectItem next<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;">try</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>index<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IndexOutOfBoundsException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">NoSuchElementException</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: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> remove<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;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The source of the Mojarra <code>SelectItemsIterator</code> can be found <a href="http://grepcode.com/file/repo1.maven.org/maven2/com.sun.faces/jsf-api/2.1.4/javax/faces/component/SelectItemsIterator.java">here</a>, MyFaces seems to have a similar implementation <a href="http://grepcode.com/file/repo1.maven.org/maven2/org.apache.myfaces.shared/myfaces-shared-impl/4.1.0/org/apache/myfaces/shared_impl/util/SelectItemsIterator.java">here</a>, but I did not test that one yet. PrimeFaces also has something like this (see <code>org.primefaces.util.ComponentUtils#createSelectItems</code>).</p>
<p>A custom <code>selectOneMenu</code>, <code>selectManyListbox</code> etc could even go a step further and should be able to do this without the need for a converter at all. Combined with an extended <code>selectItems</code> tag, it could look like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;selectOneMenu</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{bean.selectedUser}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;selectItems</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{bean.selectableUsers}&quot;</span> <span style="color: #000066;">var</span>=<span style="color: #ff0000;">&quot;user&quot;</span> <span style="color: #000066;">itemValue</span>=<span style="color: #ff0000;">&quot;#{user}&quot;</span> <span style="color: #000066;">itemValueAsString</span>=<span style="color: #ff0000;">&quot;#{user.id}&quot;</span> <span style="color: #000066;">itemLabel</span>=<span style="color: #ff0000;">&quot;#{user.name}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:selectOneMenu<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>In this hypothetical example, we could even simplify stuff further by setting <code>itemValue</code> by default to <code>var</code>, so it would then look like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:selectOneMenu</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{bean.selectedUser}&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:selectItems</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{bean.selectableUsers}&quot;</span> <span style="color: #000066;">var</span>=<span style="color: #ff0000;">&quot;user&quot;</span> <span style="color: #000066;">itemValueAsString</span>=<span style="color: #ff0000;">&quot;#{user.id}&quot;</span> <span style="color: #000066;">itemLabel</span>=<span style="color: #ff0000;">&quot;#{user.name}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:selectOneMenu<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p></code></small></p>
<p>Implementing this might be the topic for a next article. For now I'll hope the converter based approach is useful.</p>
<p><i>Arjan Tijms</i></p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/automatic-toobject-conversion-jsf-selectonemenu/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Try-with-resources in JDK7 without scoped declarations</title>
		<link>http://jdevelopment.nl/trywithresources-jdk7-scoped-declarations/</link>
		<comments>http://jdevelopment.nl/trywithresources-jdk7-scoped-declarations/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 12:59:27 +0000</pubDate>
		<dc:creator>jan.beernink</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=1069</guid>
		<description><![CDATA[A handy new feature in JDK7 is the try-with-resources statement. This statement is meant to eliminate a lot of the boilerplate code required for managing InputStreams and OutputStreams. Say for example, that I would want to copy the contents of an InputStream to an OutputStream. This would require the following code only to manage the [...]]]></description>
			<content:encoded><![CDATA[<p>A handy new feature in JDK7 is the try-with-resources statement. This statement is meant to eliminate a lot of the boilerplate code required for managing InputStreams and OutputStreams. Say for example, that I would want to copy the contents of an InputStream to an OutputStream. This would require the following code only to manage the in- and output stream:</p>
<pre>InputStream in = createInputStream();
try {
   OutputStream out = createOutputStream();
   try {
      /* Copy data here */
   } finally {
      try {
         out.close();
      } catch(IOException e) {
         //Prevent this exception from suppressing actual exception
      }
   }
} finally {
   try {
      in.close();
   } catch(IOException e) {
      //Prevent this exception from suppressing actual exception
   }
}
</pre>
<p>Using the new try-with-resources statement, the above code can be rewritten as the following:</p>
<pre>try (InputStream in = createInputStream(); OutputStream out =
      createOutputStream()) {
   /* Copy data here */
}</pre>
<p>The InputStream and OutputStream are automatically closed at the end of the try-with-resources block. If an exception is thrown during the main block and then again during the closing of one (or both) of the streams, the exception on the close operation is added to the original exception as a suppressed exception, so no exceptions are swallowed silently anymore. The try-with-resources blocks are also not limited to be used for in- and output streams, but can be used on any object that implements the AutoCloseable interface.</p>
<p>There is one minor disadvantage to the try-with-resource statement, it is required to define the variable that refers to the object to be closed within the brackets after the try. For example, if you have a method that receives an InputStream as a parameter, the Java compiler would not allow you to do this:<br />
<code></code></p>
<pre>public void readData(InputStream in) {
   try(in) {
      int input;
      while((input = in.read()) &gt;=0) {
         //Use input here
      }
   }
}</pre>
<p>The above code would produce a compiler error as no variable has been defined between the brackets of the try-with-resources statement. I would propose the following workaround for this situation:<br />
<code></code></p>
<pre>public void readData(InputStream in) {
   try(InputStream autoCloseableInputStream = in) {
      int input;
      while((input = in.read()) &gt;=0) {
         //Use input here
      }
   }
}</pre>
<p>&nbsp;</p>
<p><code></code><br />
This code does compile and the stream is automatically closed at the end of the try block. Since the autoCloseableInputStream and in variables refer to the exact same object, it is not necessary to actually use the autoCloseableInputStream variable in the code. Using a name like autoCloseableInputStream makes it clear that this variable is only defined in order to be able to use the try-with-resources statement.</p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/trywithresources-jdk7-scoped-declarations/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Single class pure Java JSF application</title>
		<link>http://jdevelopment.nl/single-class-pure-java-jsf-application/</link>
		<comments>http://jdevelopment.nl/single-class-pure-java-jsf-application/#comments</comments>
		<pubDate>Sun, 18 Sep 2011 22:59:17 +0000</pubDate>
		<dc:creator>arjan tijms</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jsf]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=1041</guid>
		<description><![CDATA[In my previous blog entry, Authoring JSF pages in pure Java, it was explained how to set up a JSF based web application using nothing but pure Java. No XML based templating (Facelets) was required and the view was build purely programmatically. I got one remark though that the example code did used expression language [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous blog entry, <a href="http://jdevelopment.nl/authoring-jsf-pages-pure-java/">Authoring JSF pages in pure Java</a>, it was explained how to set up a JSF based web application using nothing but pure Java. No XML based templating (Facelets) was required and the view was build purely programmatically.</p>
<p>I got one remark though that the example code did used expression language (EL) to patch some of the parts together. Although EL is really convenient, as it provides a way to point to a (nested) method on a scoped object, it&#8217;s not regular type-safe Java.</p>
<p>Luckily, the Java component API of JSF doesn&#8217;t only use EL based ValueExpressions and MethodExpressions, but also allows regular Java types to be set as listeners and values. This is demonstrated in the code below. While at it, I&#8217;ve done away with the separate backing bean for this example and let the same class that defines the page handle the action event.</p>
<p>The result is a pure Java, xml-less, EL-less, config-less, single class JSF application [phew]:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">@ManagedBean
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Intro <span style="color: #000000; font-weight: bold;">implements</span> Page, <span style="color: #003399;">ActionListener</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> HtmlOutputText message<span style="color: #339933;">;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> buildView<span style="color: #009900;">&#40;</span>FacesContext context, UIViewRoot root<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        List<span style="color: #339933;">&lt;</span>UIComponent<span style="color: #339933;">&gt;</span> rootChildren <span style="color: #339933;">=</span> root.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        UIOutput output <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UIOutput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        output.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;html xmlns=<span style="color: #000099; font-weight: bold;">\&quot;</span>http://www.w3.org/1999/xhtml<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                
        rootChildren.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        HtmlBody body <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HtmlBody<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        rootChildren.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>body<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        HtmlForm form <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HtmlForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        body.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        message <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HtmlOutputText<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        form.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        HtmlCommandButton actionButton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HtmlCommandButton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        actionButton.<span style="color: #006633;">addActionListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        actionButton.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Do action&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        form.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>actionButton<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        output <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UIOutput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        output.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;/html&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        rootChildren.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> processAction<span style="color: #009900;">&#40;</span><span style="color: #003399;">ActionEvent</span> event<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> AbortProcessingException <span style="color: #009900;">&#123;</span>
        message.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hello, world&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>    
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To run this on Glassfish, only this class and the small (8,980 bytes) javavdl.jar library are required:</p>
<p><a href="http://jdevelopment.nl/wp-content/uploads/2011/09/single_app_demo_ws.png"><img src="http://jdevelopment.nl/wp-content/uploads/2011/09/single_app_demo_ws.png" alt="" title="Single app demo workspace" width="215" height="206" class="alignnone size-full wp-image-1042" /></a></p>
<p>After requesting localhost:8080/demo/intro.jsf, a single button will be shown. The message &#8220;hello, world&#8221; will be displayed when this button is clicked. </p>
<p>Interesting to note perhaps is that the components themselves only exist for the duration of the request and after every post-back the component tree will be re-created. State set on the components during <em>buildView</em> will be marked as the so-called <em>initial state</em> and will typically not be taken into account by JSF&#8217;s state saving mechanism. Since only the <em>HtmlCommandButton</em> in the example code references the Page instance after this <em>buildView</em> it will effectively have request scope.</p>
<p>The java based vdl is still rather simple, so it&#8217;s still just a proof of concept. I&#8217;ve planned some enhancements that I&#8217;ll work on soon if time permits (e.g. perhaps support for scopes other than request scope without using EL). Stay tuned!</p>
<p><i>Arjan Tijms</i></p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/single-class-pure-java-jsf-application/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Authoring JSF pages in pure Java</title>
		<link>http://jdevelopment.nl/authoring-jsf-pages-pure-java/</link>
		<comments>http://jdevelopment.nl/authoring-jsf-pages-pure-java/#comments</comments>
		<pubDate>Mon, 12 Sep 2011 10:37:10 +0000</pubDate>
		<dc:creator>development</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jsf]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=986</guid>
		<description><![CDATA[JSF is well known as a server side web framework to build a web application&#8217;s UI with the help of components. For the overwhelming majority of users those components are represented by the tags one uses to compose a page, be it via JSP or Facelets. The following is a typical example of a page [...]]]></description>
			<content:encoded><![CDATA[<p>JSF is well known as a server side web framework to build a web application&#8217;s UI with the help of components. For the overwhelming majority of users those components are represented by the tags one uses to compose a page, be it via JSP or Facelets.</p>
<p>The following is a typical example of a page authored with Facelets:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> <span style="color: #000066;">xmlns:h</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/html&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>    
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>   
       #{backingBean.hello}   
        <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>      
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:inputText</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{backingBean.name}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>                    
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:commandButton</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Say Hello&quot;</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;#{backingBean.sayHello}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</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>        
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>This may give the impression that JSF components <em>*are*</em> the tags shown above, and thus that JSF is necessarily about tags and XML. That is however not true. </p>
<p>JSF has always had a clear separation between the technology used to author pages (the so-called <em>view description language</em>) and the actual API to create components and compose them together into a component tree. This API is a Java API that even in the very first days of JSF had no knowledge whatsoever about JSP or tags. The strict separation allowed alternative view declaration languages like Facelets to be created and eventually be promoted to the default VDL, without needing to change the underlying JSF core framework. </p>
<p>Other view declaration languages have been created as well. One notable example is a concept based on JavaFX, in which a JSF page would look like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">var x <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
var bindVal <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello&quot;</span><span style="color: #339933;">;</span>
&nbsp;
function init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   FxPage<span style="color: #009900;">&#123;</span>
      content <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
         FxForm<span style="color: #009900;">&#123;</span>
                content <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
                    FxOutputLabel<span style="color: #009900;">&#123;</span>
                        value <span style="color: #339933;">:</span> bind bindVal
                    <span style="color: #009900;">&#125;</span>,
                    FxCommandButton<span style="color: #009900;">&#123;</span>
                        value <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Button&quot;</span>
                        actionListener <span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#123;</span>
                            bindVal <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello {x++}&quot;</span><span style="color: #339933;">;</span>
                            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#93;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><small><em>See: <a href="http://mkblog.exadel.com/2010/02/javafx-as-jsf-vdl-view-description-language/">JavaFX as JSF VDL (View Description Language)?</a></em></small></p>
<p>As it appears, the API to create components and compose a component tree is pretty accessible by itself. It could theoretically be used directly to author pages instead of solely being used as an API for VDL implementations. The problem however is that it&#8217;s not clear how to actually start doing that. One trick is to use <em>dynamic tree manipulation</em> and add components in a <em>PreRenderView</em> event on an otherwise empty page.</p>
<p>Another way is to create a minimal VDL implementation. As a proof of concept, that&#8217;s what I did. With such a thing it&#8217;s possible to create a JSF application completely in Java. No Facelets or JSP files, and following <a href="http://jdevelopment.nl/minimal-3tier-java-ee-app-xml-config/">Minimal 3-tier Java EE app, without any XML config</a>, no web.xml or faces-config.xml is required either. There doesn&#8217;t even have to be a WEB-INF or WebContent directory in the project.</p>
<p>The following shows a JSF page being defined in pure Java:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Intro <span style="color: #000000; font-weight: bold;">implements</span> Page <span style="color: #009900;">&#123;</span>
&nbsp;
    @<span style="color: #003399; font-weight: bold;">Override</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> buildView<span style="color: #009900;">&#40;</span>FacesContext context, UIViewRoot root<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">IOException</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        ELContext elContext = context.<span style="color: #006633;">getELContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ExpressionFactory expressionFactory = context.<span style="color: #006633;">getApplication</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getExpressionFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399; font-weight: bold;">List</span><span style="color: #339933;">&lt;</span>UIComponent<span style="color: #339933;">&gt;</span> rootChildren = root.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        UIOutput output = <span style="color: #000000; font-weight: bold;">new</span> UIOutput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        output.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;html xmlns=<span style="color: #000099; font-weight: bold;">\&quot;</span>http://www.w3.org/1999/xhtml<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                
        rootChildren.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        HtmlBody body = <span style="color: #000000; font-weight: bold;">new</span> HtmlBody<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        rootChildren.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>body<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        HtmlForm form = <span style="color: #000000; font-weight: bold;">new</span> HtmlForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        body.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        ValueExpression messageProperty = expressionFactory.<span style="color: #006633;">createValueExpression</span><span style="color: #009900;">&#40;</span>elContext, <span style="color: #0000ff;">&quot;#{myBean.message}&quot;</span>, <span style="color: #003399; font-weight: bold;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        HtmlOutputText message = <span style="color: #000000; font-weight: bold;">new</span> HtmlOutputText<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        message.<span style="color: #006633;">setValueExpression</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value&quot;</span>, messageProperty<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        form.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        MethodExpression helloMethod = expressionFactory.<span style="color: #006633;">createMethodExpression</span><span style="color: #009900;">&#40;</span>elContext, <span style="color: #0000ff;">&quot;#{myBean.action}&quot;</span>, <span style="color: #003399; font-weight: bold;">Void</span>.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        HtmlCommandButton hiCommand = <span style="color: #000000; font-weight: bold;">new</span> HtmlCommandButton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        hiCommand.<span style="color: #006633;">setActionExpression</span><span style="color: #009900;">&#40;</span>helloMethod<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        hiCommand.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Say hello&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        form.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>hiCommand<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        MethodExpression navigateMethod = expressionFactory.<span style="color: #006633;">createMethodExpression</span><span style="color: #009900;">&#40;</span>elContext, <span style="color: #0000ff;">&quot;#{myBean.navigate}&quot;</span>, <span style="color: #003399; font-weight: bold;">String</span>.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        HtmlCommandButton navigateCommand = <span style="color: #000000; font-weight: bold;">new</span> HtmlCommandButton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        navigateCommand.<span style="color: #006633;">setActionExpression</span><span style="color: #009900;">&#40;</span>navigateMethod<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        navigateCommand.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Do navigation&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        form.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>navigateCommand<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        output = <span style="color: #000000; font-weight: bold;">new</span> UIOutput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        output.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;/html&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        rootChildren.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>    
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This will be rendered as follows:</p>
<p><a href="http://jdevelopment.nl/wp-content/uploads/2011/09/intro.png"><img src="http://jdevelopment.nl/wp-content/uploads/2011/09/intro.png" alt="" title="intro" width="318" height="103" class="aligncenter size-full wp-image-1016" /></a></p>
<p>The code shows how components are created simply by using the new operator and a tree is created by just inserting the child components in the children collections of their parents. Action listeners are registered on action source components by setting a method expression that points to the scoped backing bean &#8220;myBean&#8221;, which is defined as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="java5" style="font-family:monospace;">@ManagedBean
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyBean <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399; font-weight: bold;">String</span> message<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> action<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        message = <span style="color: #0000ff;">&quot;Hi!&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; font-weight: bold;">String</span> navigate<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> redirectOutcome<span style="color: #009900;">&#40;</span>OtherPage.<span style="color: #000000; font-weight: bold;">class</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; font-weight: bold;">String</span> getMessage<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> message<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>    
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Note that the bean features a navigation method that uses the class type of the target page to determine the navigation outcome.</p>
<p>By putting stuff in base classes or utility methods, page implementations can be simplified. For instance, the following shows the page where the user is navigated to after pushing the button on the first page. It inherits from a base class that already defines the html and outer body part, and only implements the body content:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> OtherPage <span style="color: #000000; font-weight: bold;">extends</span> BodyTemplate <span style="color: #009900;">&#123;</span>
&nbsp;
    @<span style="color: #003399; font-weight: bold;">Override</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> buildBody<span style="color: #009900;">&#40;</span>FacesContext context, HtmlBody body<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>    
        HtmlOutputText output = <span style="color: #000000; font-weight: bold;">new</span> HtmlOutputText<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        output.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This is a new page.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        body.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This page is rather simple and is rendered as:</p>
<p><a href="http://jdevelopment.nl/wp-content/uploads/2011/09/otherpage.png"><img src="http://jdevelopment.nl/wp-content/uploads/2011/09/otherpage.png" alt="" title="otherpage" width="383" height="100" class="aligncenter size-full wp-image-1018" /></a></p>
<p>As shown by the screen shots, if the project is called <em>javavdl</em> the initial page can be requested by requesting <em>http://localhost/javavdl/intro.jsf</em>, which is fully implemented using the <em>Intro.java</em> class given above. After clicking the navigate button, the user will be redirected to <em>http://localhost/javavdl/test/otherPage.jsf</em>, which is fully implemented by <em>OtherPage.java</em>. For this simple proof of concept, classes implementing pages have to reside in the package <em>resources.javavdl.pages</em>. Sub-packages in that package become paths relative to the deployment root of the application. E.g. <em>/test/otherPage.jsf</em> is implemented by <em>resources.javavdl.pages.test.OtherPage</em>.</p>
<p>The following is a picture of my workspace, showing that there is absolutely not a single xml file:</p>
<p><a href="http://jdevelopment.nl/wp-content/uploads/2011/09/javavdl_workspace.png"><img src="http://jdevelopment.nl/wp-content/uploads/2011/09/javavdl_workspace.png" alt="" title="javavdl workspace" width="383" height="510" class="aligncenter size-full wp-image-1006" /></a></p>
<p>If this were used for real, the &#8220;src&#8221; package would probably be in a jar, and the application would consist of nothing more than this jar and the four classes shown in the demo package.</p>
<p>The current setup is really just a proof of concept. I personally think a declarative approach like Facelets is actually clearer, although some people actually do seem to like a purely programmatic approach to building a UI.</p>
<p>If people want to tinker with this, I&#8217;ve shared my Eclipse project where I implemented this here: <a href="http://code.google.com/p/javavdl">http://code.google.com/p/javavdl</a> (it currently depends on the Glassfish WTP server adapter being installed).</p>
<p><i>Arjan Tijms</i></p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/authoring-jsf-pages-pure-java/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Simple Java based JSF custom component</title>
		<link>http://jdevelopment.nl/simple-java-based-jsf-custom-component/</link>
		<comments>http://jdevelopment.nl/simple-java-based-jsf-custom-component/#comments</comments>
		<pubDate>Sun, 04 Sep 2011 22:28:17 +0000</pubDate>
		<dc:creator>development</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jsf]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=975</guid>
		<description><![CDATA[Even though in JSF components have always been a central thing, actually creating them required a ridiculous amount of tedious work in JSF 1.x. Not only did you had to create the actual component, it also required you to: Register the component in an XML file Create a tag handler class, where you referenced the [...]]]></description>
			<content:encoded><![CDATA[<p>Even though in JSF components have always been a central thing, actually creating them required a ridiculous amount of tedious work in JSF 1.x. Not only did you had to create the actual component, it also required you to:</p>
<ul>
<li>Register the component in an XML file</li>
<li>Create a tag handler class, where you referenced the component by its registered name</li>
<li>Register the tag in .tld file.</li>
</ul>
<p>You were also more or less supposed to create a separate renderer class, and although this actually has always been optionally, people seemed to interpret it as being a required part as well. Although none of this was really difficult, the sheer amount of work prevented everyone but component library builders from creating components.</p>
<p>JSF 2.0 addressed all of these concerns by introducing <a href="http://java.sun.com/javaee/6/docs/tutorial/doc/giqzr.html" title="Composite Components">composite components</a>, where you are able to create first-class components just by putting some existing components and/or markup in a Facelets file. For the majority of use cases where application builders needed their own customized components, this is actually all that&#8217;s needed.</p>
<p>For some cases however a Java based component is still a more natural fit. Although not as simple to create as composite components, creating Java based custom components has actually been simplified as well. Of the steps outlined above, only a registration for the tag is still required. The actual component can be rather simple:</p>
<p><strong>components/CustomComponent.java</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java5" style="font-family:monospace;">@FacesComponent<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;components.CustomComponent&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> CustomComponent <span style="color: #000000; font-weight: bold;">extends</span> UIComponentBase <span style="color: #009900;">&#123;</span>
&nbsp;
    @<span style="color: #003399; font-weight: bold;">Override</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399; font-weight: bold;">String</span> getFamily<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;my.custom.component&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @<span style="color: #003399; font-weight: bold;">Override</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> encodeBegin<span style="color: #009900;">&#40;</span>FacesContext context<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">IOException</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399; font-weight: bold;">String</span> value = <span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span><span style="color: #009900;">&#41;</span> getAttributes<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>
&nbsp;
        <span style="color: #000000;  font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>value <span style="color: #339933;">!</span>= <span style="color: #006600; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>        
            ResponseWriter writer = context.<span style="color: #006633;">getResponseWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            writer.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>value.<span style="color: #006633;">toUpperCase</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>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Unfortunately, a registration in an XML file is still required to use this as a tag:</p>
<p><strong>META-INF/my.taglib.xml</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;facelet-taglib</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.0&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;namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://example.com/test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tag<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tag-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>customComponent<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tag-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;component<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;component-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>components.CustomComponent<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/component-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/component<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tag<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/facelet-taglib<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The custom component can now be used on a Facelet as follows:</p>
<p><strong>page.xhtml</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:h</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/html&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:test</span>=<span style="color: #ff0000;">&quot;http://example.com/test&quot;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>   		
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;test:customComponent</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;test&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>        
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>As shown, creating a Java based JSF component that simply renders something is not that complicated. Although composite components should generally be preferred, Java based ones can come in handy and it&#8217;s an important extra tool.</p>
<p>It&#8217;s a shame perhaps the XML registration is still needed, but there&#8217;s a <a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-594" title="Add tagName attribute to @FacesComponent annotation">feature request in the JSF spec JIRA</a> to make it largely unnecessary in a future JSF version. (if you care for this, please vote for it) Hopefully the actual Java code can also be further simplified if the family and component name would be defaulted in a future jSF version (the last e.g. to the fully qualified class name).</p>
<p><br/></p>
<p><i>Arjan Tijms</i> </p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/simple-java-based-jsf-custom-component/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Minimal 3-tier Java EE app, without any XML config</title>
		<link>http://jdevelopment.nl/minimal-3tier-java-ee-app-xml-config/</link>
		<comments>http://jdevelopment.nl/minimal-3tier-java-ee-app-xml-config/#comments</comments>
		<pubDate>Sun, 21 Aug 2011 17:08:20 +0000</pubDate>
		<dc:creator>development</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jsf]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=935</guid>
		<description><![CDATA[Older versions of Java EE and Java frameworks in general were rather heavy with regard to required XML for configuration. Notorious were EJB2, Spring 2.x, JSF 1.x, Servlet 2.5 and many more. These days things have improved quite a lot. In this post I&#8217;ll demonstrate a very simple, yet 3-tiered, Hello word application for Java [...]]]></description>
			<content:encoded><![CDATA[<p>Older versions of Java EE and Java frameworks in general were rather heavy with regard to required XML for configuration. Notorious were EJB2, Spring 2.x, JSF 1.x, Servlet 2.5 and many more.</p>
<p>These days things have improved quite a lot. In this post I&#8217;ll demonstrate a very simple, yet 3-tiered, <em>Hello word</em> application for Java EE. It uses JSF 2.1 and EJB 3.1 and absolutely not a single line of XML configuration.</p>
<p>The entire application consists of just 3 files: <em>page.xhtml</em>, <em>BackingBean.java</em> and <em>BusinessBean.java</em>. To run the application, create a dynamic web project called &#8216;mytest&#8217; in Eclipse (EE edition) and delete everything that&#8217;s generated by default in WebContent. Copy page.xhtml to WebContent and copy both .java files to src. Add the app to your server (e.g. GlassFish 3.1) and browse to localhost:8080/mytest/page.jsf to see all the action.</p>
<p>The following shows the code of the 3 files involved:</p>
<p style="margin-bottom: 0in;"><strong>page.xhtml</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:h</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/html&quot;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&gt;</span></span>    
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
       #{backingBean.hello}
&nbsp;
        <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>      
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:inputText</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{backingBean.name}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>                    
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:commandButton</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Say Hello&quot;</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;#{backingBean.sayHello}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</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>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p style="margin-bottom: 0in;"><strong>BackingBean.java</strong></p?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ejb.EJB</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.bean.ManagedBean</span><span style="color: #339933;">;</span>
&nbsp;
@ManagedBean
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BackingBean <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #003399; font-weight: bold;">String</span> name<span style="color: #339933;">;</span>
    <span style="color: #003399; font-weight: bold;">String</span> hello<span style="color: #339933;">;</span>
&nbsp;
    @EJB
    BusinessBean businessBean<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> sayHello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        hello = businessBean.<span style="color: #006633;">generateHello</span><span style="color: #009900;">&#40;</span>name<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; font-weight: bold;">String</span> getName<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> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span> name<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;">name</span> = name<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; font-weight: bold;">String</span> getHello<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> hello<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p style="margin-bottom: 0in;"><strong>BusinessBean.java</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ejb.Stateless</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> BusinessBean <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399; font-weight: bold;">String</span> generateHello<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399; font-weight: bold;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello, %s!&quot;</span>, name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>    
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The following shows that the project in Eclipse only has the 3 mentioned files:</p>
<p><a href="http://jdevelopment.nl/wp-content/uploads/2011/08/eclipse_project_explorer.png"><img src="http://jdevelopment.nl/wp-content/uploads/2011/08/eclipse_project_explorer.png" alt="" title="eclipse project explorer" width="226" height="231" class="aligncenter size-full wp-image-946" /></a></p>
<p>The deployment itself also consists of only the 3 files mentioned (no IDE generated magic), although one extra step has been performed and that&#8217;s that the .java classes have been compiled to their .class versions and have been put into a newly generated directory <em>WEB-INF/classes</em>:</p>
<p><a href="http://jdevelopment.nl/wp-content/uploads/2011/08/glassfish_deployment1.png"><img src="http://jdevelopment.nl/wp-content/uploads/2011/08/glassfish_deployment1.png" alt="A simple exploded Java EE application deployment to GlassFish" title="glassfish deployment" width="318" height="364" class="size-full wp-image-951" /></a><br/><br/></p>
<p>Note the use of a couple of conventions that make this all work:</p>
<ul>
<li>The context root is by default the name of the directory in which the app is deployed (technically: the exploded war name).</li>
<li>JSF by default maps the JSF controller servlet to *.jsf (and to /faces/* and *.faces).</li>
<li>Facelets files have a default suffix of .xhtml.</li>
<li>The backing bean&#8217;s default name in EL is the simple class name with the first letter de-capitalized.</li>
<li>The backing bean is by default in request scope.</li>
<li>The bi-directional binding from the view (page.xhtml) to the model via the EL expression #{backingBean.name} takes advantage of the JavaBean naming conventions for getters/setters.</li>
<li>The EJB is by default pooled and transactional.</li>
<li>A no-interface view is created for the EJB by default (since it doesn&#8217;t implement business interfaces).</li>
</ul>
<p>Everything that is defaulted can be configured differently as needed. For instance, the default name in EL for the backing bean can be set to a different name via an attribute on the <em>ManagedBean</em> annotation, and the EJB can be made not transactional via an extra annotation. Some things would require XML to be configured differently. For instance, alternative mappings for the Faces Servlet or the default Facelets file suffix have to be configured in a web.xml file.</p>
<p>Finally, note that the goal of this example is to demonstrate one of the simplest possible setups and doesn&#8217;t necessarily adheres to all best practices. In reality, for non-trivial applications you&#8217;d want to declare you instance variables as private, definitely would not use the default package for your classes, probably create separate packages for your backing and business code, make sure *.xhtml is not directly accessible (e.g. by mapping the Faces Servlet directly to .xhtml in web.xml), and if your application really grows you might want to consider putting the business code in a separate module, add interfaces to the beans, etc etc.</p>
<p>But the point is that Java EE (and various other Java frameworks as well), now allow you to start with very simple defaults, making the barrier to enter much lower than before. You can then refactor and configure when the need arrises.</p>
<p><br/></p>
<p><i>Arjan Tijms</i> </p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/minimal-3tier-java-ee-app-xml-config/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Stateless vs Stateful JSF view parameters</title>
		<link>http://jdevelopment.nl/stateless-stateful-jsf-view-parameters/</link>
		<comments>http://jdevelopment.nl/stateless-stateful-jsf-view-parameters/#comments</comments>
		<pubDate>Sun, 03 Jul 2011 17:24:23 +0000</pubDate>
		<dc:creator>development</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jsf]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=900</guid>
		<description><![CDATA[JSF defines the concept of view parameters, which are used to support URL parameters in GET requests (although it can be used for non-faces POST parameters as well). View parameters are defined on a Facelet with the help of the f:viewParam tag. A typical usage looks like this: &#60;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" &#62; &#60;f:metadata&#62; &#60;f:viewParam [...]]]></description>
			<content:encoded><![CDATA[<p>JSF defines the concept of view parameters, which are used to support URL parameters in GET requests (although it can be used for non-faces POST parameters <a href="http://stackoverflow.com/questions/4579711/managedpropertyvalue-param-id-in-a-non-request-scope-bean/4579914#4579914">as well</a>).</p>
<p>View parameters are defined on a Facelet with the help of the <a href="http://javaserverfaces.java.net/nonav/docs/2.0/pdldocs/facelets/f/viewParam.html">f:viewParam</a> tag. A typical usage looks like this:</p>
<p><code></p>
<pre>&lt;html xmlns="http://www.w3.org/1999/xhtml"
     xmlns:h="http://java.sun.com/jsf/html"
     xmlns:f="http://java.sun.com/jsf/core"
 &gt;
    &lt;f:metadata&gt;
        &lt;f:viewParam name="foo_id" value="#{myBean.fooId}" /&gt;
    &lt;/f:metadata&gt;

    &lt;h:body&gt;
        #{myBean.fooId}
    &lt;/h:body&gt;
&lt;/html&gt;</pre>
<p></code></p>
<p>In this case myBean could look like this:</p>
<p><code></p>
<pre>@ManagedBean
@ViewScoped
public class MyBean {
    private Long fooId;

    public Long getFooId() {
        return fooId;
    }

    public void setFooId(Long fooId) {
        this.fooId = fooId;
    }
}</pre>
<p></code><br />
There&#8217;s nothing really fancy going on here. When a GET request like <em>localhost:8080/mypage?fooId=1</em> is processed, <em>setFooId()</em> is called, and when the page is rendered <em>getFooId()</em> is called. When we fire a GET request without the URL parameter, the setter will not be called. So, it looks like the presence of the URL parameter is what triggers the setter.</p>
<p>But what happens after we do a <a href="http://en.wikipedia.org/wiki/Postback">postback</a> from that page? E.g. when we add the following to the Facelet:</p>
<p><code>
<pre>
&lt;h:form>
    &lt;h:commandButton value="do action" action="#{myBean.doAction"}"/>
&lt;/h:form>
</pre>
<p></code></p>
<p>and the following to the bean:</p>
<p><code>
<pre>
public void doAction() {
}
</pre>
<p></code></p>
<p>There will be no URL parameter present in the request after the postback, so it would seem logical the setter will not be called. </p>
<p>As it appears however, the setter <strong>will</strong> be called. What happens is that f:viewParam (UIViewParameter) is a <strong>stateful</strong> component. When it initially retrieves the URL parameter <em>foo_id</em>, it internally stores this in its view state. After the postback, the value is processed again and then also pushed into the model again (the backing bean). This is a direct consequence of the fact that in JSF a view is normally stateful and this behavior is actually consistent with how other UIInput components (like e.g. h:inputText) work. In case a backing bean is request scoped, this can also be really convenient as otherwise the initial URL parameter would be &#8216;lost&#8217; after the first postback. See e.g. <a href="http://www.jakobk.com/2010/04/jsf-2-0-view-parameters/">JSF 2.0: View parameters</a>, where the author seems really happy with this behavior:</p>
<blockquote><p>The UIViewParameter component also stores its value in the state, so that the value will also be available on the next postback request (most likely a clicked button or link). So you only need to provide your view parameter(s) once and not on every request.</p></blockquote>
<p>Unfortunately, there are a couple of situations where a stateful f:viewParam can be a real nuisance. In the example given above the backing bean is already view scoped and takes care of managing its own state. Calling the setter again is simply redundant, but doesn&#8217;t hurt much. Even if the backing bean loads data from a DB based on this parameter, it probably does so in the preRenderView event handler with a guard that checks if the request is not a postback:</p>
<p><code>
<pre>
public void onPreRenderView() {
    if (!FacesContext.getCurrentInstance().isPostback()) {
        foo = fooDao.getByID(fooId);
    }
}
</pre>
<p></code></p>
<p>However, things get a little bit problematic when taking advantage of one of the killer features of view parameters: the ability to attach converters and validators to them. Instead of letting the backing bean load the <em>Foo</em> instance, we can use a universal converter for this and while at it validate right away that the foo_id is legal to be used by the current user (thus preventing parameter twiddling attacks). The code would then look like this:</p>
<p><code></p>
<pre>&lt;html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
 &gt;
    &lt;f:metadata&gt;
        &lt;f:viewParam name="foo_id" label="foo_id" value="#{myBean.foo}" converter="fooConverter" validator="fooOwnerValidator"  /&gt;
    &lt;/f:metadata&gt;

    &lt;h:body&gt;
        #{myBean.foo.id}

        &lt;h:form>
            &lt;h:commandButton value="do action" action="#{myBean.doAction"}"/>
        &lt;/h:form>
    &lt;/h:body&gt;
&lt;/html&gt;</pre>
<p></code></p>
<p>and</p>
<p><code></p>
<pre>@ManagedBean
@ViewScoped
public class MyBean {
    private Foo foo;

    public Foo getFoo() {
        return foo;
    }

    public void setFoo(Foo foo) {
        this.foo = foo;
    }

    public void doAction() {
    }
}</pre>
<p></code></p>
<p>With f:viewParam being stateful, it will now do the conversion and validation on <strong>each and every postback</strong>. This is not only unnecessary (as the correct <em>Foo</em> instance is already safely kept in the view scoped <em>MyBean</em>), but it&#8217;s also a needless drain on system resources and a potential slow down for this view as it may involve a DB query for each and every postback.</p>
<p>So what we need for this particular usecase is an f:viewParam variant that doesn&#8217;t do this needless converting every time. There are a few options:</p>
<ol>
<li>Create a stateless version</li>
<li>Only do conversion and model updates when value changes (suggested by my co-worker Bauke Scholtz)</li>
<li>Also put converted value in state, always update model, reconvert when original changes</li>
</ol>
<p>For this article I looked at the first option, as it seemed to be the easiest one to implement. The original UIViewParameter handles its state via the following methods (Mojarra 2.1.1):</p>
<p><code>
<pre>
@Override
public String getSubmittedValue() {
    return (String) getStateHelper().get(PropertyKeys.submittedValue);
}
public void setSubmittedValue(Object submittedValue) {
    getStateHelper().put(PropertyKeys.submittedValue, submittedValue);
}
</pre>
<p></code></p>
<p>In order to make a <strong>stateless variant</strong>, I simply inherited and provided alternative implementations for these two methods:</p>
<p><code>
<pre>
@FacesComponent("com.my.UIStatelessViewParameter")
public class UIStatelessViewParameter extends UIViewParameter {

    private String submittedValue;

    @Override
    public void setSubmittedValue(Object submittedValue) {
        this.submittedValue = (String)submittedValue;
    }

    @Override
    public String getSubmittedValue() {
        return submittedValue;
    }
}
</pre>
<p></code></p>
<p>I then created a new tag for this component in a facelet-taglib file (e.g. my-taglib.xml in META-INF in a jar):</p>
<p><code>
<pre>
&lt;namespace>http://my.com/test&lt;/namespace>
&lt;tag>
    &lt;tag-name>viewParam&lt;/tag-name>
    &lt;component>
        &lt;component-type>com.my.UIStatelessViewParameter&lt;/component-type>
    &lt;/component>
&lt;/tag>
</pre>
<p></code></p>
<p>After this I can use this stateless variant on my Facelets:</p>
<p><code></p>
<pre>&lt;html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:my="http://my.com/test"
 &gt;
    &lt;f:metadata&gt;
        &lt;my:viewParam name="foo_id" label="foo_id" value="#{myBean.foo}" converter="fooConverter" validator="fooOwnerValidator"  /&gt;
    &lt;/f:metadata&gt;

    &lt;h:body&gt;
        #{myBean.foo.id}

        &lt;h:form>
            &lt;h:commandButton value="do action" action="#{myBean.doAction"}"/>
        &lt;/h:form>
    &lt;/h:body&gt;
&lt;/html&gt;</pre>
<p></code></p>
<p>I can now do as many postbacks as I want, but the (expensive) conversion only happens once when the URL parameter is actually in the request. Exactly as we wanted!</p>
<p>There are two drawbacks though. The first is the fact that <a href="http://java.dzone.com/articles/bookmarkability-jsf-2?page=0,1">automatically generating a bookmarkable link</a> that includes the original URL parameters via the <em>includeViewParams</em> attribute of e.g. h:link does not work anymore. The second is that if the <em>required</em> attribute has been set to true, then this will now complain the value is missing on a postback. This can be resolved by adding a #{!facesContext.postback} expression to this attribute, but this is not a very elegant solution.</p>
<p>To solve these drawbacks I&#8217;ll have to look into the method suggested by my co-worker Bauke and this might be the topic of a followup article.</p>
<p><strong>Update:</strong></p>
<p>A simple solution to avoid having to add the #{!facesContext.postback} at every place is to add the following method to the <em>UIStatelessViewParameter</em> class given above.</p>
<p><code>
<pre>
@Override
public boolean isRequired() {
    // The request parameter get lost on postbacks, however it's already present in the view scoped bean.
    // So we can safely skip the required validation on postbacks.
    return !FacesContext.getCurrentInstance().isPostback() &#038;&#038; super.isRequired();
}
</pre>
<p></code></p>
<p>Thanks goes to Bauke for coming up with this <img src='http://jdevelopment.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><i>Arjan Tijms</i></p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/stateless-stateful-jsf-view-parameters/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

