Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Failing to take control of navigation.  RSS feed

Yuval Goldstein
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to take control of the navigation. Im using MyFaces.

let me explain:
I wish to have my own piece of code to parse the request URI and control the navigation to the next JSF page.
This functionality is desired for accomplishing a permalinks scheme of some sort.
For example

The link root/yuval/yuvals_stuff should cause the following to happen:

- I parse the request with a servlet filter and know that we need to load some data for yuval.
- I use a ViewHandler to load the data (it delegates the call to some service that gets the data from the database)
- I now forward the user to a known jsf page that is bounded with the viehandler and presents yuval's stuff .

Actually, the first time i try to use the code, the navigation goes well. But right after refresh or calling the same url again I get an exception. The code and the exception are provided here:

Here is what i do inside a servlet filter (I also tried to put this code inside the faces servlet, just for testing):

FacesContext fc = FacesUtils.getFacesContext(req, res);

String action = "#{viewPollViewHandler.go2}";

javax.faces.application.Application application = fc.getApplication();
MethodBinding mb = application.createMethodBinding("#{viewPollViewHandler.go2}",null);
String outcome = (String)mb.invoke(fc, null);

String fromAction = mb.getExpressionString();
NavigationHandler navigationHandler = application.getNavigationHandler();
navigationHandler.handleNavigation(fc, fromAction, outcome);
String viewId = fc.getViewRoot().getViewId();
UIViewRoot view = fc.getApplication().getViewHandler().createView(fc, viewId);



First we try to get the FacesContext, using the current request and response objects. Now we know what action we want to invoke on what ViewHandler, so we go ahead and make a method binding for this method through the JSF Application context. Now we invoke the method and get the outcome String from it. Till now everything seems to work ok � we get the ViewHandler's method invoked correctly and we get the proper outcome String from it.
This means the ViewHandler is now populated with our needed data, so all we have left to do is go to the presentation page that uses this ViewHandler.
We use JSF's NavigationHandler to navigate as usual, giving it a "fromAction" and an outcome. Now we want the name of the page for this navigation, so we get the view id from the FacesContext. The view id is correct and points to the next page as states in the faces-navigation.xml, so we set it as the new view root and try to "dispatch()" (which is the same as "forward()") to it.
The result seems sufficient, as we get to the next page and all the data is presented correctly (sometimes, in IE only we experienced some javascript errors that were unknown). The big problem occurs when trying to hit "REFRESH" or clicking back and then trying the original href link again. We suddenly get a NullPointerException that occurred in the "getValue()" method of JSF's getValueBinding()".

Can anyone contribute?

Thank in advance!
Yuval Goldstein
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

The following links shows how to solve the problem:

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!