1

I am trying to log the number of the button clicks. 1. Should log the number of clicks though the form is invalid. The field value1 in the form is integer. So, It shall also consider conversion errors. 2. Action to be done at backing bean

I have tried with listener on ajax.

<h:form id="form">
      <h:inputText id="in" name="in" value="#{listenBean.value1}" autocomplete="off">       
      </h:inputText>
      <h:commandButton value="Click Me" action="#{listenBean.save}">    
        <f:ajax execute="@form" render="@form message eventcount" />
      </h:commandButton>
       <h:message for="in"/>
      Button Clicks: <h:outputText id="eventcount" value="#{listenBean.eventCount}"/>
</h:form>

Bean

public void eventCount(AjaxBehaviorEvent event) {
    //increment the counter
}

public void save() {
    //save
}

Issues: The listener method is not called when the conversion errors on input field binded to integer at bean. I enter the value as "some text". During thsi time listener is not called.

Version: Mojaraa 2.2.8

Is this the correct way of doing. Am I doing any mistake.

Can some one help me.

Patan
  • 17,073
  • 36
  • 124
  • 198

1 Answers1

3

The <h:outputText value> doesn't represent a method expression which should reference a bean (listener) method. It represents a value expression which should reference a bean property which will then be outputted as (escaped) text to the response.

Your best bet is to hook on preRenderView event of the component and check if the current request represents a postback request.

<h:form id="form">
    <h:commandButton ...>
        <f:ajax execute="@form" render="@form" />
    </h:commandButton>

    Button Clicks: 
    <h:outputText id="eventcount" value="#{listenBean.eventCount}">
        <f:event type="preRenderView" listener="#{listenBean.incrementEventCount}" />
    </h:outputText>
</h:form>
private int eventCount;

public void incrementEventCount(ComponentSystemEvent event) {
    if (FacesContext.getCurrentInstance().isPostback()) {
        eventCount++;
    }
}

public int getEventCount() {
    return eventCount;
}

Note that render="@form" covers the entire form already, so there's no need of specifying individual components inside the very same form. In case you've another ajax action inside the same form for which you'd like to not count the event, then make sure that render="..." is specific enough that it doesn't cover the eventcount component.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the clear answer. Is it possible to have same f:event on the commandButton as well adding the eventtypes as click? – Patan Jul 02 '15 at 09:34
  • 1
    Yes, you can have it on any component you want. And no, f:event listens on a server side event, not a client side event. Clicks don't occur on server. For an overview of valid (standard) f:event values, head to http://stackoverflow.com/questions/13999099/jsf-execution-order-of-fevents/ You can add custom ones too, a kickoff example to that can be found here http://stackoverflow.com/questions/13144611/cant-keep-faces-message-after-navigation-from-prerender/13144845#13144845 – BalusC Jul 02 '15 at 09:47