Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

duplicate call on preRenderView event

 
Carlos Jorge Tavares Ferreira
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my xhtml file I've defined the following:



And in my bean:



And for some reason that I can't figure out, the load method is being invoked
twice every time I load the page, forcing me to add a control variable.

I've searched the code and confirmed that only exists one reference to the load method.

What other event can cause this behavior?

Kind regards,

Carlos Ferreira
 
Brendan Healey
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi, you should find that if you take the f:event tag outside the f:metadata tags that the listener
fires only once. From experimentation it seems that only f:viewParam tags should be nested
within f:metadata.

I've found this confusing myself because various JSF books contain examples showing f:event
inside f:metadata.

The structure I use is

<f:metadata>
<f:viewparam />
</f:metadata>
<f:event />
<h:head>
</h:head>
<h:body>
</h:body>

Regards,
Brendan.
 
Carlos Jorge Tavares Ferreira
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brendan,

Thanks for your reply.

Unfortunately, the suggestion you've made doesn't work. If I don't enclose the f:event tag inside the f:metadata
the load method doesn't get invoked.
So, the problem remains.

Kind regards,

Carlos Ferreira
 
Carlos Jorge Tavares Ferreira
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I've finally figured it out. I was placing the <f:event> and <f:metadata> tags inside the <ui:composition> tag,
and that was causing the duplicate call.

Kind regards,

Carlos Ferreira
 
Brendan Healey
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using templates JSF should be stripping everything outside of the ui:composition tags,
so I'd not expect your f:event tag to be processed. Here is a code extract from my template and
a content page, this is how I think it should work, note that I explicitly include an f:view tag...

--- template.xhtml ---


--- content.xhtml ---


If you've got this working with f:event outside of the ui:composition tag in a content page then I'd
say that this is almost certainly because of a bug in the implementation, rather than how it should
be done, but if it works for you who am I to argue!

From Core JSF 3rd Ed (Geary/Hortsmann) p184: "Facelets removes all tags outside the ui:composition
tag, that is the doctype declaration, html, head, title and body tags. This is necessary because the
ui:composition is replaced with the template that contains its own set of ... tags".

I perhaps confusingly name the section in my content file (defined by ui:define) metadata although
it contains f:event which is outside f:metadata.

Regards,
Brendan.
 
Carlos Jorge Tavares Ferreira
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brendan,

You're absolutely right in what you wrote on your last post. I wrongly mentioned the <ui:composition> tag, when I wanted to refer the <ui:define>.
The solution was moving the <f:metadata><f:event ...></f:metadata> outside the <ui:define> tag.
My mistake, sorry.

Kind regards,

Carlos Ferreira
 
Karsten Wutzke
Ranch Hand
Posts: 106
Hibernate MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be warned, putting

results in undefined behavior:

http://andyschwartz.wordpress.com/2009/07/31/whats-new-in-jsf-2/#get-prerenderview-event

It might just be luck if it's working. It's fragile. Not putting it inside f:metadata might cause the listener method to not be called at some other point and you start wondering what broke. Don't do it!

If you're experiencing redundant calls to the preRenderView method, you have to resort to Seam 3 Faces' s:viewAction instead:

Docs: http://docs.jboss.org/seam/3/faces/latest/reference/en-US/html/components.html#viewaction

This is the predecessor of the upcoming JSF 2.2f:viewAction:

here
http://www.oracle.com/technetwork/articles/java/jsf22-1377252.html

Both Seam 3 Faces and JSF 2.2 give you the ability to control initial/postback request behavior, which JSF 2.0 f:event doesn't. Everything else are "hacks", also see here: http://stackoverflow.com/questions/2830834/jsf-fevent-prerenderview-is-triggered-by-fajax-calls-and-partial-renders-some

Karsten



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic