0

I need to set some values whenever a JSF page is called. So I looked at the lifecycle methods, thinking I'll be able to override, for example, the restoreView method so that I can set to null some of the page's properties. But I can't seem to find any example of anyone doing so. If you can point me to a tutorial, or an example, or let me know what is the right way for doing so -- I'd greatly appreciate it. Thanks!

Thinking out loud: maybe there is a phase in the xhtml generation when I can call a method on the backing bean, and set my values before anything else is rendered?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Herzog
  • 923
  • 1
  • 14
  • 28
  • In the future questions, if you're using JSF 2.0, please mention and tag that as such. The difference with JSF 1.x is pretty huge and answers would not be exchangeable or be done much cleaner/better for JSF 2.x. – BalusC Nov 24 '11 at 15:55

1 Answers1

0

Based on your question history, you're using JSF 2.0. So, this should do:

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

with

public void preRenderView() {
    this.some = null;
    // ...
}

This makes however very little sense and I can't help having the impression that you're looking for the solution in the wrong direction.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks again, BalusC. Yes, I am using jsf 2 and I'll make sure to tag properly next time. The solution you're offering is so elegant! I guess it would work for other areas as well, such as validation. The only thing is I still don't see the method getting called. My f:event tag is inside a form, and the method is in the bean. What am I missing? – Herzog Nov 24 '11 at 22:26
  • It works for me (Mojarra 2.1.4, but should work equally good on older versions). It can be placed as child of every `UIComponent` and the ``. It's called right before the view is rendered (regardless of a form is submitted or not). As to validation, that should be done by validators. They are supposed to run during validations phase. Your functional requirement is still unclear, so I'm not able to suggest the right approach. You might at least get some helpful ideas out of this article: [Communication in JSF 2.0](http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html). – BalusC Nov 24 '11 at 22:34
  • Thanks for the great article, BalusC. I'm very impressed with the elegance of JSF 2. As for my functional need, I'm still working on [link](http://stackoverflow.com/questions/8201634/fajax-inside-uirepeat) I can get the correct Bid view to open, but I can't get it to close when the user clicks another row in the repeat, or navigates to a different page and comes back. Setting the bidViews map to null, or putting a different parcel in auctionBidViews.showBidView(parcel) doesn't help. That's why I'm looking for a point in the lifecycle where I can set the bidViews map to null, and start over. – Herzog Nov 27 '11 at 08:53