Saturday, May 13, 2006

JSF early adopter...

I know that I am not too early in the adoption cycle for JSF.. but it's funny, cause it feels that way.. I mean that I learned struts when it was already quite popular (although I think a little before it peaked in popularity), and there were already a good number of books, tutorials, etc. Not with JSF - there are a lot resources on how to do stuff, however, there is no "definitive" best practices that have existed with Struts in a while.. and so many different things just kinda seem half baked in JSF.. I mean, I understand that it was designed to have "tool support" and so on.. but really, there needs to be a package (e.g. maybe Shale) that would provide a layer on top of JSF that resolves the trivial and middle-of-the road problems in a nice way, just like Struts did the same for servlet programming.. I mean, before Struts, everyone was hacking out their own little solutions, and had their own way of dealing with http requests and responses, form submissions, validations, etc.. and now with JSF, there are a couple of different ways to solve the same problem..

Here is an example - dealing with row selection in an h:dataTable :
1. One possible way to solve it is to write a custom component, just like ibm did in this explanation . But really, how many people who don't use WebSphere would actually sit down and write their own component for that (especially considering that JSF components are notoriously difficult to do.

2. There was another post on the Sun JSF forums, where none other than CraigMcc responded to a question on how to do it (took a bit of digging
* Option#1 was to have a bolean property in the actual bean that you're iterating over, to indicate that the bean was selected. That is not very pleasant to use, as first, you might have to wrap your regular beans into a bean that has boolean property..
* Option #2 is to bind a datatable to the backing bean, bind a checkbox to the backing bean, and then on submit, actually iterate over the dataTable, and check the value of the h:selectBooleanCheckbox on each iteration . Now, this is pretty simple and straightforward, with the only downside that you definitely bind your backing bean to the JSF API (which is generally frowned upon, but then on 2nd thought, the backing bean is generally tied to the framework api anyways for whatever framework you're using). Now, the thing that kinda pissed me off was that after reading the two books that I mentioned in the previous post, I really had no clue that it would work that way - that the single checkbox that's bound into a single (not array) backing bean property, would change it's "selected" value based on how you iterate inside the h:dataTable .I kinda liked this solution the most.

3. In the appropriate column in the dataTable, include a verbatim html input checkbox. Then, when you're processing the submit in the backing bean, retrieve the request params with the same name as the checkbox, and you will get a string array with the checkbox values. Not a bad solution as you get everything you need, with the downside that you have to muck around with the request... (this option and a couple of other ones are discussed in more detail here

So, there it is.. there are a few different ways to solve this problem that should really be something trivial... and none of them are particularly straightforward....