7

JSF URLs don't seem to change after the first click, only after the 2nd. E.g. when navigating from home.jsf to auction.jsf, the page being displayed is already auction.jsf but the URL in the browser address bar stays at home.jsf, until I click on the Auction link the second time. Why is this happening? Is there a way to disable it, and get the url to display correctly?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Herzog
  • 923
  • 1
  • 14
  • 28

2 Answers2

7

You seem to be navigating by POST instead of by GET. You should not perform page-to-page navigation by POST in first place. Replace <h:commandLink> by <h:link>.

So, do not navigate by

<h:form>
    <h:commandLink value="Auction" action="auction" />
</h:form>

but by

<h:link value="Auction" outcome="auction" />

A JSF POST form basically submits to the current URL and any navigation is by default performed by a server-side forward using RequestDispatcher#forward() (if you are well familiar with the basic Servlet API, you know what this means). You could work around with performing a redirect instead

<h:form>
    <h:commandLink value="Auction" action="auction?faces-redirect=true" />
</h:form>

but, as said, this is a work around not a solution.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks, BalusC. But most, actually all of my relevant pages are with forms so I need a POST. Would this still work? – Herzog Jan 12 '12 at 19:54
  • What data do you need to send along with POST then? Nothing actually as it's just page-to-page navigation, right? Just use a ``. It doesn't matter if you already have a form or not. The `` doesn't care about if it's inside a form or not. Or if you actually need to pass request parameters, just use `` in the link and `` in the target page. Using a GET redirect after POST trashes the whole POST request anyway, so why would you not just perform the GET directly? POST is at first place the wrong tool for the page-to-page navigation job. – BalusC Jan 12 '12 at 19:56
  • Now I'm confused. I actually need to send data, and in some cases also prepare the page with fresh data from the db. In my navigation methods I've been checking to see if the bean is in the session, and if so setting some values on it. For example been.setAuctionItems. (In some cases there's quite a bit of preparation work.) If the been isn't in the session I do the same work in the PostConstruct. Is there a better way? – Herzog Jan 12 '12 at 20:05
  • In other words, a redirect likely won't solve the problem for you either. As the concrete functional requirement is unclear, all I can suggest is to read that "See also" link (and all of its "See also" links) and if necessary [Communication in JSF2](http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html). – BalusC Jan 12 '12 at 20:07
3

You need to add faces-redirect=true to your URL. Check out this short tutorial from Mkyong.com.

Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90