1
@Named
@RequestScoped
@URLMapping(id = "unblock", pattern = "/unblock", viewId = "/unblock.xhtml")
public class Unblock {

@URLAction
public void load(){
    //initialize some values..
}

public void sendRequest(){

}

and in xhtml file;

 <h:commandButton id="submit action="#{unblockAccount.sendRequest}"

Now when the first time page loads my load method gets called correctly, but when I click a submit button on the page and call "sendRequest" method, the load method gets called again.

How can I stop this? I tried to also use @ViewScope but didnt help

kolossus
  • 20,559
  • 3
  • 52
  • 104
Spring
  • 11,333
  • 29
  • 116
  • 185

3 Answers3

5

Add onPostBack=false to the @URLAction to stop the action from being called on a postback.

A post back is any request initiated on a JSF view after a fresh request for the view

    @URLAction(onPostback=false)
    public void load(){
      //initialize some values..
    }
kolossus
  • 20,559
  • 3
  • 52
  • 104
  • tnx, is there any advantage of using urlaction with onPostBack=false over using a constructor or a postconstruct? – Spring May 03 '13 at 13:37
  • @Spring [I won't advise using a constructor](http://stackoverflow.com/a/15774143/1530938). `@PostConstruct` will achieve basically the same thing but you'll need to be careful about its use with an `@RequestScoped` bean. – kolossus May 03 '13 at 13:46
  • @PostConstruct will be executed BEFORE path parameters get injected – chkal May 03 '13 at 17:01
  • @chkal, are you referring to PrettyFaces path params or JSF get parameter injections? – kolossus May 03 '13 at 17:46
  • Both are injected AFTER @PostConstruct. – chkal May 03 '13 at 19:50
  • @kolossus I use pretty faces in my website, is there anything I need to be careful using onPostback=false ? – Spring May 03 '13 at 20:32
  • 1
    @chkal, [that is absolutely untrue for JSF `@ManagedProperty`](http://stackoverflow.com/a/4889226/1530938), at least for Mojarra. If it were true, the whole point of `@PostConstruct` will be for nothing. Try it for yourself and see. If it ever happens that way,[it must be a bug in the impl](http://stackoverflow.com/questions/6605069/managedproperty-injected-after-postconstruct) – kolossus May 03 '13 at 21:38
  • @Spring, can't really say much, I don't know your setup. The only thing I could recommend is that you place only one-time setup operations in there, obviously because it'll run once and only once – kolossus May 03 '13 at 22:38
  • @kolossus could you also have alook at here http://stackoverflow.com/questions/17276752/urlaction-is-not-called – Spring Jun 24 '13 at 13:56
1

you could use @PostConstruct and maybe set your bean to ViewScoped.

howiewylie
  • 171
  • 4
  • 1
    could you be more descriptive in your answer and explain the problem or this should be a comment – Spring May 03 '13 at 13:12
  • You're right, I could have been more descriptive. Didn't have enough point to leave comments up to now though ;-). Hope it helped somewhat anyway. – howiewylie May 03 '13 at 14:20
1

In case you'd like to do your job in a @PostConstruct method you will find the following method useful. It basically initializes data only when view is loaded initially and skips initialization on postbacks:

@PostConstruct
public void initialize() {
    if (!FacesContext.getCurrentInstance().isPostback()) {
        //load your data
    }
}

Or sometimes we may use JSF event listener. It allows to initialize your data just before the view is to be rendered:

<f:event type="preRenderView" listener="#{bean.initialize}" />

with the same method, but without @PostConstruct annotation:

public void initialize() {
    if (!FacesContext.getCurrentInstance().isPostback()) {
        //load your data
    }
}

Finally, when JSF 2.2 is out you could use <f:viewAction> instead of <f:event> with the same method, but without the check, like:

<f:viewAction action="#{bean.initialize}" onPostback="false" />

with

public void initialize() {
    //load your data
}

But in case you're using PrettyFaces, just stick to the answer by kolossus. But just in case in won't like to use PrettyFaces for the job, the three methods above are always there for you. Of course, they'll work with 'plain' JSF.

skuntsel
  • 11,624
  • 11
  • 44
  • 67