aspose file tools*
The moose likes JSF and the fly likes Failing to take control of navigation. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Failing to take control of navigation." Watch "Failing to take control of navigation." New topic
Author

Failing to take control of navigation.

Yuval Goldstein
Greenhorn

Joined: Dec 27, 2006
Posts: 18
Hi,
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):

<code>
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);
view.setViewId(viewId);
fc.setViewRoot(view);

fc.getExternalContext().dispatch(viewId);
fc.responseComplete();
</code>

Explanation:

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.
Yuval Goldstein
Greenhorn

Joined: Dec 27, 2006
Posts: 18
Hey all,

The following links shows how to solve the problem:
http://wiki.apache.org/myfaces/InvokingJsfPagesWithStandardUrls#head-6c1aaf488d48f938896da962aaa4361ec3ffaf70

Enjoy.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Failing to take control of navigation.
 
Similar Threads
How to adjust Application at start-up?
why the same URL shows different page in JSF
Going back to previous page
Not Session Timeout, but throw javax.faces.application.ViewExpiredException
forwarding after response has been committed