Gem Survey
In response to Mike Clark’s question, here is what I’ve got on my machine:
wei-fieg@nono ~ $ sudo gem list --local | grep '([0-9]\.' actionmailer (1.3.2, 1.3.1) actionpack (1.13.2, 1.13.1) actionwebservice (1.2.2, 1.2.1) activerecord (1.15.2, 1.15.1) activesupport (1.4.1, 1.4.0) acts_as_taggable (2.0.2) ajax_scaffold_generator (3.1.11, 3.1.10) capistrano (1.4.0, 1.3.1) cheat (1.2.1) coverage (0.3) deprec (1.2.3, 1.2.1) facets (1.8.20, 1.7.46) ferret (0.10.14) flickr (1.0.0) highline (1.2.7, 1.2.5) hoe (1.1.7) log4r (1.0.5) mocha (0.4.0) model_security_generator (0.0.9) mysql (2.7) needle (1.3.0) net-sftp (1.1.0) net-ssh (1.0.10) openid_login_generator (0.1) rails (1.2.2, 1.2.1) rake (0.7.1) RedCloth (3.0.4) ruby-activeldap (0.8.1) ruby-openid (1.1.4) ruby-yadis (0.3.4) rubyforge (0.4.0) RubyInline (3.6.2) sources (0.0.1) streamlined_generator (0.0.5) tattle (1.0.1) termios (0.9.4) wirble (0.1.2) xml-simple (1.0.10)
Hmm, the number matches that of Mike Clark’s list (but is really low compared to Chad Fowler’s), the individual gems don’t all match up, though. But then, diversification is a good thing.
JSF: DataTable and CommandLink
Like a lot of other programmers who are forced to work with JSF, I fell into THE TRAP!
If you are using ManagedBeans in request scope, you get problems with CommandLinks inside DataTables.
DataTables are one thing I really like about JSF, and CommandLinks often come in handy as well. But when you put a CommandLink inside a DataTable, e. g., to select the entry of the row in which the CommandLink is, you get bitten. That is, if you want ManagedBeans with request scope. The action which should be triggered by the CommandLink is never triggered, the page is simply rendered again.
The reason for this behaviour is that the DataTable modifies the id of the CommandLink during renderering, but the CommandLink does not know that it was rendererd with a different id. During the decoding of the request which was triggered by clicking the CommandLink, the ComandLinkRenderer looks at a hidden form parameter. If the value of that form parameter equals the id of the CommandLink, an action is queued. If not, nothing is done.
Since the DataTable changes the ids, the value of the hidden form parameter does not match the id of the CommandLink.
So I wrote my own CommandLinkRenderer, overwrote the decode method and patched it so that the id match up again. When I want a CommandLink in a DataTable, I then specify that it should use my CommandLinkRenderer.
Here is the source of the decode method:
@Override
public void decode(FacesContext context, UIComponent component)
{
if (context == null || component == null)
{
throw new NullPointerException(Util
.getExceptionMessageString(Util.NULL_PARAMETERS_ERROR_MESSAGE_ID));
}
if (log.isTraceEnabled())
{
log.trace("Begin decoding component " + component.getId());
}
UICommand command = (UICommand) component;
// If the component is disabled, do not change the value of the
// component, since its state cannot be changed.
if (Util.componentIsDisabledOnReadonly(component))
{
if (log.isTraceEnabled())
{
log.trace("No decoding necessary since the component "
+ component.getId() + " is disabled");
}
return;
}
String clientId = command.getClientId(context), paramName = getHiddenFieldName(
context, command);
Map requestParameterMap = context.getExternalContext()
.getRequestParameterMap();
String value = (String) requestParameterMap.get(paramName);
clientId = clientId.substring(0, clientId.lastIndexOf(":"));
value = value.substring(0, value.lastIndexOf(":"));
if (value == null || value.equals("") || !clientId.equals(value))
{
return;
}
ActionEvent actionEvent = new ActionEvent(component);
component.queueEvent(actionEvent);
if (log.isDebugEnabled())
{
log.debug("This command resulted in form submission "
+ " ActionEvent queued " + actionEvent);
}
if (log.isTraceEnabled())
{
log.trace("End decoding component " + component.getId());
}
return;
}
The diff to the original method is:
diff CommandLinkRenderer.java PatchedCommandLinkRenderer.java
124a125,126
> clientId = clientId.substring(0, clientId.lastIndexOf(":"));
> value = value.substring(0, value.lastIndexOf(":"));
It works form me, maybe others get some benefit from it as well.
(P.S. this is crossposted to http://forum.java.sun.com/thread.jspa?threadID=5116147)
How to make JSF navigation rules more dynamic
Consider the following situation:
You do a post request from some form in an JSF application and want to decide on the page to redirect to dynamically. No problem, just use different “navigation-case” entries and go to different “to-view-id”s depending on the “from-outcome”.
<navigation-rule>
<from-view-id>/pages/organisation/*</from-view-id>
<navigation-case>
<from-action>#{OrganisationBean.select}</from-action>
<from-outcome>to-user</from-outcome>
<to-view-id>/pages/user/details.jsf</to-view-id>
<redirect/>
</navigation-case>
<navigation-case>
<from-action>#{OrganisationBean.select}</from-action>
<from-outcome>to-project</from-outcome>
<to-view-id>/pages/project/details.jsf</to-view-id>
<redirect/>
</navigation-case>
<navigation-case>
<from-action>#{OrganisationBean.select}</from-action>
<from-outcome>to-whatever</from-outcome>
<to-view-id>/pages/whatever/details.jsf</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>Sure, this works. It’s not exactly what I would call DRY, but it works. But what if you need to be more dynamic then that?
Just use a framework that’s more dynamic I hear you say.
Sure, I’d love to. But what if that is not an option?
I found this, this and this on the web, and putting these together gave me the following solution which I wanted to share with you:
Step 1:
Create a ViewHandler, in my case a subclass of com.sun.facelets.FaceletViewHandler and overwrite getActionURL():
@Override
public String getActionURL(FacesContext context, String viewId)
{
String result = viewId;
if(Util.isVBExpression(viewId))
{
ValueBinding vb = context.getApplication().createValueBinding(viewId);
result = vb.getValue(context).toString();
}
result = super.getActionURL(context, value);
int queryStart = value.indexOf("?");
if((queryStart > 0) && (result.indexOf("?") == -1))
{
result = result + value.substring(queryStart);
}
return result;
}This enables you to use EL in the to-view-id entry of a navigation case.
Step 2:
Register your view handler with the application, in my case:
<application>
...
<view-handler>com.interactive_objects.jsf.crud.generic.DynamicViewHandler</view-handler>
</application>Step 3:
Now you can do things like:
<navigation-rule>
<from-view-id>/pages/organisation/*</from-view-id>
<navigation-case>
<from-action>#{OrganisationBean.select}</from-action>
<from-outcome>selected</from-outcome>
<to-view-id>#{OrgansiationBean.computedRedirect}</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>(You can also use compound expressions like “/path/to/page.jsf?foo=#{ManagedBean.bar}”).
More fun with JSF
I’m getting into the flow of JSF at last. OK, flow might be too big a word, maybe it’s more like walk or stumble, but I’m diverting.
My official work these last days has been the creation of a generic component for CRUD operations in JSF, something that is fed with an interface or an class, configured and which then provides views for CRUD operations, lists and Searching and a controller which delegates the work of providing the right instances to a middle tier.
Such a component comes in handy when you have a huge domain model and need CRUD functionality for all domain classes. Sure, it is possible to generate the backing beans and views, but as long as the domain model is still a moving target, you need to regenerate those beans and classes each time a property or association is added to or removed from one of the domain classes. The company I’m working at has profound knowledge of everything that has to do with generation, but we also know that there are situations in which you can simply overdo things (the old problem that to someone who owns (only) a hammer, everything is a nail).
So we decided on a component which does the heavy lifting in this case. We will (most likely) still generate the classes needed to configure the crud component for the different domain classes, but after that we are not forced to touch the views or the backing bean when the domain class changes.
Maybe I’ll come back to write some more about this later on.
New Theme
Finally I came around to change the theme of this blog. The new theme is an adaption of Justin Palmer’s Azure, I just changed some styles and images.
The header image is a rotated detail of this image:
A spire of the Sagrada Familia.
Page URLs in JSF Applications revisited
Since I have to work with JSF (the customer wants it badly) I spend some time digging for ways to make the URLs of JSF Pages less annoying. I’ve found valuable information from Gavin King via http://www.theserverside.com/news/thread.tss?thread_id=38601 on how to use GET for JSF. I even went as far as to try my hands on something similar to rails’ routes. It works alright so far, but I still find it strange that there is now easy Out-Of-The-Box way to do GET requests in JSF. Have I simply missed something? I wouldn’t want to miss the value binding and some of the JSF components make writing Java webapps a better experience. All the declarative parts are fun to use. But it’s way too much xml. I’d rather specify which view to show after an action somewhere near the action, if possible in Java code, and not in some xml file. Right now I’ve got the feeling that the creation of a JSF webapp with URLs that are at least bookmarkable forces me to work my way around JSF’s core, and that seems like a stupid thing to do, when I use a framework.
Or maybe simply I shouldn’t have read this here.
Page URLs in JSF Applications
Is it just me, or is the following behavior annoying others as well?
I’ve been playing around with JSF lately and noticed that the URLs displayed by the browser and the pages shown don’t match.
The minimal setup I used was a page which lists all users and a page which displays the details for an user named, respectively, users/list.jsf and users/details.jsf.
The navigation part of the xml configuration I’m forced to write for that trivial application looks like:
<navigation-rule>
<from-page-id>/users/list.jsf</from-page-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/users/details.jsf</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-page-id>/users/details.jsf</from-page-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/users/list.jsf</to-view-id>
</navigation-case>
</navigation-rule>When I navigate to /users/list.jsf the list page is shown as expected. Once I select an entry I get served the details page. Sounds OK, but the browser is still showing /users/list.jsf as URL. And when I use a link on the details page to go back to the list page, the list page gets displayed again. That is fine as well. But now the browser shows /users/details.jsf as URL.
I’ve tested this with not only list and details, but the whole bunch of CRUD pages for Users, and the behavior is consistent. Consistently wrong.
Am I missing a point here?
Somehow I’m used to having some kind of relation between the URL shown by the browser and the page displayed. As I’m typing this, e. g., the URL shown is typo/admin/content/new and not typo/admin/content (which would have been the overview page). Now I have written and used my part of Java web applications, and often the URL shown by the browser was totally useless, but even that is an better behavior than having the wrong URL shown to you.
And it seems like I’m not doing everything wrong. Just checkout the URLs shown in Figures 4 & 5 of http://www-128.ibm.com/developerworks/java/library/j-jsf2/index.html. The same behavior over again.
And I haven’t even started to talk about the fact that, even if the right URL were shown for the details page, I could not bookmark it, because it carries no information about the user whose details I’m viewing at the moment.
Seems like I’m already spoiled by rails…
Things to do in 2007 (Part 1)
- Start a blog – check as done
Well, actually that was one of the things I wanted to do in 2006, but I didn’t take the time to do it, so I thought it would be good to start a blog now, early in the year, and hope that once the blog is there, I will keep on posting…
I will try to write down my thoughts on programming and software development as an art in contrast to engineering. And, if it is an art, I think that software should not only satisfy the needs and requirements of the end-users, but should satisfy certain aesthetic aspects as well.
So I named my blog “Ars Subtilior”. Ars Subtilior is a musical style that was developed in the late 14th century in the Provence. The meaning of it is
a more subtle art
and this is what I’m striving for when I write software and what I want to write about here.

