1

Is there an autoload command within JSF or PrimeFaces?

I have a JSF template that I use for my facelets:

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

    <h:form>
        <p:remoteCommand name="onload" action="#{mainMenuBean.setMenuCategories}" autoRun="true" />
    </h:form>

    <ui:define name="west">
        <ui:include src="mainmenu.xhtml" />
    </ui:define>

    <ui:define name="center">
        <p:outputLabel value="Zentrum" />
        <h:form>
            <p:remoteCommand name="rc" update="msgs" actionListener="#{mainMenuBean.setMenuCategories()}" />

            <p:growl id="msgs" showDetail="true" />
            <p:commandButton type="button" onclick="rc()" value="Execute" icon="ui-icon-refresh" />
        </h:form>
    </ui:define>
</ui:composition>

Now the p:remoteCommand does not work for some reason. Is there are command that I could place instead of p:remoteCommand to automatically execute some bean method everytime it gets loaded?

Socrates
  • 8,724
  • 25
  • 66
  • 113
  • Search for 'method on page load jsf stackoverflow' on google and pick one that you think is an answer to your question and then please flag yours as a duplicate or remove it. – Kukeltje Aug 30 '15 at 10:10
  • Thank you @Kukeltje for the link. This adds a great and very complete explanation to this whole subject. Regarding this case here the answer below did pretty much exactly what I was looking for. Always nice though to have the possibility to go into greater detail. Thanks again. – Socrates Aug 30 '15 at 11:15

1 Answers1

2

JSF 2.2 has viewAction, which can be executed on each load or on each get (and skipped on postbacks). It's placed in meta part of page:

  <f:meta>
    <f:viewAction action="#{anyBean.anyAction}"/>
  </f:meta>

edit: mind that this is an action component, so it's execution will depend on any preceding validations / conversions being successful.

fdreger
  • 12,264
  • 1
  • 36
  • 42