<?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, 17 May 2012 07:29:22 +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>Fetching arbitrary object graphs in JPA 2</title>
		<link>http://jdevelopment.nl/fetching-arbitrary-object-graphs-jpa-2/</link>
		<comments>http://jdevelopment.nl/fetching-arbitrary-object-graphs-jpa-2/#comments</comments>
		<pubDate>Wed, 25 Apr 2012 15:12:05 +0000</pubDate>
		<dc:creator>development</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=1443</guid>
		<description><![CDATA[In Java EE, JPA (Java Persistence API) is used to store and retrieve graphs of objects. This works by specifying relations between objects via annotations (or optionally XML). Hand over the root of an object graph to the entity manager and it will persist it. Ask the entity manager for an object with a given [...]]]></description>
			<content:encoded><![CDATA[<p>In Java EE, JPA (Java Persistence API) is used to store and retrieve graphs of objects. This works by specifying relations between objects via annotations (or optionally XML). Hand over the root of an object graph to the <em>entity manager</em> and it will persist it. Ask the <em>entity manager</em> for an object with a given Id and you&#8217;ll get the graph back.</p>
<p>This is all fine and well, but how in this model do we control which branches of the graph are retrieved and to which depth branches should be followed?</p>
<p>The primary mechanism to control this with is the eager/lazy mechanism. Mark a relation as eager and JPA will fetch it upfront, mark it as lazy and it will dynamically fetch it when the relation is traversed. In practice, both approaches have their cons and pros. Mark everything eager and you&#8217;ll risk pulling in the entire DB for every little bit of data that you need. Mark everything lazy, and you&#8217;ll not only have to keep the persistence context around (which by itself can be troublesome), but you also risk running into the 1 + N query problem (1 base query is fired, and then an unknown amount of N queries when iterating over its relations). If fetching 1000 items in one query took approximately as long as fetching 1 item per query and firing 1000 queries, then this wouldn&#8217;t be a problem. Unfortunately, for a relational database this is not the case, not even when using heaps of memory and tons of <a href="http://jdevelopment.nl/one-dvd-per-second" title="One DVD per second" target="_blank">fast SSDs in RAID</a>.</p>
<p>There are various ways to overcome this. For instance there are proprietary mechanisms for setting the batch size, so not 1000 queries are fired but 10. We could also assume that all entities relating to those 1000 items are all in the (JPA) cache. Then 1000 fetches of 1 entity are indeed about as costly as 1 fetch of 1000 entities, but this is a dangerous assumption. Assume wrong and you might bring down your DB.</p>
<p>The fundamental problem however is that eager/lazy are static properties of the entity model. In practice, the part of the graph that you want often depends on the use case. For a master overview of all <em>User</em>s in a system, you&#8217;d probably want a rather shallow graph, but for the detail view of a particular <em>User</em> you most likely need a somewhat deeper one.</p>
<p>Again, there are various solutions for this. One is to write individual JPQL queries for each use case. This certainly works, but the number of queries can grow rapidly out of hand this way (<em>allUsersWithAddress</em>, <em>allUsersWithAddressAndFriends</em>, <em>allUsersWithAddressAndFriendsWithAddress</em> , &#8230;). Another solution that addresses exactly this problem are the <a href="http://rossenstoyanchev.org/blog/2009/09/02/fetch-profiles-in-hibernate-35/" title="Fetch Profiles In Hibernate 3.5" target="_blank">fetch profiles</a> that were introduced in Hibernate 3.5. As can be seen in the <a href="http://docs.jboss.org/hibernate/core/4.1/manual/en-US/html/ch21.html#performance-fetching-profiles" target="_blank">official documentation</a>, this solution is not particularly JPA friendly. You need access to the native Hibernate session, which is possible, but not pretty. One way or the other, fetch profiles are Hibernate specific.</p>
<p>In this posting I would like to present an alternative solution. It feels a little like fetch profiles, but the graph to be fetched can be specified dynamically and it uses the JPA API only. It works by using the criteria API to programmatically add one or more JOIN FETCH clauses to a query. Unfortunately JPA does not yet have the capabilities to turn a JPQL query into a Criteria query, so either the query must already be a Criteria or it should be a simple find. The following code demonstrates the latter case:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">@Stateless
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeDAO <span style="color: #009900;">&#123;</span>
&nbsp;
    @PersistenceContext
    <span style="color: #000000; font-weight: bold;">private</span> EntityManager entityManager<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> T findWithDepth<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Class</span><span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> type, <span style="color: #003399; font-weight: bold;">Object</span> id, <span style="color: #003399; font-weight: bold;">String</span>... <span style="color: #006633;">fetchRelations</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        CriteriaBuilder criteriaBuilder = entityManager.<span style="color: #006633;">getCriteriaBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        CriteriaQuery<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> criteriaQuery = criteriaBuilder.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Root<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> root = criteriaQuery.<span style="color: #006633;">from</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000;  font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span> relation : fetchRelations<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            FetchParent<span style="color: #339933;">&lt;</span>T, T<span style="color: #339933;">&gt;</span> fetch = root<span style="color: #339933;">;</span>
            <span style="color: #000000;  font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span> pathSegment : relation.<span style="color: #006633;">split</span><span style="color: #009900;">&#40;</span>quote<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.&quot;</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>
                fetch = fetch.<span style="color: #006633;">fetch</span><span style="color: #009900;">&#40;</span>pathSegment, JoinType.<span style="color: #006633;">LEFT</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;
        criteriaQuery.<span style="color: #006633;">where</span><span style="color: #009900;">&#40;</span>criteriaBuilder.<span style="color: #006633;">equal</span><span style="color: #009900;">&#40;</span>root.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span>, id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> getSingleOrNoneResult<span style="color: #009900;">&#40;</span>entityManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>criteriaQuery<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> T getSingleOrNoneResult<span style="color: #009900;">&#40;</span>TypedQuery<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> query<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>        
        query.<span style="color: #006633;">setMaxResults</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399; font-weight: bold;">List</span><span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> result = query.<span style="color: #006633;">getResultList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000;  font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>result.<span style="color: #006633;">isEmpty</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;">return</span> <span style="color: #006600; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> result.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</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></div></div>

<p>The <em>findWithDepth</em> method can now be called with one or more path expressions, where each path is just a chain of properties separated by a dot (like in expression language). E.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">User user = someDao.<span style="color: #006633;">findWithDepth</span><span style="color: #009900;">&#40;</span>
    User.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #cc66cc;">15</span>, <span style="color: #0000ff;">&quot;addresses&quot;</span>, <span style="color: #0000ff;">&quot;friends.addresses&quot;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The above line would fetch the user with &#8220;id&#8221; 15, and pre-fetches the addresses associated with that user, as well as the friends and their addresses. (Note that the @Id field is hardcoded to be called &#8220;id&#8221; here. A more fancy implementation could query the object for it)</p>
<p>This solution, though handy, is however not perfect. While all JPA vendors support fetching multiple relations of one level deep (<em>addresses</em> and <em>friends</em> in the example above), not all of them support fetching chained relations (<em>friends.addresses</em> in the example above). Specifically for Hibernate care should be taken to avoid fetching so-called &#8220;multiple bags&#8221; (sets and @OrderColumn are a typical solution). Of course it&#8217;s always wise to avoid creating a huge Cartesian product, which is unfortunately one low-level effect of the underlying relational DB you have to be aware of, even when purely dealing with object graphs.</p>
<p>Despite the problems I outlined with this approach above, I hope it&#8217;s still useful to someone. Thanks go to my co-workers Jan Beernink and Hongqin Chen for coming up with the original idea respectively refining it.</p>
<p><i>Arjan Tijms</i></p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/fetching-arbitrary-object-graphs-jpa-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Hibernate&#8217;s &#8220;Pure native scalar queries are not yet supported&#8221;</title>
		<link>http://jdevelopment.nl/hibernates-pure-native-scalar-queries-supported/</link>
		<comments>http://jdevelopment.nl/hibernates-pure-native-scalar-queries-supported/#comments</comments>
		<pubDate>Sun, 22 Apr 2012 20:53:12 +0000</pubDate>
		<dc:creator>arjan tijms</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JPA]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=1423</guid>
		<description><![CDATA[In JPA one can define JPQL queries as well as native queries. Each of those can return either an Entity or one or more scalar values. Queries can be created on demand at run-time from a String, or at start-up time from an annotation (or corresponding XML variant see Where to put named queries in [...]]]></description>
			<content:encoded><![CDATA[<p>In JPA one can define JPQL queries as well as native queries. Each of those can return either an Entity or one or more scalar values. Queries can be created on demand at run-time from a String, or at start-up time from an annotation (or corresponding XML variant see <a href="http://jdevelopment.nl/put-named-queries-jpa" target="_blank">Where to put named queries in JPA?</a>).</p>
<p>Of all those combinations, curiously Hibernate has never supported named native queries returning a scalar result, including insert, update and delete queries which all don&#8217;t return a result set, but merely the number of rows affected.</p>
<p>It&#8217;s a curious case, since Hibernate does support scalar returns in non-native named queries (thus a scalar return and named queries is not the problem), and it does support scalar returns in dynamically created native queries (thus scalar returns in native queries are not the problem either).</p>
<p>An example of this specific combination:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;named-native-query</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;SomeName&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;query<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        INSERT INTO
            foo
        SELECT
            *
        FROM
            bar
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/query<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/named-native-query<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>If you do try to startup with such a query, Hibernate will throw an exception with the notorious message:</p>
<blockquote><p>
Pure native scalar queries are not yet supported
</p></blockquote>
<p>Extra peculiar is that this has been reported as a bug nearly 6 years(!) ago (see <a href="https://hibernate.onjira.com/browse/HHH-4412" target="_blank">HHH-4412</a>). In that timespan the advances in IT have been huge, but apparently not big enough to be able to fix this particular bug. <em>&#8220;Not yet&#8221;</em> certainly is a relative term in Hibernate&#8217;s world.</p>
<p>A quick look at Hibernate&#8217;s source-code reveals that the problem is within <a href="http://grepcode.com/file/repo1.maven.org/maven2/org.hibernate/hibernate-core/4.1.1.Final/org/hibernate/cfg/annotations/QueryBinder.java" target="_blank">org.hibernate.cfg.annotations.QueryBinder</a>, more specifically the <em>bindNativeQuery</em> method. It has the following outline:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> bindNativeQuery<span style="color: #009900;">&#40;</span>org.<span style="color: #006633;">hibernate</span>.<span style="color: #006633;">annotations</span>.<span style="color: #006633;">NamedNativeQuery</span> queryAnn, Mappings mappings<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>BinderHelper.<span style="color: #006633;">isEmptyAnnotationValue</span><span style="color: #009900;">&#40;</span>queryAnn.<span style="color: #006633;">name</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;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> AnnotationException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;A named query must have a name when used in class or package level&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    NamedSQLQueryDefinition query<span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> resultSetMapping <span style="color: #339933;">=</span> queryAnn.<span style="color: #006633;">resultSetMapping</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>BinderHelper.<span style="color: #006633;">isEmptyAnnotationValue</span><span style="color: #009900;">&#40;</span>resultSetMapping<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        query <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NamedSQLQueryDefinition <span style="color: #009900;">&#40;</span>
            <span style="color: #666666; font-style: italic;">// ...</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><span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">void</span>.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>queryAnn.<span style="color: #006633;">resultClass</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;">&#123;</span>        
        <span style="color: #666666; font-style: italic;">// FIXME should be done in a second pass due to entity name?</span>
        <span style="color: #000000; font-weight: bold;">final</span> NativeSQLQueryRootReturn entityQueryReturn <span style="color: #339933;">=</span>
            <span style="color: #000000; font-weight: bold;">new</span> NativeSQLQueryRootReturn <span style="color: #009900;">&#40;</span>
                <span style="color: #666666; font-style: italic;">// ...</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: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> NotYetImplementedException<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Pure native scalar queries are not yet supported&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></div></div>

<p>Apparently, the <em>NamedNativeQuery</em> annotation (or corresponding XML version), should either have a non-empty resultset mapping, or a result class that&#8217;s something other than void.</p>
<p>So, isn&#8217;t a workaround for this problem perhaps to just provide a resultset mapping, even though we&#8217;re not doing a <em>select</em> query? To test this, I tried the following:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;named-native-query</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;SomeName&quot;</span> <span style="color: #000066;">result-set-mapping</span>=<span style="color: #ff0000;">&quot;dummy&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;query<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        INSERT INTO
            foo
        SELECT
            *
        FROM
            bar
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/query<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/named-native-query<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sql-result-set-mapping</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dummy&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column-result</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;bla&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sql-result-set-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>And lo and behold, this actually works. Hibernate starts up and adds the query to its named query repository, and when subsequently executing the query there is no exception and the insert happens correctly.</p>
<p>Looking at the Hibernate code again it looks like this shouldn&#8217;t be that impossible to fix. It&#8217;s almost as if the original programmer just went out for lunch while working on that code fragment, temporarily put the exception there, and then after lunch completely forgot about it.</p>
<p>Until this has been fixed in Hibernate itself, the result-set-mapping workaround might be useful.</p>
<p><i>Arjan Tijms</i></p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/hibernates-pure-native-scalar-queries-supported/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>Tue, 10 Apr 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 an intermediate stage 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 an intermediate stage 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.</p>
<h3> Download </h3>
<p>The milestone 1 release that contains a lot of functionality implemented before ~March 2012 can be downloaded <a href="https://maven.java.net/service/local/repositories/releases/content/org/glassfish/javax.faces/2.2.0-m01/javax.faces-2.2.0-m01.jar">here</a>.</p>
<h3> New features </h3>
<ul>
<li> <a href="#get">GET Support </a>
<ul>
<li><a href="#758">View Actions</a></li>
</ul>
</li>
<li> <a href="#navigation">Navigation</a>
<ul>
<li><a href="#730">Faces Flow</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="#594">Facelets Component Tag can be declared via annotation</a></li>
<li><a href="#1038">Facelets ResourceResolver can be declared via annotation</a></li>
<li><a href="#703">Component and validator annotations default to simple class name</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="#1071">FlashFactory and FlashWrapper</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>
<li><a href="#001">Wrapper class for Lifecycle</a></li>
</ul>
</li>
<li> <a href="#lifecycle">Lifecycle</a>
<ul>
<li><a href="#949">Identify client windows via a Window Id</a></li>
<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">Unique ids and fixed name attribute for view state field</a></li>
</ul>
<ul>
<li><a href="#1100">Allowing id attribute on all elements for HTML5 content</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="navigation"></a></p>
<h3> Navigation </h3>
<p><a name="730"></a><br />
<strong>Faces Flow</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-730">spec issue 730</a>) <small>(mentioned in JSR)</small></p>
<p>In web applications, and in applications in general actually, there is often the concept of a &#8220;flow&#8221; that takes the user through a series of screens. Such flows includes wizards, multi-screen subscriptions, bookings, etc.</p>
<p>Common for such flows is that they are not a random selection of pages linked to each other, but there is clear starting point and a goal where the user comes closer to after each successive step in the flow.</p>
<p>Implementing such flows has in the past been done with ad-hoc techniques, like simply linking full pages to each other and using the session scope to pass information between pages. The problem here is that this makes the result difficult to re-use and not safe when the user opens the same flow in more than one window.</p>
<p>For the last use case CDI already presented a solution via the conversation scope (<em>@ConversationScoped</em>), but this is just a part of the equation and by itself doesn&#8217;t enable truly reusable flows. Inspired by <a href="http://www.springsource.org/spring-web-flow" title="SPRING WEB FLOW" target="_blank">Spring Web Flow</a> and <a href="http://docs.oracle.com/cd/E23943_01/web.1111/b31974/taskflows.htm" title="Getting Started with ADF Task Flows" target="_blank">ADF Task Flows </a>, JSF 2.2 will directly add support for this concept, most likely using the name <b>Faces Flow</b>.</p>
<p>Faces Flow is a rather big new feature, which brings with it quite an amount of new concepts and terminology in JSF. This is explained in much more detail in the official <a href="http://javaserverfaces-spec-public.java.net/nonav/proposals/JAVASERVERFACES_SPEC_PUBLIC-730/proposal.txt" title="730 - Faces Flow Proposal" target="_blank">proposal</a>.</p>
<p>Part of this feature will be a new annotation, <strong><em>@FlowScoped</em></strong>, which in the initial commit has the following JavaDoc:</p>
<blockquote><p>
Classes with this annotation attached to them will be considered to be in the scope of the named flow.  The implementation must provide an implementation of <code>javax.enterprise.inject.spi.Extension</code> that implements the semantics such that beans with this annotation are created when the user enters into the named flow, and de-allocated when the user exits the named flow.
</p></blockquote>
<p><small><i>(A remarkable detail is that JSF 2.2 thus seems to introduce a dependency on CDI here, which might be indicative of JSF moving closer to CDI for other aspects as well)</i></small></p>
<p>A flow scope bean will look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">@Named
@FlowScoped<span style="color: #009900;">&#40;</span>name=<span style="color: #0000ff;">&quot;someFlowName&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> SomBean <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A new element has been introduced for faces-config.xml: <strong><em>faces-flow-definition</em></strong>. At the moment it&#8217;s not yet clear how this will be used, but the current skeleton form looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;faces-config</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.2&quot;</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> <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-facesconfig_2_2.xsd&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;faces-flow-definition<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/faces-flow-definition<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/faces-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Holding up the design philosophy that as many things as possible should not require configuration in faces-config.xml, it will also be possible to put the flow definition directly into the artifacts that make up a flow. For instance, in a view the <em>metadata</em> element will be leveraged:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;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>
    xmlns:f=&quot;http://java.sun.com/jsf/core&quot;
    xmlns:j=&quot;http://java.sun.com/jsf/flow&quot;&gt;
    <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;j:faces-flow-definition</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;flow&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>
<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></div></div>

<p>Notice the addition of a new taglib URI in core JSF: <em>http://java.sun.com/jsf/flow</em>. This new taglib has the following description:</p>
<blockquote><p>Specify how a flow is declared in VDL pages. This schema is functionally equivalent to the same content in a faces-config.xml file.</p></blockquote>
<p>As of now, the following tags have been placed in that lib:</p>
<ul>
<li><em>faces-flow-definition</em></li>
<li><em>default-node</em> &#8211; (Gets name of this page without any extension)</li>
<li><em>view</em></li>
<li><em>vdl-document</em> &#8211; (Gets name of this page with the extension)</li>
</ul>
<p>A few new methods have been added to the Java API, for instance <em>javax.faces.application.Application</em> has a new method <em>FlowHandler getFlowHandler()</em>. A FlowHandler can be used to obtain the current <em>Flow</em> or transition to a new point in the flow. A <em>Flow</em> can among others be asked for the Id relative to the current window. This currently consists  of the <a href="#949">Window Id</a> and the Flow Id separated by an underscore.</p>
<p>At the time of writing, the implementation is only in a very early stadium and various things have not been implemented yet.</p>
<p>Commits for this feature have been done between 29/mar/12 and 10/apr/12.<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="594"></a><br />
<strong>Facelets Component Tag can be declared via annotation</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-594">spec issue 594</a>)<small>(mentioned in JSR)</small></p>
<p>Before JSF 2.0, creating (java based) custom components was a <a href="http://jdevelopment.nl/simple-java-based-jsf-custom-component" title="Simple Java based JSF custom component" target="_blank">lot of work</a>. The amount of required work was largely reduced in JSF 2.0, mainly because Facelets does not require an explicit tag handler and because a component can be registered via its annotation.</p>
<p>However, one tedious requirement remained. In order to make a component useable in Facelets, a tag name for it had to be declared in a *-taglib.xml file.</p>
<p>In JSF 2.2 this requirement has been lifted and the tag declaration can be done via the component&#8217;s annotation. For this the <em>@FacesComponent</em> annotation introduces 3 new attributes:</p>
<ul>
<li><em>createTag</em> &#8211; If set to true the component will be directly useable via a tag on a Facelet.</li>
<li><em>tagName</em> &#8211; Optional explicit name for the tag. If omitted, the class&#8217; simple name with the first character in lowercase will be used.</li>
<li><em>namespace</em> &#8211; Optional explicit namespace for the tag. If omitted the namespace &#8216;http://java.sun.com/jsf/component&#8217; will be used.</li>
</ul>
<p>For instance:</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>value=<span style="color: #0000ff;">&quot;components.CustomComponent&quot;</span>, createTag=<span style="color: #006600; font-weight: bold;">true</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>Without the need to configure or declare anything else, this component can now be used on a JSF 2.2 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://java.sun.com/jsf/component&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>Commits for this feature have been done between 13/feb/12 and 15/feb/12 and the associated issue has been marked as resolved.<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="703"></a><br />
<strong>Component and validator annotations default to simple class name</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-703">spec issue 703</a>)</p>
<p>With Java EE 5 a trend was started to introduce <a href="http://en.wikipedia.org/wiki/Convention_over_configuration" target="_blank">convention over configuration</a> into the platform, a trend which continued strongly in Java EE 6.</p>
<p>For annotations that cause a class to be registered under some name this typically means the simple class name (class name without the package) with the first letter de-capitalized is taken as the default for this name. For instance, in JSF 2 with the following class definition the managed bean will be available to EL using the name <em>foo</em></p>

<div class="wp_syntax"><div 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> Foo <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If needed the name can be specified explicitly:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">@ManagedBean<span style="color: #009900;">&#40;</span>name=<span style="color: #0000ff;">&quot;myfoo&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> Foo <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For components and validators this convention wasn&#8217;t used, and the name always had to be specified fully, e.g. for a component:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">@FacesComponent<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bar&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> Bar <span style="color: #000000; font-weight: bold;">extends</span> UIComponentBase <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><small><i>Notice the inconsistency between &#8220;name=&#8221; for the managed bean and not having to use an attribute name for the component.</i></small></p>
<p>In JSF 2.2 the name can now be omitted for components, converters and validators as well, so the code shown above can become:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">@FacesComponent
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Bar <span style="color: #000000; font-weight: bold;">extends</span> UIComponentBase <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This makes the overall platform a bit more consistent and is especially useful for simple implementations in application space. Most likely component libraries will still provide a (long) explicit name to prevent name clashes.</p>
<p>Commits for this feature have been done on 07/mar/12 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. The factory is obtainable via the constant <em>FactoryFinder.FACELET_FACTORY</em> (javax.faces.view.facelets.FaceletFactory).</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="1071"></a><br />
<strong>FlashFactory and FlashWrapper</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1071">spec issue 1071</a>)</p>
<p>Next to the <em>FaceletFactory</em>, another new factory obtainable via the <em>FactoryFinder</em> in JSF 2.2 is the new <em>FlashFactory</em>. This factory can be used to create <em>Flash</em> instances.</p>
<p>Obtaining those instances was already possible before JSF 2.2 via <em>ExternalContext#getFlash</em> and this will remain the default way for &#8216;normal&#8217; user code. The fact that there&#8217;s now a standard factory underneath means that there&#8217;s a hook available for overriding and/or wrapping the default implementation. This mechanism is explained <a href="http://insights2jsf.wordpress.com/2009/07/03/using-custom-factories-or-howto-wrap-facescontext" title="Using Custom Factories Or Howto Wrap FacesContext" target="_blank">here</a>.</p>
<p>The factory is obtainable via the constant <em>FactoryFinder.FLASH_FACTORY</em> (javax.faces.context.FlashFactory).</p>
<p>Overriding it in faces-config.xml can be done as follows:</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;factory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;flash-factory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.example.MyFlashFactory<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/flash-factory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/factory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>In addition to a factory, a new convenience wrapper class for the <em>Flash</em>, <em>FlashWrapper</em> has been added. Similar to existing wrapper classes like <em>ViewHandlerWrapper</em>, <em>ExceptionHandlerWrapper</em>, etc this allows one to wrap an existing <em>Flash</em> instance and selectively override methods.</p>
<p>Commits for this feature have been done on 16/feb/12 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="001"></a><br />
<strong>Wrapper class for Lifecycle</strong></p>
<p>In JSF many parts of the framework can be replaced by user supplied versions. Those versions can either completely replace the existing functionality, or can do this partially. For this the decorator pattern is utilized, where the user supplied implementation gets a reference to the existing implementation and can delegate functionality that it doesn&#8217;t want to replace to that.</p>
<p>To make this easy, JSF offers a bunch of wrapper classes; <em>ViewHandlerWrapper</em> for <em>ViewHandlerWrapper</em>, <em>StateManagerWrapper</em> for <em>StateManager</em>, etc.</p>
<p>Until now there wasn&#8217;t a wrapper class for <em>LifeCycle</em>. In JSF 2.2 there now is, with the predictable name <em>LifeCycleWrapper</em>  <img src='http://jdevelopment.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>This small feature didn&#8217;t had a separate JIRA issue, but was committed as part for the work for <a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-949">spec issue 949</a>.</p>
<p>Commits for this feature have been done on 23/feb/12.<br />
<br/></p>
<p><a name="lifecycle"></a></p>
<h3> Lifecycle </h3>
<p><a name="949"></a><br />
<strong>Identify client windows via a Window Id</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-949">spec issue 949</a>)</p>
<p>Arguably one of the biggest problems that has been plaguing web application development since its inception is the inability to distinguish requests originating from different windows of a single browser. Not only has an actual solution been long overdue, it has taken a long time to realize this even was a problem.</p>
<p>The root of the problem, as always, is that the HTTP protocol is inherently stateless while applications in general are not. There is the concept of a cookie though, which is overwhelmingly the mechanism used to distinguish requests from different users and to implement things like a session scope where on its turn the bulk of login mechanisms are based on.</p>
<p>While a cookie does work for this, it&#8217;s global per browser and domain. If a user opens multiple tabs or windows for the same domain then requests from those will all send the same cookie to the server. Logging in as a different user in a different window for the same website is thus not normally possible, and having workflows (involving post-backs, navigation) in different windows can also be troublesome because of this.</p>
<p>In JSF there are various solutions that are somehow related to this. The view scope effectively implements a session per window as long as the user stays on the same page and does only post-backs. The Flash is used for transferring data between different pages (presumably within the same window) when navigation is done via Redirect/GET. There&#8217;s a wide variety of scopes implemented by third parties that do something similar.</p>
<p>All of these have some implicit notion or assumption of the concept of a &#8216;client window&#8217;, but there is no explicit API for this.</p>
<p>JSF 2.2 will introduce support for two different aspects of this:</p>
<ol>
<li>Identification of an individual window: the Window Id</li>
<li>API and life-cyle awareness of the window concept</li>
</ol>
<p>The first item unfortunately cannot be implemented perfectly, as ultimately only the HTTP protocol can do this. <a href="http://lists.w3.org/Archives/Public/ietf-http-wg/2012JanMar/0098.html">HTTP/2.0</a> might indeed do this, but it will be some time before that will be available. JSF will do a best-effort though and might allow users to easily plug-in their own implementation if they so desire.</p>
<p>Since no method to identify a window is perfect, the user can configure the preferred method in web.xml via a context parameter:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.faces.WINDOW_ID_MODE<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>field<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The current draft implementation supports only <em>field</em> and <em>none</em> which is the default. Additional anticipated values are <em>script</em> and <em>url</em>.</p>
<p>The window Id currently consists of the session Id (JSESSIONID) followed by separator and a sequential number, e.g. <em>953FAAGhGhYF78:1</em>. The draft uses a hidden field with the name &#8220;javax.faces.WindowId&#8221;.</p>
<p>There are various places where the window concept will appear in the API, but most visible will be the new class <em>ClientWindow</em> and the method to obtain it: <em>ExternalContext#getClientWindow()</em>.</p>
<p>The JavaDoc for <em>ClientWindow</em> mentions the following:</p>
<blockquote>
<p>The lifetime of a <code>ClientWindow</code> starts on the first request made by a particular client window (or tab, or pop-up, etc) to the JSF runtime and persists as long as that window remains open. A client window is always associated with exactly one <code>UIViewRoot</code> instance at a time, but may display many different <code>UIViewRoot</code>s during its lifetime.</p>
</blockquote>
<p>Note that Window Id nor ClientWindow implement a new scope, but are instead a basis on which existing and new scopes can be portably implemented.</p>
<p>See also these resources:</p>
<ul>
<li><a href="https://cwiki.apache.org/confluence/display/MYFACES/WindowId+Proposal" target="_blank">WindowId Proposal</a></li>
<li><a href="https://cwiki.apache.org/confluence/display/EXTCDI/JSF+Usage" target="_blank">Window Context</a></li>
</ul>
<p>Commits for this feature have been done between 23/feb/12 and 29/feb/12.<br />
<br/></p>
<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>Unique ids and fixed 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 to have the fixed name. JSF 2.2 compatible libraries wanting to do something with the viewstate now have to work with a name-spaced Id (like other &#8216;normal&#8217; elements in the markup rendered by JSF), or do lookups based on the name <em>name</em> attribute if they want to continue using the fixed name.</p>
<p>For now, 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 had initially been marked as resolved, but was re-opened on 06/feb/12 because of complexities involving the notorious troublesome Portlets environment and a new commit was done on 09/feb/12. See the <a href="http://java.net/projects/javaserverfaces-spec-public/lists/jsr344-experts/archive/2012-01/message/20" title="[JSR344-EXPERTS] [220-VIEWSTATEPARAMNAMEONLY] PROPOSAL" target="_blank">EG discussion</a> for more details.</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><a name="1100"></a><br />
<strong>Allowing id attribute on all elements for HTML5 content</strong> (<a href="http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1100">spec issue 1100</a>)</p>
<p>Contrary to previous HTML standards, it&#8217;s allowed in HTML5 to use an id attribute for every kind of element.</p>
<p>In JSF 2.1 and earlier, the id attribute on the head was not being rendered. In case HTML5 content is being served, JSF 2.2 will render the id attribute for this element. For previous versions of HTML this is <strong>not</strong> done. Additionally, the VDL docs were updated and now state that for the body element an id attribute is rendered as well (this already happened, but the docs didn&#8217;t mention it).</p>
<p>Incidentally, a nice tidbit of information that we learned from this issue is that in JSF 2.2, HTML5 will be the default.</p>
<p>Commits for this feature have been done between 14/may/12 and the associated issue has been marked as resolved.<br />
<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>19</slash:comments>
		</item>
		<item>
		<title>Sample CRUD application with JSF and RichFaces</title>
		<link>http://jdevelopment.nl/sample-crud-app-with-jsf-and-richfaces/</link>
		<comments>http://jdevelopment.nl/sample-crud-app-with-jsf-and-richfaces/#comments</comments>
		<pubDate>Fri, 30 Mar 2012 13:01:05 +0000</pubDate>
		<dc:creator>Mark van der Tol</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jsf]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[ejb]]></category>
		<category><![CDATA[Java EE]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=1394</guid>
		<description><![CDATA[During my thesis project I will be using JavaServer Faces. Therefore it is important I get familiar with the framework. To get familiar I made a small CRUD (Create Read Update Delete) application. It is a simple application that makes it possible to keep track of users. It consists of a user list, a page [...]]]></description>
			<content:encoded><![CDATA[<p>During my thesis project I will be using JavaServer Faces. Therefore it is important I get familiar with the framework. To get familiar I made a small CRUD (Create Read Update Delete) application. It is a simple application that makes it possible to keep track of users. It consists of a user list, a page to add/edit users and a page to delete a user.</p>
<h3>The code</h3>
<p>The research project will focus on when it is beneficial to use client-side scripting instead of/complementary to server side programming. To get up to speed a small CRUD application has been made without the use of client-side scripting and the same CRUD application was adapted to use client-side scripting by using RichFaces. The client-side scripting was added to the editing form to allow validating the form, without the need of making requests to the server.</p>
<p>The structure of the source project is as follows:</p>
<ul>
<li>backing</li>
<ul>
<li>Index.java – Backing for index.xhtml</li>
<li>UserDelete.java – Backing for UserDelete.xhtml</li>
<li>UserEdit.java – Backing for UserEdit.xhtml</li>
</ul>
<li>constraints</li>
<ul>
<li>Email.java –  Validation annotation for fields. Fields with this annotation are validated to be a proper email address.</li>
<li>EmailConstraintValidator.java – Performs the validation for email addresses.</li>
</ul>
<li>ejb</li>
<ul>
<li>UserDAO.java – Data Access Object for users.</li>
</ul>
<li>entities</li>
<ul>
<li>User.java – Bean object for a user. The fields of this object are annotated with validators.</li>
<li>UserConvertor.java – Converts a userId to a user.</li>
</ul>
<li>util</li>
<ul>
<li>Messages.java – Utility object that helps with sending messages between pages.</li>
</ul>
</ul>
<p>The following JSF pages are in the project:</p>
<ul>
<li>index.xhtml &#8211; Page with a list with all users</li>
<li>user_delete.xhtml &#8211; Page used to confirm whether a user should be validated</li>
<li>user_edit.xhtml &#8211; Page used for adding and editing users</li>
</ul>
<p>While creating this application, I tried as much as possible to adhere to best practices. For examples, to go from the master (list) view to the detail (edit) view a GET request is used with the user id as parameter. The user is modified via POST and there’s a redirect and GET back to the master view (PRG pattern).</p>
<p>Both applications make use of Enterprise Java Beans, Bean Validation and Java Persistence API. EJB is used to inject persistence in the managed beans. Bean validation is used to ensure the data is consistent with the business rules.</p>
<p>In the RichFaces version user_edit.xhtml is updated to have client-side validations. Only the email address cannot be validated on the client. For that field is Ajax used.</p>
<p>The code of the project has been uploaded to <a href="http://code.google.com/p/javaee6-crud-example/">Google Code</a> so it can viewed by everyone. The code without the use of client-side scripting is put in the <a href="http://code.google.com/p/javaee6-crud-example/source/browse/?name=default">default branch</a> and the code with client-side scripting is put in the <a href="http://code.google.com/p/javaee6-crud-example/source/browse/?name=richfaces">RichFaces branch</a>.</p>
<h3>Demo</h3>
<p>The compiled applications have been uploaded to <a href="https://openshift.redhat.com/app/">OpenShift</a>. It makes showing your work to the public very easy. This is a free cloud platform which runs a JBoss server. The projects can be directly uploaded from Eclipse to the OpenShift server. <a href="http://javaee6crud-beginning.rhcloud.com/">The live demo can be viewed here.</a></p>
<p>To upload your project yourself to OpenShift it is first required to make an account at OpenShift. Then register your public key at OpenShift. When you have the OpenShift plug-in installed in Eclipse you can add the OpenShift server to the server view in Eclipse. From there you can get the default project from OpenShift. This project is only used to send the war files to server, not to hold the code. By adding the JSF project to the OpenShift server it automatically places the war file in the OpenShift project during a publish. By pushing the OpenShift project to the server using GIT, the application is put in the cloud and is ready to use. <a href="https://www.redhat.com/openshift/community/blogs/getting-started-with-the-openshift-eclipse-plug-in-for-java-applications-on-the-clo">The detailed process to upload to OpenShift from Eclipse is available here.</a></p>
<p>So wrapping up, I’ve made a small project to get familiar with the code, I have uploaded the code to Google Code and I’ve put the application OpenShift.</p>
<p><em>Mark van der Tol</em></p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/sample-crud-app-with-jsf-and-richfaces/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Automatically setting the label of a component in JSF 2</title>
		<link>http://jdevelopment.nl/automatically-setting-label-component/</link>
		<comments>http://jdevelopment.nl/automatically-setting-label-component/#comments</comments>
		<pubDate>Thu, 22 Mar 2012 12:18:11 +0000</pubDate>
		<dc:creator>arjan tijms</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=1375</guid>
		<description><![CDATA[In JSF input components have a label attribute that is typically used in (error) messages to let the user know about which component a message is. E.g. Name: a value is required here. If the label attribute isn&#8217;t used, JSF will show a generated Id instead that is nearly always completely incomprehensible to users. So, [...]]]></description>
			<content:encoded><![CDATA[<p>In JSF input components have a label attribute that is typically used in (error) messages to let the user know about which component a message is. E.g.</p>
<blockquote><p>Name: a value is required here.</p></blockquote>
<p>If the label attribute isn&#8217;t used, JSF will show a generated Id instead that is nearly always completely incomprehensible to users. So, this label is something you definitely want to use.</p>
<p>Of course, if the label is going to be used in the error message to identify said component, it should also be rendered on screen somewhere so the user knows which component has that label. For this JSF has the separate <code>&lt;h:outputLabel&gt;</code> component, which is typically but not necessarily placed right before the input component it labels.</p>
<h3>The problem</h3>
<p>The thing is though that this label component should nearly always have the exact same value as the label attribute of the component it labels, e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:outputLabel</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Username&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:inputText</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{myBean.username}&quot;</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;Username&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>There&#8217;s a duplication here that feels rather unnecessary (it feels even worse when the label comes from a somewhat longer expression, which is typical for I18N). My co-worker Bauke identified this problem <a href="http://java.net/jira/browse/JAVASERVERFACES-677" title="If any h:outputLabel is assigned for an UIInput component, use its value as label" target="_blank">quite some time ago</a>.</p>
<h3>Finding a solution</h3>
<p>It appears though that an implementation that automatically sets the label attribute of the target &#8220;for&#8221; component to the value of the outputLabel isn&#8217;t that difficult, although there are a couple of things to keep in mind.</p>
<p>For starters, a component in JSF doesn&#8217;t directly have something akin to an <em>@PostConstruct</em> method in which you can set up things. There are tag handlers and meta rules in which you can set up attributes, but when they execute not all components have to exist yet.</p>
<p>Luckily, we always have the plain old constructor and since JSF 2 components can register themselves for system events. This gets us into a method where we can setup things.</p>
<p>Additionally, we have to be aware of state. System event listeners are luckily not stateful, so perfectly suited for tasks that need to be setup once (Phase listeners are stateful though, and will &#8216;come back&#8217; after every postback). Attributes of a component are by default stateful, so we only need to set those once, not at every postback. Finally, the API distinguishes between deferred expressions (value expressions) and literals. If we want to support dynamic labels and only want to setup the wiring once, it&#8217;s important to take this distinction into account.</p>
<p>Finally, when searching for the target &#8220;for&#8221; component we can take advantage of the fact that typically this component will be close by. Compared to the regular search on the view root, the well-known &#8220;relative-up/down&#8221; search algorithm is probably more efficient here. This algorithm will start the search in the first naming container that is the parent of the component from where we start our search in the component tree. This will work its way up until there are no more parents, and if the component then still isn&#8217;t found (practically this is rare if the component indeed exists), then a downward sweep will be done starting from the root.</p>
<p>So, this all comes down to the following piece of code then (slightly abbreviated):</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">@FacesComponent<span style="color: #009900;">&#40;</span>OutputLabel.<span style="color: #006633;">COMPONENT_TYPE</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> OutputLabel <span style="color: #000000; font-weight: bold;">extends</span> HtmlOutputLabel <span style="color: #000000; font-weight: bold;">implements</span> SystemEventListener <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> OutputLabel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000;  font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>isPostback<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            getViewRoot<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">subscribeToViewEvent</span><span style="color: #009900;">&#40;</span>PreRenderViewEvent.<span style="color: #000000; font-weight: bold;">class</span>, <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>
&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> processEvent<span style="color: #009900;">&#40;</span>SystemEvent event<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> AbortProcessingException <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399; font-weight: bold;">String</span> forValue = <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;for&quot;</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>forValue<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            UIComponent forComponent = findComponentRelatively<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, forValue<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>getOptionalLabel<span style="color: #009900;">&#40;</span>forComponent<span style="color: #009900;">&#41;</span> == <span style="color: #006600; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                ValueExpression valueExpression = getValueExpression<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: #000000;  font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>valueExpression <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>
                    forComponent.<span style="color: #006633;">setValueExpression</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;label&quot;</span>, valueExpression<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #000000;  font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>                    
                    forComponent.<span style="color: #006633;">getAttributes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;label&quot;</span>, getValue<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>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>After creating a tag for this component, the following can now be used on a Facelet:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;o:outputLabel</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Username&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:inputText</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{myBean.username}&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>If a form is posted with this in it and no value is entered, JSF will respond with something like:</p>
<blockquote><p>Username: Validation Error: Value is required.</p></blockquote>
<p>An implementation of this is readily available in the new <a href="http://code.google.com/p/omnifaces" target="_blank">OmniFaces</a> library, from where you can find the <a href="http://wiki.omnifaces.googlecode.com/hg/vdldoc/o/outputLabel.html" target="_blank">documentation</a> and the full <a href="http://code.google.com/p/omnifaces/source/browse/src/org/omnifaces/component/outputlabel/OutputLabel.java" title="OutputLabel.java" target="_blank">source code</a>.</p>
<p><i>Arjan Tijms</i></p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/automatically-setting-label-component/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to run the Mojarra automated tests</title>
		<link>http://jdevelopment.nl/run-mojarra-automated-tests/</link>
		<comments>http://jdevelopment.nl/run-mojarra-automated-tests/#comments</comments>
		<pubDate>Sun, 26 Feb 2012 17:21:01 +0000</pubDate>
		<dc:creator>arjan tijms</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=1339</guid>
		<description><![CDATA[The JSF RI implementation Mojarra comes with a very extensive test suite. There are some instructions available on how to actually run those tests, such as the entry &#8220;How do I run the automated tests?&#8221; in the official FAQ and the document &#8220;TESTING_A_BUILD.txt&#8221; at the root of the Mojarra repository. Both sets of instructions are [...]]]></description>
			<content:encoded><![CDATA[<p>The JSF RI implementation Mojarra comes with a very extensive test suite. There are some instructions available on how to actually run those tests, such as the entry &#8220;<a href="https://wikis.oracle.com/display/GlassFish/JavaServerFacesRI#JavaServerFacesRI-HowdoIruntheautomatedtests%3F" target="_blank">How do I run the automated tests?</a>&#8221; in the official FAQ and the document &#8220;<a href="https://svn.java.net/svn/mojarra~svn/trunk/TESTING_A_BUILD.txt" target="_blank">TESTING_A_BUILD.tx</a>t&#8221; at the root of the Mojarra repository.</p>
<p>Both sets of instructions are not overly detailed and they don&#8217;t seem to be entirely up to date either. For instance, the entry in the FAQ states the following:</p>
<blockquote><p>This target will cause the JSF API and implementation to be deployed to the GlassFish server<br />
that is downloaded as part of the build process</p></blockquote>
<p>This however doesn&#8217;t happen for testing JSF 2.x anymore (there was code that downloaded GlassFish V2 for JSF 1.x, but this can&#8217;t be used for 2.x).</p>
<p>The TESTING_A_BUILD.txt document mentions:</p>
<blockquote><p>Be sure to install glassfish with a password on admin<br />
make sure that password.txt is found in container.home</p></blockquote>
<p>But how do we install glassfish with a password? If GlassFish is distributed as a .zip there is no install script, and the version with an installer also doesn&#8217;t offer any option to specify a password. I guess that there once was an option in the installer for this but it seems to have been removed since. Additionally, the password.txt in <em>container.home</em> appears to be not entirely correct.</p>
<p>After some trial &#038; error I got the tests to run (more or less, read below). For my own reference and in the hope it&#8217;ll be useful to someone, I&#8217;m jotting the instructions down here.</p>
<p>Download GlassFish 3.1.1 from <a href="http://glassfish.java.net/downloads/3.1.1-final.html" title="GlassFish Server Open Source Edition - 3.1.1" target="_blank">http://glassfish.java.net/downloads/3.1.1-final.html</a> (I choose the <a href="http://download.java.net/glassfish/3.1.1/release/glassfish-3.1.1.zip" target="_blank">zip archive</a>).</p>
<p>Unzip GlassFish somewhere; we&#8217;ll call the resulting directory <em>[glassfish home]</em> below. This will look something like:</p>
<pre>
glassfish3 <--- [glassfish home]
    bin
    glassfish
    javadb
    mq
    pkg
</pre>
<p>The tests assume a password is being used, so we're now going to set one:</p>
<p>cd into [glassfish home]/bin and execute the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">./asadmin start-domain domain1</pre></div></div>

<p><small>On Mac OS X this may take a minute due to a reverse DNS lookup being attempted (there's a <a href="http://java.net/jira/browse/GLASSFISH-16544" target="_blank">fix</a>).</small></p>
<p>After GlassFish has started, execute the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">./asadmin change-admin-password</pre></div></div>

<p>Press enter twice and enter <em>adminadmin</em> for the password as below:</p>
<pre>
Enter admin user name [default: admin]> (press enter)
Enter admin password> (press enter)
Enter new admin password> (enter adminadmin)
Enter new admin password again> (enter adminadmin)
Command change-admin-password executed successfully.
</pre>
<p>Most commands in the test suite that interact with GlassFish use a password file (<em>--passwordfile</em> option), but for some reason not all. For those an additional <em>.asadminpass</em> file is required to be in the home directory of the user as whom the tests are run. This can be created via the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">./asadmin login</pre></div></div>

<p>Press enter once and again enter <em>adminadmin</em> for the password as below:</p>
<pre>
Enter admin user name [default: admin]> (press enter)
Enter admin password> (enter adminadmin)
Login information relevant to admin user name [admin]
for host [localhost] and admin port [4848] stored at
[/home/youruser/.asadminpass] successfully.
Make sure that this file remains protected.
Information stored in this file will be used by
asadmin commands to manage the associated domain.
Command login executed successfully.
</pre>
<p>You can shutdown the container now using the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">./asadmin stop-domain domain1</pre></div></div>

<p>Mojarra has recently gone from being distributed as 2 jars to 1 jar. The Glassfish 3.1.1 that you downloaded still uses 2 jars. In the past the test code patched GlassFish, but probably in anticipation of GlassFish shipping with 1 jar this no longer happens. In the mean time, it's necessary to patch GlassFish yourself.</p>
<p>In <em>[glassfish home]/glassfish/domains/domain1/config/default-web.xml</em> and <em>[glassfish home]/glassfish/lib/templates/default-web.xml</em> remove the entries <em>jsf-api.jar</em> and <em>jsf-impl.jar</em> and replace them by a single <em>javax.faces.jar</em>. I.e.:</p>
<p>Replace</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">jsf-api.jar
jsp-impl.jar
jsf-impl.jar</pre></div></div>

<p>With</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">javax.faces.jar
jsp-impl.jar</pre></div></div>

<p>Make a backup of the resulting <em>[glassfish home]</em> directory now. The test suite occasionally corrupts GlassFish and you often need to restore a fresh version. In fact, to have reliable test results you might opt to use a fresh GlassFish for every test run.</p>
<p>The Mojarra tests depend a lot on Maven, so if haven't installed Maven then install it now. E.g. on Ubuntu:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">sudo apt-get install maven2</pre></div></div>

<p>(This is not needed on OS X, since it already comes with Maven)</p>
<p>Now we're ready to check out the Mojarra source code. I'll assume SVN knowledge here.</p>
<p>Check out: <a href="https://svn.java.net/svn/mojarra~svn/trunk" target="_blank">https://svn.java.net/svn/mojarra~svn/trunk</a></p>
<p>We'll rever to the directory where you checked out those sources as <em>[source home]</em> below. E.g.</p>
<pre>
Mojarra <--- [source home]
    jsf-api
    jsf-ri
    jsf-tools
    [...]
    password.txt
    build.properties.glassfish
    [...]
</pre>
<p>Prior to starting the tests, a few files need to be customized. First, put the following in <em>[source home]/password.txt</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="properties" style="font-family:monospace;"><span style="color: #000080; font-weight:bold;">AS_ADMIN_PASSWORD</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">adminadmin</span></pre></div></div>

<p>Now copy <em>[source home]/build.properties.glassfish</em> to <em>[source home]/build.properties</em>.</p>
<p>Set the following entries in the newly created <em>[source home]/build.properties</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="properties" style="font-family:monospace;"><span style="color: #000080; font-weight:bold;">jsf.build.home</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;"><span style="">&#91;</span>source home<span style="">&#93;</span></span>
<span style="color: #000080; font-weight:bold;">container.name</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">glassfishV3.1_no_cluster</span>
<span style="color: #000080; font-weight:bold;">container.home</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;"><span style="">&#91;</span>glassfish home<span style="">&#93;</span>/glassfish</span>
<span style="color: #000080; font-weight:bold;">halt.on.failure</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">no</span></pre></div></div>

<p>For example:</p>

<div class="wp_syntax"><div class="code"><pre class="properties" style="font-family:monospace;"><span style="color: #000080; font-weight:bold;">jsf.build.home</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">/home/myuser/Mojarra</span>
<span style="color: #000080; font-weight:bold;">container.name</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">glassfishV3.1_no_cluster</span>
<span style="color: #000080; font-weight:bold;">container.home</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">/home/myuser/glassfish3/glassfish</span>
<span style="color: #000080; font-weight:bold;">halt.on.failure</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">no</span></pre></div></div>

<p>If you haven't already set this up, define <em>JAVA_HOME</em>. This is typically not needed on OS X, but is needed on a e.g. a fresh Ubuntu installation. If you don't do this, Maven will let you think it can't find an artifact from a remote repository, while in fact it couldn't compile some source files.</p>
<p>Additionally set <em>ANT_OPTS</em> to allow more memory to be used. E.g. in Ubuntu (bash):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">JAVA_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>jdk1.6.0_31<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PATH</span>=<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>jdk1.6.0_31<span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #007800;">$PATH</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">ANT_OPTS</span>=<span style="color: #ff0000;">'-Xms512m -Xmx786m -XX:MaxPermSize=786m'</span></pre></div></div>

<p>Now we're *almost* ready to run the build and the test. Unfortunately the Mojarra trunk seems to have a circular dependency between the clean and main targets at the moment. To break this dependency, comment out two ant tasks in <em>[source home]/jsf-test/build.xml</em> (as of writing on line 119):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>119
120
121
122
123
124
125
126
127
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- ensure the api jar is deployed to the local maven repo --&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!-- COMMENT THIS OUT:</span>
<span style="color: #808080; font-style: italic;">&lt;ant dir=&quot;${api.dir}&quot; target=&quot;main&quot;&gt;</span>
<span style="color: #808080; font-style: italic;">    &lt;property name=&quot;skip.javadoc.jar&quot;  value=&quot;true&quot; /&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;/ant&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;ant dir=&quot;${api.dir}&quot; target=&quot;mvn.deploy.snapshot.local&quot;&gt;</span>
<span style="color: #808080; font-style: italic;">    &lt;property name=&quot;skip.javadoc.jar&quot;  value=&quot;true&quot; /&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;/ant&gt;</span>
<span style="color: #808080; font-style: italic;">--&gt;</span></pre></td></tr></table></div>

<p>Now cd to [source home] and execute the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">ant clean main</pre></div></div>

<p>If you're lucky, the build will succeed without failures. Now undo the commenting in <em>[source home]/jsf-test/build.xml</em>, so the file will look like below again:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>119
120
121
122
123
124
125
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- ensure the api jar is deployed to the local maven repo --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ant</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${api.dir}&quot;</span> <span style="color: #000066;">target</span>=<span style="color: #ff0000;">&quot;main&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;skip.javadoc.jar&quot;</span>  <span style="color: #000066;">value</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;/ant<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ant</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${api.dir}&quot;</span> <span style="color: #000066;">target</span>=<span style="color: #ff0000;">&quot;mvn.deploy.snapshot.local&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;skip.javadoc.jar&quot;</span>  <span style="color: #000066;">value</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;/ant<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>cd to [source home] and execute the following command again:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">ant clean main</pre></div></div>

<p>If everything still went correctly, we can now run the tests. There are quite a lot of tests and running the suite till completion took 31 minutes on my 2.93Ghz i7 iMac and 48 minutes on an Ubuntu 11.10 installation running inside VirtualBox 4.1.8 on the same machine. The tests generate a whopping 1.6MB of logging, so if you want to scan it later you might want to divert it to a file:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">ant test.with.container.refresh &gt; ~/test.txt</pre></div></div>

<p>If you want to follow the proceedings of the test run, you can use e.g. tail in a second terminal:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">tail -f ~/test.txt</pre></div></div>

<p>In my case, whatever I tried and however clean my system was, some tests were always failing. E.g. <em>testFormOmittedTrinidad</em>, <em>TestLifecycleImpl</em>, <em>ScrumToysTestCase</em>, <em>AdminGuiTestCase</em>.</p>
<p>Occasionally some tests don't honor the <em>halt.on.failure=no</em> setting that we did in the build.properties files. If this happens comment out the offending test. If you want to test your own changes to Mojarra, make sure to run the test suite a couple of times to get an idea of what tests are already failing for your system. I personally tried a couple of different machines and environments but none of them was able to pass all tests.</p>
<p>Finally, some extra information for Eclipse users. The tests can be run via Eclipse as well. If you initially check out the Mojarra project in Eclipse, you'll see a lot of errors. There are Eclipse project files, but they are obviously outdated (this is fixable, but I'll leave that for a next article). When working with Eclipse, you'll use Eclipse for editing the source code and for navigation (call references, navigate into and such). Even if the Eclipse project files are corrected, the actual compiler output will be thrown away.</p>
<p>To start the ant build via Eclipse, do the following:</p>
<p>Right click on build.xml, click Run As -> Ant Build… Go to the JRE tab and under VM arguments add the following:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">-Xms512m -Xmx786m -XX:MaxPermSize=786m</pre></div></div>

<p>On the same dialog go to the Environment tab and set JAVA_HOME to your installed JDK, e.g.<br />
variable JAVA_HOME, Value /opt/jdk1.6.0_31 (this is not specifically needed on OS X or if you've already set JAVA_HOME globally for your system).</p>
<p>Go to the Targets tab and deselect everything. Then first select clean and then main. The target execution order in the bottom left corner of the dialog should list them in the right order. Having followed the instructions outlined above for command line ant (commenting out entries in <em>[source home]/jsf-test/build.xml</em>) now click Run. Follow the same instructions for restoring the file and again click Run. Finally, deselect both targets and select the test.with.container.refresh target.</p>
<p><i>Arjan Tijms</i></p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/run-mojarra-automated-tests/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Eclipse 3.7 SR2 released!</title>
		<link>http://jdevelopment.nl/eclipse-37-sr2-released/</link>
		<comments>http://jdevelopment.nl/eclipse-37-sr2-released/#comments</comments>
		<pubDate>Fri, 24 Feb 2012 21:21:14 +0000</pubDate>
		<dc:creator>arjan tijms</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=1327</guid>
		<description><![CDATA[With once again an amazing release accuracy, today Eclipse Indigo Service Release 2, aka Eclipse 3.7.2 has been released. In the core packages, some 89 bugs have been fixed. Important projects that are part of the release train were updated as well, for instance WTP was updated from 3.3.1 to 3.3.2 (a fact not mentioned [...]]]></description>
			<content:encoded><![CDATA[<p>With once again an amazing release accuracy, today Eclipse Indigo Service Release 2, aka Eclipse 3.7.2 has been released.</p>
<p>In the core packages, some <a href="https://bugs.eclipse.org/bugs/buglist.cgi?bug_file_loc_type=allwordssubstr&#038;bug_status=RESOLVED&#038;bug_status=VERIFIED&#038;bug_status=CLOSED&#038;classification=Eclipse&#038;classification=RT&#038;columnlist=short_desc&#038;field0-0-0=noop&#038;keywords_type=allwords&#038;long_desc_type=allwordssubstr&#038;product=Equinox&#038;product=JDT&#038;product=PDE&#038;product=Platform&#038;query_format=advanced&#038;remaction=&#038;resolution=FIXED&#038;short_desc=&#038;short_desc_type=allwordssubstr&#038;status_whiteboard=&#038;status_whiteboard_type=allwordssubstr&#038;target_milestone=3.7.2&#038;type0-0-0=noop&#038;value0-0-0=%7C&#038;order=bug_id&#038;query_based_on=" title="Eclipse 3.7.2 Bug LIst" target="_blank">89 bugs</a> have been fixed. </p>
<p>Important projects that are part of the release train were updated as well, for instance WTP was updated from 3.3.1 to 3.3.2 (a fact not mentioned yet on the WTP homepage, but it can be found <a href="http://eclipse.org/webtools/releases/3.3.2/" title="WTP 3.3.2" target="_blank">here</a>). WTP 3.3.2 fixes no less than <a href="https://bugs.eclipse.org/bugs/report.cgi?x_axis_field=bug_severity&#038;y_axis_field=product&#038;z_axis_field=&#038;query_format=report-table&#038;short_desc_type=allwordssubstr&#038;short_desc=&#038;classification=WebTools&#038;target_milestone=3.0.2&#038;target_milestone=3.0.2+M1&#038;target_milestone=3.0.2+M2&#038;target_milestone=3.0.2+M3&#038;target_milestone=3.0.2+M4&#038;target_milestone=3.0.2+M5&#038;target_milestone=3.0.2+M6&#038;target_milestone=3.0.2+M7&#038;target_milestone=3.0.2+RC0&#038;target_milestone=3.0.2+RC1&#038;target_milestone=3.0.2+RC2&#038;target_milestone=3.0.2+RC3&#038;target_milestone=3.0.2+RC4&#038;target_milestone=3.3.2&#038;target_milestone=3.3.2+RC0&#038;target_milestone=3.3.2+RC1&#038;target_milestone=3.3.2+RC2&#038;target_milestone=3.3.2+RC3&#038;target_milestone=3.3.2+RC4&#038;target_milestone=3.3.2+RC5&#038;resolution=FIXED&#038;format=table&#038;action=wrap&#038;chfieldfrom=2011-09-15&#038;chfieldto=2012-02-29&#038;chfield=resolution&#038;chfieldvalue=FIXED" title="WTP 3.3.2 Bug List" target="_blank">112</a> bugs.</p>
<p>My personal favorite bug that has been fixed is <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=366825" target="_blank">100% CPU for long time in ASTUtils.createCompilationUnit</a>. Just having a fairly innocent page in your workspace would completely hang the CPU for minutes or more.</p>
<p>For the next version of Eclipse we&#8217;re supposedly going to get to see the 4.x line by default on the downloads page, so this might well be the last 3.x version that&#8217;s prominently featured on said page. Time will tell of course.</p>
<p>For now, Eclipse 3.7.2 can thus be downloaded from the usual place at <a href="http://eclipse.org/downloads">http://eclipse.org/downloads</a> and the general release notes can be found at <a href="http://www.eclipse.org/eclipse/development/readme_eclipse_3.7.2.html">http://www.eclipse.org/eclipse/development/readme_eclipse_3.7.2.html</a></p>
<p><i>Arjan Tijms</i></p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/eclipse-37-sr2-released/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Passing null to the model in JSF</title>
		<link>http://jdevelopment.nl/passing-null-model-jsf/</link>
		<comments>http://jdevelopment.nl/passing-null-model-jsf/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 16:30:28 +0000</pubDate>
		<dc:creator>development</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://jdevelopment.nl/?p=1306</guid>
		<description><![CDATA[The problem JSF allows you to bind an input component to a model bean via a so-called value binding. A notorious major headache is that a null is automatically coerced into a zero (0) when the bounded property is of type Integer or Long. This behavior only seems to rarely be the required one. A [...]]]></description>
			<content:encoded><![CDATA[<h3> The problem </h3>
<p>JSF allows you to bind an input component to a model bean via a so-called value binding. A notorious major headache is that a null is automatically coerced into a zero (0) when the bounded property is of type <em>Integer</em> or Long. This behavior only seems to rarely be the required one. A number of other types are coerced as well, e.g. in case of <em>Boolean</em> it&#8217;s coerced to <em>Boolean.FALSE</em>.</p>
<h3> Finding a solution </h3>
<p>Although coercing is a literal interpretation of the spec, it seems that basically only Tomcat that implements this somewhat peculiar behavior. They have a setting to turn this off:</p>
<blockquote><p>
-Dorg.apache.el.parser.COERCE_TO_ZERO=false
</p></blockquote>
<p>See e.g. <a href="http://stackoverflow.com/questions/4541612/jsf-integer-property-binded-to-a-inputtext-in-ui-is-set-to-zero-on-submit" target="_blank">jsf: integer property binded to a inputtext in UI is set to zero on submit</a></p>
<p>If you&#8217;re in a position to use this option, don&#8217;t read further and use it.</p>
<p>If you can&#8217;t use this option (you have e.g. a big system, lots of testing needed) and a null is needed at only a few places, another option is required. One possibility, that I will describe here, is based on a value-changed listener and a custom tag handler. What we do is breaking up the existing value binding of the input component in a base part and a property part, and then setting a null directly via the <a href="http://commons.apache.org/beanutils/" target="_blank">Apache BeanUtils</a> library.</p>
<p>The following shows a <em>TagHandler</em> implementation that does this breaking up and installs a value-changed listener on the parent component:</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
</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> MinusOneToNullConverter <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;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Class<span style="color: #339933;">&lt;?&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> VALUECHANGE_LISTENER_ZEROARG_SIG <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> LISTENER_EL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;#{minusOneToNullListener.processValueChange(component, %s, '%s')}&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> MinusOneToNullConverter<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>
&nbsp;
        <span style="color: #003399;">String</span> expression <span style="color: #339933;">=</span> parent.<span style="color: #006633;">getValueExpression</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#41;</span>.<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: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>expression.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">String</span> base <span style="color: #339933;">=</span> expression.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span>, expression.<span style="color: #006633;">lastIndexOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> property <span style="color: #339933;">=</span> expression.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>expression.<span style="color: #006633;">lastIndexOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>, expression.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            ExpressionFactory expressionFactory <span style="color: #339933;">=</span> ctx.<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: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>EditableValueHolder<span style="color: #009900;">&#41;</span> parent<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addValueChangeListener</span><span style="color: #009900;">&#40;</span>
                <span style="color: #000000; font-weight: bold;">new</span> MethodExpressionValueChangeListener<span style="color: #009900;">&#40;</span>expressionFactory.<span style="color: #006633;">createMethodExpression</span><span style="color: #009900;">&#40;</span>ctx, 
                    <span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>LISTENER_EL, base, property<span style="color: #009900;">&#41;</span>,
                    <span style="color: #003399;">Void</span>.<span style="color: #000000; font-weight: bold;">class</span>, VALUECHANGE_LISTENER_ZEROARG_SIG<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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Note that the EL expression uses &#8216;component&#8217; as the first parameter for the <em>processValueChange</em> method. This is an implicit EL object that refers to the current component being processed, which is in the case of this tag the parent component. Implementation wise it&#8217;s important to use the <em>FaceletContext</em> as the <em>ELContext</em>, instead of grabbing it from the <em>FacesContext</em>, when creating the method expression. Otherwise the context of the method expression would not be entirely correct and things like &lt;ui:param>s will not be resolved.</p>
<p>The following shows the value-changed listener that will be installed:</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="java5" style="font-family:monospace;">@ManagedBean
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MinusOneToNullListener <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> processValueChange<span style="color: #009900;">&#40;</span>UIInput component, <span style="color: #003399; font-weight: bold;">Object</span> object, <span style="color: #003399; font-weight: bold;">String</span> property<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> AbortProcessingException <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000;  font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>component.<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: #006600; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> component.<span style="color: #006633;">getValue</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: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;-1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            component.<span style="color: #006633;">resetValue</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;">try</span> <span style="color: #009900;">&#123;</span>
                PropertyUtils.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span>object, property, <span style="color: #006600; 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;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">IllegalAccessException</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> AbortProcessingException<span style="color: #009900;">&#40;</span>e<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; font-weight: bold;">InvocationTargetException</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> AbortProcessingException<span style="color: #009900;">&#40;</span>e<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; font-weight: bold;">NoSuchMethodException</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> AbortProcessingException<span style="color: #009900;">&#40;</span>e<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></pre></td></tr></table></div>

<p>As can be seen, the listener uses the input component to check if the value submitted was &#8220;-1&#8243;. The string representation is used here to be compatible with different types, although there are of course some other possibilities here. The listener will then reset the value of the component, so JSF will not attempt later on to push the -1 to our model object. Finally, bean utils is used to set the actual null value.</p>
<p>In this example, we used -1 to encode the desire for null. Other options might be feasible as well, such as the empty string or perhaps even null itself.</p>
<p>Before using this all, there is the tedious but mandatory registration of the tag handler in a *-taglib.xml file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</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>minusOneToNullConverter<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.MinusOneToNullConverter<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;/tag<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h3> Using the &#8216;converter&#8217; </h3>
<p>After doing the above, we&#8217;re now ready to use this on a Facelet, 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;h:selectOneMenu</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{someBean.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;my:minusOneToNullConverter</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;f:selectItem</span> <span style="color: #000066;">itemLabel</span>=<span style="color: #ff0000;">&quot;ALL&quot;</span> <span style="color: #000066;">itemValue</span>=<span style="color: #ff0000;">&quot;-1&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;#{data.somethings}&quot;</span> <span style="color: #000066;">var</span>=<span style="color: #ff0000;">&quot;something&quot;</span> <span style="color: #000066;">itemLabel</span>=<span style="color: #ff0000;">&quot;#{something.label}&quot;</span> <span style="color: #000066;">itemValue</span>=<span style="color: #ff0000;">&quot;#{something.key}&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>

<h3> Any other alternatives? </h3>
<p>Once again, if on Tomcat, <em>-Dorg.apache.el.parser.COERCE_TO_ZERO=false</em> should be your first choice. If you use an application server that isn&#8217;t based on Tomcat, you also probably don&#8217;t need this. There&#8217;s a <a href="http://java.net/jira/browse/JSP_SPEC_PUBLIC-184" target="_blank">spec proposal</a> open that asks to change this behavior, but despite a large amount of votes it hasn&#8217;t seen any activity for a long time. </p>
<p>Hopefully the hacky workaround presented here is helpful to someone.</p>
<p><i>Arjan Tijms</i></p>
]]></content:encoded>
			<wfw:commentRss>http://jdevelopment.nl/passing-null-model-jsf/feed/</wfw:commentRss>
		<slash:comments>1</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>4</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>
	</channel>
</rss>

