aspose file tools*
The moose likes JSF and the fly likes Why does URL change if REDIRECT not specified? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Why does URL change if REDIRECT not specified?" Watch "Why does URL change if REDIRECT not specified?" New topic
Author

Why does URL change if REDIRECT not specified?

Tokunbo Oke
Ranch Hand

Joined: Sep 09, 2003
Posts: 57
In order to get to the menu form I enter http://localhost:8080/myapp/pages/menu.jsf into the browser. When the menu form comes up, I click on a button that takes me to the status form. After getting to the status form, the URL remains the same i.e http://localhost:8080/myapp/pages/menu.jsf However, as soon as I click any button in the status form, the URL changes (to http://localhost:8080/myapp/pages/status.jsf ). As a result of this, all the components that have been programmatically set to rendered=false become rendered when the status form is rendered after submission.

Even though I did not specify a redirect, the changing of the URL gives the impression that a redirect takes place after the status form is submitted. Can anyone please tell me why this is happening and how I can stop it. I am using MyFaces 2.0 on Tomcat 7 The managed bean for status form is in the request scope.

Thanks.


1. A button in the status form that changes the URL
====================================================


2. Part of status form's managed bean
====================================


2. Faces Config
================

Guy deLyonesse
Ranch Hand

Joined: Apr 12, 2011
Posts: 200

In looking at your navigation rules I don't see the desired behavior configured.

You need navigation rules to take you from menu to status, and your action method needs to return the appropriate String to trigger the navigation rule.
Tokunbo Oke
Ranch Hand

Joined: Sep 09, 2003
Posts: 57
Guy deLyonesse, thanks for your response.

I do not have any problem navigating from the menu form to the status form, though. This is because I am using JSF 2.0'S implicit navigation, which is an alternative to declaring navigation rules in faces-config.xml. The only thing I need help with is understanding why the URL changes when I submit the status form and I am not redirecting?

Thanks.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

You do not own the URL in JSF. JSF does. And JSF, unlike most web frameworks, uses the URL less as an absolute resource path than it does as a session handle, so the actual displayed text is not necessarily an indication of the underlying resource(s) used to build the View (page).

You are not doing a redirect. A redirect is a completely separate operation and actually should be avoided due to the extra overhead required.

The reason you're losing your "rendered" settings has nothing to do with the URL. It has to do with the fact that your backing bean is in Request scope.

Meaning that once the page has been displayed, the original backing bean is destroyed and when you then postback the form, a whole new un-initialized backing bean will be created.

JSF can't do much with request-scope objects because of this issue. You need longer-lived scopes. Originally, this meant Session or Application scope, but JSF2 added a special scope called View scope that's usually sufficient for data entry forms.


Customer surveys are for companies who didn't pay proper attention to begin with.
Tokunbo Oke
Ranch Hand

Joined: Sep 09, 2003
Posts: 57
Tim Holloway, thanks for your response. You are absolutely correct my rendered setting are lost because of the fact that the backing bean is request scoped.

Regarding the visible URL that changes when I am not using redirect to navigate to another page (or even to the same page); are you saying this is a normal behaviour of JSF? If yes, please give me a link to any document that sheds more light on why this is the case. I am just curious to know why it is designed to work this way, as this is different from what I understand about forwarding and redirecting.

Thanks.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

I don't recall ever having seen anything written up on the URL situation but it basically has to do with the fact that JSF makes extensive use of postbacks.

Most web frameworks display a form and they're done with it. In JSF, the same form will be repeatedly redisplayed (thanks to postbacks) until you've satisfied all the validation constraints and made all the error messages go away.

HTTP does not send out a URL when it sends out a page. In fact, it really can't since a given page may have dozens of different URLs coded on it for things like menus, links to other places, click-through adds and so forth. The URL in the navigation bar is simply the URL being used to do the next server request.

You can force the URL in the navigation bar to reflect the target resources more accurately using the "redirect" navigation directive - and in cases of secured pages, you usually have to. But that requires a literal redirect, which is extra overhead. So for best performance, it's better not to unless you absolutely must.
Tokunbo Oke
Ranch Hand

Joined: Sep 09, 2003
Posts: 57
Noted, thanks a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does URL change if REDIRECT not specified?