2

I have a problem with my Mojarra 2.1.6 web-application, I'm developing it using @ViewScoped managed beans and each bean is attached to an xhtml page. This page is receiving some view params and after initializing the bean in that way:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui"
template="/templates/general_template.xhtml">

<ui:define name="metadata">
    <f:metadata>
        <f:viewParam id="user" name="user"
            value="#{navegableUserData._ParamUser}" />

        <f:viewParam id="NavIndex" name="NavIndex"
            value="#{navegableUserData._QueueIndex}" />
        <f:event type="preRenderView"
            listener="#{navegableUserData.initialize}" />
    </f:metadata>
    <h:message for="user" />
</ui:define>

<ui:define name="general_content">
    <p:outputPanel autoUpdate="false" id="Datos_Loged" name="Datos_Loged"
        layout="block">
        <h:form id="SystemUserForm">
            <ui:include
                src="/system/manage_user/content/edit_user/system_user_data/system_user.xhtml">
                <ui:param name="manager" value="#{navegableUserData}" />
            </ui:include>
        </h:form>
    </p:outputPanel>
</ui:define>

As you can see, I have my pages nested into a general template which looks like that:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui" xmlns:o="http://omnifaces.org/ui">


<h:head>
    <meta http-equiv="Pragma" CONTENT="no-cache"></meta>
    <meta http-equiv="cache-control" content="no-cache"></meta>
    <meta http-equiv="Expires" CONTENT="-1"></meta>
    <meta http-equiv="Content-Type"
        content="text/html; charset=ISO-8859-15" />
    <h:outputStylesheet library="css" name="prime_styles.css" />
    <h:outputScript library="js" name="prime_translations.js" />
</h:head>

<h:body>
    <ui:insert name="metadata" />
    <o:importConstants
        type="com.company.system.view.beans.NavigationResults" />

<!-- More stuff -->

Problem comes when I make an ajax request such as a Primefaces table filtering. Although my backing bean is not being created again, <f:event type="preRenderView" listener="#{navegableUserData.initialize}" /> is being called again.

I'm doing a data loading based into view params and need that method to execute only when the page is rendered first time. I have been very careful using <c:xxx> tags and think that's not the problem because I used them only in general template and my view beans properties are not attached to them. Also I have this problem with all my pages, so I think it is not an issue of an specific backing bean.

Aritz
  • 30,971
  • 16
  • 136
  • 217
  • Which version of primefaces? The problem may be there. Make sure you have the latest version. You should also try upgrading to Mojarra 2.1.14 (2.1.15/16 there is an issue in combination with Primefaces so that's why I'm not recommending it), just to rule out that it is caused by a bug that has already been fixed. – Gimby Jan 04 '13 at 09:03
  • Tried with Prime 3.4.2 and Mojarra 2.1.14 and I still have the same issue. Ajax request is not specifically done by myself, but by PF dataTable filters. – Aritz Jan 04 '13 at 09:10
  • 2
    Related: http://stackoverflow.com/questions/6377798/what-can-fmetadata-and-fviewparam-be-used-for/6377957#6377957 – BalusC Jan 04 '13 at 12:12

2 Answers2

11

If this occur only during ajax request, try the following:

public void initialize() {
    if (!FacesContext.getCurrentInstance().isPostback()) {
        // -----------
        // -----------
    }
}

Related as BalusC suggested:

Community
  • 1
  • 1
Johny T Koshy
  • 3,857
  • 2
  • 23
  • 40
1

Just for completeness, apart from the method @jonhy mentions, there's a cleaner choice if using JSF 2.2: Using f:viewAction instead of f:event.

<f:viewAction action="#{navegableUserData.initialize}" />

With that, it's not necessary to wrap your server code with if (!FacesContext.getCurrentInstance().isPostback()), since basically the method itself doesn't get called, because by default this actions are not executed on postbacks.

You alternatively can set them to be, which would be the same behavior as using the original f:event:

<f:viewAction action="#{navegableUserData.initialize}" onPostback="true" />

See also:

Aritz
  • 30,971
  • 16
  • 136
  • 217
  • @ChRoNoN It will in a template client (where `ui:define` happens). Using it in the template itself doesn't make much sense since it's intended to initialize a concrete bean, which will usually be `@ViewScoped`. – Aritz Feb 12 '19 at 19:21
  • What I meant was to execute something everytime the page is called (log in check? Access Control?) it will not work if you put it in the master template, you need to put it in each file that uses the master template. I believe viewAction is mainly to execute something in a specific place while rendering the page. Bean initialize suppost to use @PostConstruct – ChRoNoN Feb 12 '19 at 20:55
  • Security checks are better placed in a web filter rather than in the view itself.. The difference between view action and post construct is that JSF does call setters before the first happens (which is valid to have url parameters and so on) while the second gets called right after the constructor is. So first one is much more handy for initing the bean with the values to be shown. – Aritz Feb 12 '19 at 21:47