0

JSF commandbutton works on second and subsequent click but not on the initial click. I am working with JSF 2.2!

I see that there is a work around to get this to work by adding a selectBooleanCheckbox tag, forcing each click in checkbox call JSF request and don't wait for commandButton submit. In this way when commandButton is clicked all checkbox was processed, as found in this post.

I would want to know if there is a way to get the commandbutton itself to work for the first click without having to add any additional checkboxes. Any ideas?

Below is the code-snippet that I am working on,

<h:commandButton event="click" value="Send">
  <f:ajax listener="#{managedBeanName.methodToExcecute}"/>
</h:commandButton> 
Community
  • 1
  • 1
chan
  • 1
  • 1
  • Is this command button part of a form whose parent component is being rendered by another ajax request beforehand? – BalusC Apr 08 '16 at 07:31
  • yes, that's right. The component is being rendered by ajax request (through richfaces's a4j:commandButton). – chan Apr 08 '16 at 16:30
  • Okay. The answer is in the duplicate. In the future try creating a MCVE. Act as if you're trying to answer your own question and try to reproduce the described problem in a scratchpad project based on solely the information provided so far. You'll quickly see that your question is incomplete and that the real problem is elsewhere. See also http://stackoverflow.com/tags/jsf/info – BalusC Apr 08 '16 at 19:12

1 Answers1

-2

The below code works in JSF 2.2.4 with Java 1.8_73 in tomcat 8.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
    <title>test page</title>
</h:head>
<h:body>
    <h:form>
        <h:commandButton event="click" value="Send">
            <f:ajax listener="#{myBean.methodToExcecute}" />
        </h:commandButton>
    </h:form>
</h:body>
</html>

Managed Bean

package com.test;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class MyBean implements Serializable {

    public void methodToExcecute() {
        System.out.println("methodToExcecute");
    }
}

On First Click.. Console log : methodToExcecute

Ravi
  • 361
  • 1
  • 16
  • I am running it on JBoss App Server. No change in rest of the code except that I had a Session Scope earlier. I tried with Request Scope but somehow my application doesn't accept it. Again, it works the same way with View Scope, it gives required output but the button has to be clicked twice. – chan Apr 07 '16 at 21:50
  • 1
    All due respect, "Works for me" is not an answer which solves the concrete problem stated in the question and it's therefore entirely unhelpful. The asker itself is also very aware that this code should work, but it just doesn't for them (and this is usually also the sole reason the asker posts a question here; the asker just want to get them problem explained/solved). Any "Works for me" post which doesn't at all explain/solve the problem is therefore entirely unhelpful. Next time you can't reproduce a problem based on the information provided so far, just post a comment on the question. – BalusC Apr 08 '16 at 07:47