Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2 Prerenderview Listener Not Called When Page Called From Action Method Outcome

I am calling a page from another with a prerender view event in an f:metadata element.

If I navigate to page using <h:link> it works and calls the listener method.

However if I navigate to the page using an outcome from the action method of a managed bean belonging to the calling page, the listener designated in the prerenderview doesn't get called (as it does if called by a link). It does navigate to the second page, just no listener call.

I would really prefer to call from the action method because I use it to do some work and place a variable in the session map for the called page to use. I'm not sure how to achieve the same thing using a link. The object can be rather large... kb not Mb but still not something I want to put in the request.

I've tried making the managed bean of the calling page request scoped and view scoped.

Is it not possible to get a prerenderview to fire if it is called from a managed bean outcome? As I said, I got it to work from a link.

<body>
    <ui:define name="metadata">
        <f:view>
            <f:metadata>
                <f:event type="preRenderView" listener="#{businessBean.init}" />
            </f:metadata>
        </f:view>
    </ui:define>
    <ui:composition template="#{navigationprops.soulard_2col_uprefs_template}">
like image 949
Bill Rosmus Avatar asked Feb 19 '23 01:02

Bill Rosmus


1 Answers

ui:composition tag trim everything outside it, so facelets compiler doesn't have the chance to read the code. Instead, you should use ui:decorate, but remember f:metadata tag only works on the top level page, not inside the template client. For example:

<ui:composition 
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:metadata>
    <f:viewParam name="hotid" 
       value="#{hotelBooking.hotelId}" 
       converter="javax.faces.Long"/>
    <f:event type="preRenderView" listener="#{hotelBooking.selectHotel}"/>
</f:metadata>
<ui:decorate template="template.xhtml">
   <ui:define name="content">
   <!-- ... -->
   </ui:define>
</ui:decorate>
</ui:composition>

See This example for details.

like image 161
lu4242 Avatar answered Apr 28 '23 17:04

lu4242