This discussion is archived
4 Replies Latest reply: Feb 26, 2013 1:12 PM by 992953 RSS

Why is code from previous page executed when a refresh my page?

992953 Newbie
Currently Being Moderated
I have an application with 2 xhtml pages each with a managed bean. The first bean is session scoped and the second is request scoped.
When I click a button on my first page it saves some selections in a database and calls the second page. The second page constructor reads the saved records and the content is displayed in a table on the page.

If I press the backward button in my browser I get back to my first page where I can do a new selection which will be added to my first selection and displayed on my second page. So far it works fine and as expected.

Now, if I press the refresh button in my browser when the second page is displayed it will run all code that is run when I press the button on my first page. I.e. the last selection I made on the first page will be duplicated in my database.

I run my application on a tomcat server and I use Eclipse Juno to develop my app.

Can anyone explain why and what I can do to avoid this. When I press the refresh button I only want the constructor for my second page to execute.

Thanks in advance
Lars
  • 1. Re: Why is code from previous page executed when a refresh my page?
    gimbal2 Guru
    Currently Being Moderated
    First of all: don't use the constructor, use a @PostConstruct annotated method to initialize a bean. Otherwise you will get into problems when you start to use the JSF/JEE features more fully.

    JSF uses POST requests by default, even for navigation unless you use specialized JSF 2 components. The way I work around that is to make excessive use of post-get redirects, which in the context of JSF means that after I handle a post back I do this:
    public String doSomeButtonAction(){
      // bla bla bla
    
      return "outcome?faces-redirect=true";
    }
    That way you end up with a GET request in the end of the chain and the page url is correct too. When the user presses refresh, he/she refreshes that GET request and not the POST request that triggered the doSomeButtonAction().

    Be aware that you can only properly use JSF when you really know how it ticks under the hood. I would advise you to do some reading around about it, especially such topics as the stateful nature of JSF and the 6 lifecycle phases.
  • 2. Re: Why is code from previous page executed when a refresh my page?
    992953 Newbie
    Currently Being Moderated
    Thanks a lot.

    I have read the Java EE6 tutorial from top to bottom, but it is hard to remember everything written on 1000+ pages.

    I'm not sure I understand how to use

    return "outcome?faces-redirect=true";

    but now I know what to look for so I will search the internet for more information

    Thanks again
    Lars
  • 3. Re: Why is code from previous page executed when a refresh my page?
    gimbal2 Guru
    Currently Being Moderated
    989950 wrote:
    Thanks a lot.

    I have read the Java EE6 tutorial from top to bottom, but it is hard to remember everything written on 1000+ pages.

    I'm not sure I understand how to use

    return "outcome?faces-redirect=true";

    but now I know what to look for so I will search the internet for more information

    Thanks again
    Lars
    I would get a copy of the JSF reference book if I were you (digitally if you must). The tutorial must really suck badly if you didn't pick up on a simple and core concept such as outcomes. Its the cornerstone of JSF navigation :s
  • 4. Re: Why is code from previous page executed when a refresh my page?
    992953 Newbie
    Currently Being Moderated
    I have studied some articels on faces-redirect, so now I think I understand how to use it. I have modified my application and it works the way I want.

    I will also study the "book" you mentioned.

    Thanks again!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points