Posted by Dominik
Wed, 24 Jan 2007 13:22:00 GMT
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}”).
Tags java, JSF | 8 comments | 3 trackbacks
Posted by Dominik
Tue, 23 Jan 2007 17:54:00 GMT
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.
Tags CRUD, generation, JSF | no comments | no trackbacks
Posted by Dominik
Thu, 11 Jan 2007 10:23:00 GMT
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.
Posted in Misc | no comments | no trackbacks
Posted by Dominik
Mon, 08 Jan 2007 17:55:00 GMT
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.
Posted in Programming | Tags JSF | no comments | no trackbacks
Posted by Dominik
Fri, 05 Jan 2007 15:51:00 GMT
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…
Posted in Programming | Tags java, JSF, rails | 2 comments | no trackbacks
Posted by Dominik
Tue, 02 Jan 2007 11:46:00 GMT
- 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.
Posted in Misc | Tags blogging, social | no comments | no trackbacks