Tuesday, June 1, 2010

Custom Components strike back!

Hi all you JSF haters! Here's yet another heresy about writing JSF components you might want to know before you step into the ugly world of creation of reusable parts.

First of all if there's someone you know that knows for a fact that writing custom components is easy please direct that person to me so we can shed some light on why he/she things so. In my opinion having a 32 pages long manual just to achieve the simplest possible result (like this one) is just plain crazy. Please note that all this component does is it can show/hide itself from the resulting HTML, it can "bind" itself to a server-side bean and has a handful of properties most of which contradict with the actual well-known attribute names from HTML (for example styleClass where the well-known name for this is simply class).

Secondly one needs to notice that beautiful picture on page 10 that shows the necessary bits and pieces to make it work. Actually it shows the "simplified" version without separate renderers!

So what we need to have is 2 classes (the so called "Tag" class and the actual "Component" class) and 2 xml files (one being a tag library descriptor and the other one being the faces-config.xml).

Oh! And btw if you're wondering if they made any progress on that one in JSF 2.0 I rush to explain that they didn't! They only switched from the tag library descriptor to Facelets descriptor which has pretty much the same content, just more catchy name!

After going through the painful, unrefactorable xml bush we're presented with the powerful class model that we need to work with.

At this stage I really need to ask this question: was it really so damn hard to implement a pass-through for arguments??? I mean, come on guys! It's like stone age compared to what's available in other frameworks.

Back to classes...

The TickerTag class begins pretty simple - a field, getter, setter,... and then - the setProperties and release methods. "setProperties - what's that" you might ask. Well my friend, the fact of having a getter and setter is just not enough to make it work! You need a separate method that again contains some non-refactorable code simply to use it in the actual component tag... It doesn't get any worse than that, really...

But anyway... Let's see how we can implement the famous binding to bean properties!

In setProperties there's an if-statement to decide if it really _is_ a binding or a value. That thing could have been done one level down and not force the component writers to do this crazy check all over the place. One might ask at this point that since we're already storing the value why not re-use that in the actual code so that we don't multiply the instance of our values (once in the backing bean and once in the actual component itself). Well - keep askin'! That's simply how things are - period.

JSF component creation is a literal pain in the a$$. The actual model is plain stupid, does not provide an easy way to do simple things and requires one to write tons of stuff, possibly wrong at first (that's when nothing works). By comparison the model for creation user controls in ASP.NET is easier because every component is just a class that inherits from some other class (UserControl to be precise) and has both parts (the composition from other controls and custom rendering) simply built in. It is so easy that if you once go ASP.NET you'll never go back (at least not to JSF).

Back to reality - meaning to JSF :)

Beyond the simple, 32 pages long tutorial there are topics that are way more complicated like processing user input or action components. If you have the guts to go there please share your experience with the rest of us because the amount of tutorials and walk-throughs out there is just not enough.

No comments:

Post a Comment