Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF2.0 with <f:ajax> only works once

Tags:

jsf

jsf-2

I'm having a problem with the tag in JSF2.0 and I hope someone can point out what I'm doing wrong. Here's what I've got in the UI:

<h:panelGroup>
  <h:form id="theForm">
    <h:selectOneMenu id="theMenu" value="#{viewBean.selectedItem}">
        <f:ajax event="change" render="selectedItemText"/>
    <f:selectItem itemLabel=""/>
    <f:selectItems value="#{viewBean.selectableItems}"/>
    </h:selectOneMenu>
    <h:outputText id="selectedItemText" value="#{viewBean.selectedItemText}" />
  </h:form>
</h:panelGroup>

This is working great - my conversation-scoped backing bean has a method setSelectedItem, and it's called and it does its thing the first time I select a different item from the menu; the output text is updated in the frontend, and I'm happy. However, further changes to the menu selection do not trigger a call to the setter via ajax. I've also tried this with a listener on the f:ajax tag - the listener method is only called that first time as well (breakpoints in the code to figure this out).

Am I doing something incorrectly?

like image 208
angrybirds Avatar asked Feb 15 '11 00:02

angrybirds


3 Answers

I had a similar problem.

My second commandButton below only works once in the JSF view below that has a view param. Adding <f:param> to my first commandButton solved the problem. This is a situation not covered by BalusC's very helpful discussion.

<!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:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">

<f:view>
    <f:metadata>
        <f:viewParam name="id" value="#{fooManager.millis}" required="true"/>
    </f:metadata>

    <h:head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    </h:head>

    <h:body>
        <h:form id="fooForm">
            <h:commandButton
                    id="barBbutton"
                    value="foo:test"
                    action="#{fooManager.test}">
                <f:param name="id" value="1"/>
                <f:ajax render="fooMillis1"/>
            </h:commandButton>

            <p>
                <h:outputText id="fooMillis1" value="foo:display1: #{fooManager.millis}"/>
            </p>
            <p>
                <h:outputText id="fooMillis2" value="foo:display2: #{fooManager.millis}"/>
            </p>
        </h:form>
        <h:form id="barForm">
            <h:commandButton
                    id="barButton"
                    value="bar:test"
                    action="#{barManager.test}">
                <f:ajax render="barMillis1"/>
            </h:commandButton>

            <p>
                <h:outputText id="barMillis1" value="bar:display1: #{barManager.millis}"/>
            </p>
            <p>
                <h:outputText id="barMillis2" value="bar:display2: #{barManager.millis}"/>
            </p>
        </h:form>
    </h:body>
</f:view>
</html>

And my FooManager and BarManager look the same:

@ManagedBean
@ViewScoped
public class FooManager {

    public long getMillis() {
        return millis;
    }

    public void setMillis(long millis) {
        this.millis = millis;
    }

    public void test() {
        setMillis(System.currentTimeMillis());
    }
    private long millis;

}

When it is not working, my Weblogic/Mojarra library does not give any helpful hint. There is no error anywhere. It was only after numerous tries that I came up with a working button like the first one above.

like image 141
hyang04 Avatar answered Nov 04 '22 15:11

hyang04


I had the same issue. For the code below ajax was run only once.

<h:inputText id="element_id" value="#{viewBean.someValue}"></h:inputText>   
<h:commandLink action="#{viewBean.someAction}" value="click me">
     <f:ajax render=":my_form:another_element" execute="element_id> </f:ajax>
</h:commandLink>

When I add to render attribute the element which I'm executing then the ajax is triggered every time.

<h:inputText id="element_id" value="#{viewBean.someValue}"></h:inputText>   
<h:commandLink action="#{viewBean.someAction}" value="click me">
     <f:ajax render=":my_form:another_element element_id" execute="element_id> </f:ajax>
</h:commandLink>
like image 45
hopeman Avatar answered Nov 04 '22 14:11

hopeman


I had a similar problem, in my case everithing worked fine in all browsers except that in IE9 the ajax was fired only once.

I was using render="@form" and when I changed it to render="@all", it worked fine. I dunno why, since I only have one Form in that page, and all my components are in that form.

So I would advise you to check the render and execute tags, at least in my case it was the solution

like image 24
Benjamin Torres Avatar answered Nov 04 '22 14:11

Benjamin Torres