The Artima Developer Community
Sponsored Link

Agile Buzz Forum
How to make JSF navigation rules more dynamic

5 replies on 1 page. Most recent reply: Jul 19, 2007 9:20 AM by Wojciech Bobiatyński

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Dominik Wei-Fieg

Posts: 60
Nickname: dominikwei
Registered: Aug, 2005

Dominik Wei-Fieg is a software developer living and working in Freiburg, Germany
How to make JSF navigation rules more dynamic Posted: Jan 24, 2007 4:42 AM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by Dominik Wei-Fieg.
Original Post: How to make JSF navigation rules more dynamic
Feed Title: Ars Subtilior
Feed URL: http://typo.ars-subtilior.com/articles.rss
Feed Description: Finest Handmade Code Random thoughts about agile software development as an art
Latest Agile Buzz Posts
Latest Agile Buzz Posts by Dominik Wei-Fieg
Latest Posts From Ars Subtilior

Advertisement

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(); } return super.getActionURL(context, 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}”).

Read: How to make JSF navigation rules more dynamic


Fredrik Sköldheimer

Posts: 2
Nickname: fredriksk
Registered: Mar, 2007

Re: How to make JSF navigation rules more dynamic Posted: Mar 9, 2007 7:52 AM
Reply to this message Reply
(You can also use compound expressions like “/path/to/page.jsf?foo=#{ManagedBean.bar}”).

I can't get this to work. If I try to this I only get redirected to /path/to/page.jsf

All the paramterers are gone.

I use facelets and wonder if I impored the right ViewHandler in my class that overrides the getActionURL

I get two suggestions when fixing imports in netBeans.
javax.faces.application.ViewHandler;
javax.faces.context.FacesContext ;

Witch one should I use?

Here is my class.
------ 8< ----
import com.sun.facelets.tag.jsf.core.ViewHandler;
import com.sun.faces.util.Util;
import javax.faces.application.ViewHandler;
//import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;

public class DynamicViewHandler extends com.sun.facelets.FaceletViewHandler {

public DynamicViewHandler(ViewHandler parent) {
super(parent);
}

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();
}
return super.getActionURL(context, result);
}
}

Fredrik Sköldheimer

Posts: 2
Nickname: fredriksk
Registered: Mar, 2007

Re: How to make JSF navigation rules more dynamic Posted: Mar 12, 2007 7:27 AM
Reply to this message Reply
ok. I have done some more research on this.

I have done a System.out on the result variable before: return super.getActionURL(context, result);


It seems as if facelets/myfaces or something reloads the page one more time without the paramters:

The output gives me this:

----- 8<----
result=/result.jsf?function=foo&amp;result=100
result=/result.xhtml
------ --------
I can't figure out why though... anyone that can help?

Wojciech Bobiatyński

Posts: 3
Nickname: pesel
Registered: Jul, 2007

Re: How to make JSF navigation rules more dynamic Posted: Jul 18, 2007 1:44 PM
Reply to this message Reply
Look at http://typo.ars-subtilior.com/articles/2007/01/24/how-to-make-jsf-navigation-rules-more-dynamic.
There is a following fragment:

int queryStart = value.indexOf("?");
if((queryStart > 0) && (result.indexOf("?") == -1))
{
result = result + value.substring(queryStart);
}
return result;
at the end of getActionUrl, which you seem not to have. I think it should solve your problem.
Regards
Wojtek

Wojciech Bobiatyński

Posts: 3
Nickname: pesel
Registered: Jul, 2007

Re: How to make JSF navigation rules more dynamic Posted: Jul 19, 2007 7:39 AM
Reply to this message Reply
And of course
String value=result;
before
result= getActionURL(...

Wojciech Bobiatyński

Posts: 3
Nickname: pesel
Registered: Jul, 2007

Re: How to make JSF navigation rules more dynamic Posted: Jul 19, 2007 9:20 AM
Reply to this message Reply
Hello Dominik. I've tried it and it works cute. But what about situation, when we return null from backing bean method (there is no navigation used and page is re-rendered), and we'd like to keep request parameter for that page. I need it for my jsp-jsf application and I'd very grateful for any your suggestion:)

Flat View: This topic has 5 replies on 1 page
Topic: Smalltalk Daily 7/16/07: Application Settings Previous Topic   Next Topic Topic: Farscape?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use