f:ajax action method is not invoked

11,030

Here,

<f:ajax event="change" action="#{requestBean.generateRequestId}" 
    execute="@form" render="requestId"/>

The action attribute is wrong. You're likely confusing with <h:commandButton action>. You need the listener attribute instead.

<f:ajax listener="#{requestBean.generateRequestId}" 
    execute="@form" render="requestId" />

(note that I removed the event attribute; it already defaults to change in case of <h:inputText>)

And here,

public void generateRequestId(ActionEvent e) 

The ActionEvent argument is wrong. You're likely confusing with <h:commandButton actionListener>. You need the AjaxBehaviorEvent argument instead.

public void generateRequestId(AjaxBehaviorEvent e) 

You could if necessary also remove it altogether if you don't need anything from it. It's optional.

public void generateRequestId() 

See also:

Share:
11,030
Admin
Author by

Admin

Updated on June 07, 2022

Comments

  • Admin
    Admin almost 2 years

    I am using JSF2 with rich faces 4. My aim is to display a request ID that is generated from a method in the backend into an input text field using ajax.

    Here is snippet of my code:


    ....
        <h:outputText value="Drawer No" /> 
        <h:outputText value="*" style="color:#ff0000;"/> 
        <h:inputText id="drawer" value="#{requestBean.drawerNumber}" required="true" 
             requiredMessage="Please enter drawer number">
            <f:ajax event="change" action="#{requestBean.generateRequestId}" 
                execute="@form" render="requestId"/>
        </h:inputText>
    
        <h:outputText value="Request Id" /> 
        <h:inputText id="requestId" value="#{requestBean.requestId}"  readonly="true" required="true" >
        </h:inputText>
    </td>
    <td>
        <h:message for="requestId"  style="color:red;font-size:10pt"/>
    ....
    

    Bean:

    public void generateRequestId(ActionEvent e) 
    {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        String emp=(String) session.getAttribute("loginUser");
        System.out.println("inside ajax event" + emp);
        try {
            requestId = AdminDAO.getRequestId(emp);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
    }
    

    Once the method is called, it would generate a request ID which needs to rendered back to request ID.

    This code is not even calling the method, and if I use <a4j:ajax instead of f:ajax, it is telling me that method is not present.