Karthik Prasad
Karthik Prasad

Reputation: 10004

f:ajax method gets called on load of the jsf page

I've simple 3line jsf page and a backing bean. I'm using command button in my page. Onclick event I'm calling ajax. where ajax method adds an output text component to child. Now the problem is. Out put text component is rendered on load of the page rather than on click of the page.

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <h:commandButton id="button1" action="return false" value="Submit">
                <f:ajax execute="#{bb.method1()}" render="@form" transient="true" event="click" />
            </h:commandButton>
        </h:form>
    </h:body>
</html>

Backing bean code

@Named("bb")
@SessionScoped
public class BackingBean implements Serializable {

public BackingBean() {
}

public void method1()
{
    HtmlOutputText out=new HtmlOutputText();
    out.setValue("Text1");
    FacesContext context=FacesContext.getCurrentInstance();
    context.getViewRoot().getChildren().add(out);
    context.getViewRoot().setTransient(true);
}   
}

on click of button firebug show status 200 OK.. But I can see no changes at the page

Upvotes: 0

Views: 6043

Answers (1)

BalusC
BalusC

Reputation: 1108642

The execute attribute should refer a space separated collection of client IDs of input components which needs to be processed during ajax submit. This attribute is evaluated when the view get rendered and any method bound to it thus get invoked immediately.

You should be using the listener attribute instead.

<f:ajax listener="#{bb.method1()}" render="@form" transient="true" event="click" />

The same is true for the render attribute.

See also:

Upvotes: 2

Related Questions