<?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>Benny's Blog &#187; richfaces</title>
	<atom:link href="http://blog.projectnibble.org/tag/richfaces/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.projectnibble.org</link>
	<description>House Of Code</description>
	<lastBuildDate>Fri, 13 Aug 2010 15:06:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Java generic paged lazy List with JSF/JPA example implementation</title>
		<link>http://blog.projectnibble.org/2009/07/22/java-generic-paged-lazy-list-with-jsfjpa-example-implementation/</link>
		<comments>http://blog.projectnibble.org/2009/07/22/java-generic-paged-lazy-list-with-jsfjpa-example-implementation/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 12:58:53 +0000</pubDate>
		<dc:creator>Benny Bottema</dc:creator>
				<category><![CDATA[JSF/Facelets]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[lazy loading]]></category>
		<category><![CDATA[paging]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[richfaces]]></category>

		<guid isPermaLink="false">http://blog.projectnibble.org/?p=310</guid>
		<description><![CDATA[Here&#8217;s a list implementation I came up with to enable true paged data fetching completely transparent to any user. It works independent of persistence layers, such as JPA implementations etc. download PagedList Lazy loading with the PagedList Originally I made the PagedList because I needed paged data fetching using JMS queues for a JSF Rich [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a list implementation I came up with to enable true paged data fetching completely transparent to any user. It works independent of persistence layers, such as JPA implementations etc.</p>
<ul>
<li><a href="http://projectnibble.org/dump/blog/pagedlist/PagedList.rar">download PagedList</a></li>
</ul>
<p><span id="more-310"></span></p>
<h3>Lazy loading with the PagedList</h3>
<p>Originally I made the PagedList because I needed paged data fetching using JMS queues for a JSF Rich datatable and datascroller; a common problem in JSF but less commonly solved, even less documented and less still solved in an elegant way. Most people are able to solve this problem by using an <a href="https://www.hibernate.org/43.html">OpenSessionInView</a> antipattern (keeping Hibernate-oid session open until the view requests the lazy loaded data) but since I&#8217;m using JMS queue, this was not an option. I decided to circumvent the entire JSF to JMS queue integration by providing my data in a paging lazy list. It works beautifully.</p>
<pre  name="code" class="java">
public class PagedList&lt;Dto, QueryParameters&gt; extends AbstractList&lt;Dto&gt; {

    private final QueryParameters queryParameters;

    private final PagedDataProvider&lt;Dto, QueryParameters&gt; pagedDataProvider;

    private final Map&lt;Integer, List&lt;Dto&gt;&gt; fetchedPages;

    private final int dataSize;

    private final int pageSize;

    public PagedList(PagedDataProvider&lt;Dto, QueryParameters&gt; pagedDataProvider, QueryParameters queryParameters) {
        this.pagedDataProvider = pagedDataProvider;
        this.queryParameters = queryParameters;
        fetchedPages = new HashMap&lt;Integer, List&lt;Dto&gt;&gt;();
        dataSize = pagedDataProvider.getDataSize();
        pageSize = pagedDataProvider.getPageSize();
    }

    public Dto get(int index) {
        int pageNr = (int) Math.floor(index / pageSize);
        if (fetchedPages.get(pageNr) == null) {
            fetchedPages.put(pageNr, pagedDataProvider.provide(pageNr, queryParameters));
        }
        return fetchedPages.get(pageNr).get(index % pageSize);
    }

    public int size() {
        return dataSize;
    }
}
</pre>
<p>It is different from <a href="http://commons.apache.org/collections/apidocs/org/apache/commons/collections/list/LazyList.html">Apache&#8217;s LazyList</a> (and many other implementations) in that it relies on an external dataprovider to provide the dataset&#8217;s total size, pagesize and data provision itself. For example, using my lazy list version the <i>size()</i> method actually returns the total size of data potentially available, not just physically available in the list at the present. This way the list works completely transparent to its users. Also, there is no simple factory being passed in that creates dummy objects or something: actual remote data is being fecthed in pages by a data provider. Pages being fetched are independent of each other; when requesting page 5, page 1 through 4 are not need or fetched.</p>
<p>The PagedList works in combo with a <em>PagedDataProvider</em> interface that external dataproviders (some dao for example) could implement. So it has no dependencies whatsoever on some specific implementation. The paged data provider provides the total size of the dataset, the page size and pages of the dataset itself when needed.</p>
<h3>Example implementation for JSF and JPA dao</h3>
<p>Here&#8217;s an example implementation which ties a JSF Richfaces datatable to a JPA dao using a PagedList. I cut out the service layer, view handlers/controllers and whatnot for sake of simplicity.</p>
<pre  name="code" class="xhtml">
&lt;rich:dataTable id="searchResults" value="#{appleService.apples}" var="apple"&gt;
...
&lt;/rich:dataTable&gt;
</pre>
<pre  name="code" class="java">
/**
 * JPA dao which acts as paged data provider for the paged list.
 *
 * Relies on two JPA named queries specified on the Entity being fetched.
 *
 * @author Benny Bottema
 * @see PagedDataProvider
 */
public class AppleJPADao implements AppleDao, PagedDataProvider&lt;AppleDto, AppleQueryParameters&gt; {

    // entitymanager stuff omitted for example (real world: injected by Spring)

    private static final int PAGESIZE = 50;

    /**
     * @see AppleDao#getAllApples(String, String)
     */
    public List&lt;AppleDto&gt; getAllApples(String type, String quality) {
        // optional query parameters: can be null
        AppleQueryParameters params = new AppleQueryParameters();
        params.setAppleType(type);
        params.setAppleQuality(quality);
        return new PagedList&lt;AppleDto, AppleQueryParameters&gt;(this, params);
    }

    /**
     * @see PagedDataProvider#provide(int, Object)
     */
    public List&lt;AppleDto&gt; provide(int page, AppleQueryParameters queryCriteria) {
        Query query = entityManager.createNamedQuery("Apple.findAll");
        query.setParameter("appleType", queryCriteria.getAppleType());
        query.setParameter("appleQuality", queryCriteria.getAppleQuality());
        // JPA's paging mechanism
        query.setFirstResult(PAGESIZE * page);
        query.setMaxResults(PAGESIZE);
        return (List&lt;AppleDto>) query.getResultList();
    }

    /**
     * @see PagedDataProvider#getDataSize()
     */
    public int getDataSize() {
        Query countQuery = entityManager.createNamedQuery("Apple.count");
        return ((Long) countQuery.getSingleResult()).intValue();
    }

    /**
     * @see PagedDataProvider#provide()
     */
    public int getPageSize() {
        return PAGESIZE;
    }
}
</pre>
<h3>Known issues</h3>
<p>So far there is only one known issue, which is more of a problem with lazy loading in general: synchronizing the lazy loading list with the remote dataset to avoid becoming stale when changes happen to the dataset. Example: when the dataset changes, the size of the list is not being updated and the <i>get()</i> method may try to fetch wrong or even unavailable data throwing an exception. Since I&#8217;m using the paged list in a limited way, I&#8217;ve not yet encountered this problem. </p>
<p>One way would be to solve this problem for the Rich datable and datascroller specifically is to have a a4j poll component that listens for dataset changes on the server and refreshes the entire datatable as necessary. Just thinking out loud here&#8230;</p>
<p><a href="http://blog.projectnibble.org/2009/07/22/java-generic-paged-lazy-list-with-jsfjpa-example-implementation">trackback</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.projectnibble.org/2009/07/22/java-generic-paged-lazy-list-with-jsfjpa-example-implementation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSF is a diamond in the rough, you just need to make it shine</title>
		<link>http://blog.projectnibble.org/2009/05/24/jsf-is-a-diamond-in-the-rough-you-just-need-to-make-it-shine/</link>
		<comments>http://blog.projectnibble.org/2009/05/24/jsf-is-a-diamond-in-the-rough-you-just-need-to-make-it-shine/#comments</comments>
		<pubDate>Sun, 24 May 2009 13:30:53 +0000</pubDate>
		<dc:creator>Benny Bottema</dc:creator>
				<category><![CDATA[JSF/Facelets]]></category>
		<category><![CDATA[PrettyFaces]]></category>
		<category><![CDATA[a4j]]></category>
		<category><![CDATA[facelets]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[richfaces]]></category>
		<category><![CDATA[seam]]></category>
		<category><![CDATA[tomahawk]]></category>

		<guid isPermaLink="false">http://blog.projectnibble.org/?p=108</guid>
		<description><![CDATA[[RE: JSF – Still pretty much a steaming pile of donkey turd] &#8211; I was replying to Wille Faler’s post about why JSF sucks, when the comment was getting too large, so I made it into a post on my own blog. JSF is hard to learn yes, but in my opinion it doesn&#8217;t suck [...]]]></description>
			<content:encoded><![CDATA[<div style="color:#999999">
<p>[RE: JSF – Still pretty much a steaming pile of donkey turd] &#8211; I was replying to <a href="http://faler.wordpress.com/2007/03/13/jsf-still-pretty-much-a-steaming-pile-of-donkey-turd/">Wille Faler’s post about why JSF sucks</a>, when the comment was getting too large, so I made it into a post on my own blog.</p>
</div>
<hr />
<p><b>JSF is hard to learn yes, but in my opinion it doesn&#8217;t suck as bad as Wille says. All the points Wille mentions can be solved by certain libraries or write-once reusable solutions (I&#8217;ve included my &#8216;magical&#8217; combination of frameworks on the bottom). JSF is a diamond in the rough, you just need to make it shine.</b></p>
<p><span id="more-108"></span></p>
<h3>The devil is in the details</h3>
<p>There are a couple of pitfalls you need to know about with JSF. It&#8217;s all really a matter of having read a good book about JSF that points them out, or having learned them the hard way. Luckily I&#8217;ve had some guidance by experienced colleagues of mine; I&#8217;m guessing that&#8217;s the missing factor in Wille&#8217;s situation.</p>
<p>Referring to the pitfalls you need to know about, I&#8217;m talking about the common beginner&#8217;s problems like using JSTL c:tags that cause havoc when combined with those of jsf and facelets. I&#8217;m talking about the Ajax4Jsf details as Wille mentioned: once you know what to use it&#8217;s easy. Those 48 attributes? most of them are inherited and never used and you can forget about them, and that&#8217;s the learning curve. Once you know the right ones, it&#8217;s using exactly those, over and over again. And things like how you can incorporate Spring security and stuff like that. Another pitfall that comes to mind is how action=&#8221;" work quirky with navigation rules when using the array notation (action=&#8221;controller['action']&#8220;). And ofcourse there&#8217;s the issue of <a href="http://blog.projectnibble.org/2008/07/26/parameterized-jsf-facelets-validators/">parameterized JSF validators</a>, which needs some understanding. All pitfalls that can baffle a beginner or intermediate.</p>
<p>The thing with JSF is there are a number of little things that are hard to solve, but once you&#8217;re past that it <i>does</i> make you much more productive. You just have to have a solid base to start from each project. The pitfalls learning curve is definitely a downside of JSF, but <a href="http://blogs.jsfcentral.com/jsf2group/entry/jsf_2_primer">JSF 2.0</a> is in the making to solve all these issues.</p>
<h3>Bad performance or beginner&#8217;s mistake?</h3>
<p>Then there is this performance issue Wille mentions, where I think he missed the target: JSF does not have to be slow at all. It really depends on how you treat your backing beans. JSF has the weird tendency to call setters/getters multiple times, which seems utterly pointless (it&#8217;s related to resolving valuebindings in nested components). I&#8217;ve made the mistake before to directly retrieve values from a controller/service in my views instead of a static model (backing bean): this is what makes things slow. JSF in itself isn&#8217;t that slow for a webapp: yes there are many redundant calls but we&#8217;re not dealing with a realtime simulation application, webapps do just fine. On a sidenote, it also depends on the implementation&#8230; JSF is a reference specification, while there are various implementations of the framework (IBM&#8217;s, Sun&#8217;s etc.)</p>
<h3>SEO friendly JSF?</h3>
<p>Ahh, but for the <a href="http://blog.projectnibble.org/2009/03/20/bookmarkable-and-seo-friendly-jsf/">JSF Search Engine Friendliness</a>. That&#8217;s solved too, somewhat. There&#8217;s is a framework called <a href="http://ocpsoft.com/prettyfaces/">PrettyFaces</a> which allows you to dynamically generate/interpret pretty GET urls with bean values. It&#8217;s a great framework that solves a common problem, but there still exists the problem that existing jsf/facelets/richfaces components are not generating SEO HTML code. That&#8217;s really the fault of the component makers, not JSF, though JSF definitely isn&#8217;t innocent with its POST obsession: PrettyFaces fixes at least that.</p>
<h3>Common sense: use frameworks to avoid most pitfalls</h3>
<p>Once you&#8217;ve got those kind of things down and use a good framework combo, JSF is straightforward. You won&#8217;t really need to know about the various phases and you can be very productive if you&#8217;ve set up the jsf/facelets components right. I&#8217;ve worked with JSF intensively for about a year now in various projects with various implementations and I hardly ever need to use phaselisteners and that sort of stuff. The rare cases where you do need them, you can define these things ones and reuse them in other projects. With this approach I&#8217;ve created a template project with some standard libraries, phaselisteners, security set up, facelets components etc. and I&#8217;m up and running in no time.</p>
<p>I haven&#8217;t sanitized or otherwise cleaned up the template project so I won&#8217;t put that up for the time being, but at least I can tell you what frameworks are in it by default. </p>
<ul>The magical combination that works for me and my colleagues: </p>
<li>JSF (MVC)</li>
<li>Facelets (Templating, use of XHTML)</li>
<li><a href="http://livedemo.exadel.com/richfaces-demo/richfaces/inputNumberSlider.jsf?c=inputNumberSlider&#038;tab=usage">Richfaces</a> (component suite)</li>
<li><a href="http://livedemo.exadel.com/richfaces-demo/richfaces/commandButton.jsf?c=commandButton&#038;tab=usage">Richfaces A4J</a> (Ajax support)</li>
<li><a href="http://myfaces.apache.org/tomahawk/index.html">Tomahawk</a> (only for <a href="http://myfaces.apache.org/tomahawk/extensionsFilter.html">file uploads with the Extensions Filter</a>)</li>
<li>PrettyFaces (for indexable GET urls)</li>
</ul>
<p>Now that I&#8217;ve mentioned these, you should check out <a href="http://www.jboss.com/products/seam/">JBoss Seam</a> as well, which basically is supposed to do everything the above libraries do combined, plus a couple of things more like solving the <a href="http://matthiaswessendorf.wordpress.com/2008/11/19/conversation-scope-and-jsf-or-your-favorite-web-framework/">conversational scope</a> issue. I haven&#8217;t worked with this framework myself though so I can&#8217;t speak from experience.</p>
<h3>Final note</h3>
<p>Although I agree JSF has some annoying pitfalls, shortcomings and a high learning curve, it&#8217;s been out there for enough time to pass for supplement frameworks to pop up from under the ground to solve most of these problems.</p>
<p>With the right combination, most classic counter arguments have become obsolete. Maybe it&#8217;s time for critics to take JSF as is: incomplete but ultimately making you more productive if combined with the right frameworks.</p>
<p><a href="http://blog.projectnibble.org/2009/05/24/jsf-is-a-diamond-in-the-rough-you-just-need-to-make-it-shine/trackback/">trackback</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.projectnibble.org/2009/05/24/jsf-is-a-diamond-in-the-rough-you-just-need-to-make-it-shine/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
