尽管表单无效,但记录提交按钮的点击次数

Log the number of submit button clicks though the form is invalid

我正在尝试记录按钮点击次数。 1.虽然表格无效,但应该记录点击次数。表单中的字段 value1 是整数。因此,它还应考虑转换错误。 2. backing bean要完成的动作

我已经尝试在 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>

豆子

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

public void save() {
    //save
}

问题: listener method is not called 当输入字段绑定到整数时出现转换错误 bean。我输入的值为 "some text"。在此期间不调用侦听器。

版本:Mojaraa 2.2.8

这是正确的做法吗?我是不是做错了。

谁能帮帮我。

<h:outputText value> 不表示应引用 bean(侦听器)方法的方法表达式。它表示一个值表达式,该表达式应引用一个 bean 属性,然后将其作为(转义的)文本输出到响应。

最好的办法是挂钩组件的 preRenderView 事件并检查当前请求是否表示回发请求。

<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;
}

请注意,render="@form" 已经涵盖了整个表单,因此无需在同一个表单中指定各个组件。如果您在同一表单中有另一个 ajax 操作,您不想将其计入事件,那么请确保 render="..." 足够具体,不会涵盖 eventcount 组件.