aspose file tools*
The moose likes JSF and the fly likes why the same URL shows different page in JSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "why the same URL shows different page in JSF" Watch "why the same URL shows different page in JSF" New topic
Author

why the same URL shows different page in JSF

reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
I made a simple JSF project. That has a login page called index.jsp, if successfully logged in, the page will go to welcome.jsp, otherwise, stay in the index.jsp. Everything goes fine except that the address bar always show http://localhost:8880/JSF/index.jsp, even after the page shows the content after login which is in welcome.jsp. Does someone explain this?

here is part of the configuration file:

<faces-config>

<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/welcome.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>

<navigation-rule>
<from-view-id>/*</from-view-id>
</navigation-rule>
....


SCJA, SCJP5.0, SCBCD, SCWCD
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
When you enter a user name, a password and then submit, the form data is posted to "index.jsp". If you successfully login, on the server-side you are forwarded to welcome.jsp. The end result is that your browser never sees the actual URL for welcome.jsp: as far as it is concerned, you posted to index.jsp and some html was returned -- the fact that this html was generated by welcome.jsp is not revealed to the browser.

This can be changed easily, however. Try this:

What this does is that success causes a short html page to be returned to your browser telling it to immediately redirect, or browse, to welcome.jsp. This will cause the location bar to show http://...welcome.jsp as your URL. A positive consequence of this is that if the client reloads the welcome page (F5 in IE, ^R for Firefox) with this redirect option, they just resubmit the GET request to view welcome.jsp. On the other hand, without redirect, they end up rePOSTing their login, after being warned about reposting, which will scare or annoy some users.

Feel free to sprinkle redirect in anywhere you want the location bar updated or want reload to work without reposting. (Plus, it can makes bookmarking possible.) The penalty is that redirecting causes an extra trip to the server, so test it and see if that degrades performance too much for the users -- but hey, you're already using JSF so you're probably not too concerned about performance


There is no emoticon for what I am feeling!
madhuri madhuri
Ranch Hand

Joined: Jan 18, 2006
Posts: 58
Thanks Jeff! The reply really helped me understand. I too had that question in my mind right from day 1 when i started using JSF, did not bother to ask, thought would dig it myself later; but that later date never came .



~Madhuri
Saskia de Jong
Ranch Hand

Joined: Jan 24, 2006
Posts: 34
<redirect/> is sadly not always an option. The problem is you LOOSE al values that have been set in your request scope beans during for example the process updates phase.

Using <redirect/> forces you in a lot of situations to use session scoped beans. However session beans have their own problems, namely the fact that a user can't have different versions of the same page open and expect a submit to yield sane results.

In a typical scenario where page A forwards to B and B can forward back to A, you're in the curious situation that the browser shows A when you're actually on B and B when you're actually on A, except for the first time you're on A. In general, the browser always shows the previous page visitted instead of the current one.

Doesn't anyone have a solution to this?

I would say that showing ONE SINGLE url for a set of pages that forward to eachother is way better than showing the WRONG page. Showing the wrong page is very confusing for users, let alone a true support nightmare.

I've been trying to play a bit with a custom NavigationHandler and setting the action on the generated forms of a JSF page directly to a single page, but thusfar I haven't come really far with this. Since this is such a general problem I almost can't believe someone else hasn't come up with a solution before.
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
Thanks for the reply, Jeff.

What should I change in order to let that welcome.jsp open in a new browser? Should I add something else in the config or do I have to use button onclick?
Thanks.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by reubin yi:

What should I change in order to let that welcome.jsp open in a new browser?


This is more of an HTML/JavaScript question than a JSF question. In HTML, if you want a submitted form to always show its results in another window (perhaps you are using frames), you use the form's target attribute:

<form target="_blank">

The target _blank opens a new window. JSF's form tag passes through the target attribute.

To have specific command buttons open new windows, I do this:

<h:commandButton value="..." action="..." onklick="return myDoPopup('xyz.jsf')" />

(I wanted to spell that o-n-c-l-i-c-k, but this forum software is concerned about embedded JavaScript tricks...)

Where I define myDoPopup as


If you want a button to *conditionally* open a window, then you need to be a little trickier with your JavaScript. For example, you may want a submitted form to simply show validation errors, but if the form is filled in properly, have it remain in original window, but have a new window open with the results of the submit. To do this, you could conditionally include JavaScript on the page, or pass a JavaScript function a boolean value that's calculated server-side.
Saskia de Jong
Ranch Hand

Joined: Jan 24, 2006
Posts: 34
Originally posted by Jeff Albertson:
If you want a button to *conditionally* open a window,


Isn't that story a bit offtopic? I thought this thread was about a browser showing the wrong URL when using JSF's navigation rules?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
There's a long traditon here for threads that don't stick to topic.
arjan tijms
Greenhorn

Joined: Jan 22, 2006
Posts: 5
Originally posted by Saskia de Jong:

I would say that showing ONE SINGLE url for a set of pages that forward to eachother is way better than showing the WRONG page. Showing the wrong page is very confusing for users, let alone a true support nightmare.


I've stumbled upon the same problem. My initial solution to this problem is to create a custom view-handler. This view handler doesn't use the requested URL to restore the view, but uses a supplied parameter. If you alter the form in each JSF page to post to a single URL, then you essentially have what is asked above.

A quick and dirty implementation would look like this:



ViewHandlerDecorator is a straighforward class that just wraps the previous installed viewhandler and by default passes everything to this embedded handler:



In your set of JSF pages do something like this:



(remove the _ in the on_click attribute)

For completeness, an additional viewhandler can be installed by putting the following in faces-config.xml:



I've tested this using Myfaces 1.1.1 on Tomcat 5.5.17 with statesaving set to client and everything seems to work. Note that I'm depending on the MyFaces specific parameter "jsf_viewid", so for a more generic method you'll have to do some extra work. As said, the code given above is just a quick and dirty implementation of the idea, but it does show how flexible JSF really is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why the same URL shows different page in JSF
 
Similar Threads
Navigation Problem - URL not updated
Malformed end tag
action is not going to facesconfig.xml from login page
form address inverse
How to create a secure login system?