5

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?

angrybirds
  • 51
  • 1
  • 2
  • Works fine here with normal JSF 2.0 annotations on the bean. Does it work for you as well with `@RequestScoped` or `@ViewScoped`? If so, then the problem is in your conversation scope thing. I'd then revise the question as such. – BalusC Feb 15 '11 at 02:41
  • @BalusC Thank you - I'll do some more investigation to see if it's just something about ConversationScoped beans. – angrybirds Feb 21 '11 at 21:32
  • I am having a similar problem. Did you solve it? https://stackoverflow.com/questions/43955873/jsf-ajax-called-only-1st-time – user1156544 May 15 '17 at 11:25

4 Answers4

5

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.

Community
  • 1
  • 1
hyang04
  • 121
  • 2
  • 3
  • 3
    This is covered by point 3 of http://stackoverflow.com/a/2120183. You didn't have a `` on the `` and hence was never notified of the `required="true"` error. This should however have logged a warning line in server log, or at least have rendered a development stage message list if `javax.faces.PROJECT_STAGE` is set to Development. By the way, as your bean is view scoped, the `` on a `` does technically not make much sense. You may want to look at OmniFaces `` http://showcase.omnifaces.org/components/viewParam – BalusC Mar 22 '13 at 19:41
  • 1
    I always pass in the "id" parameter in the URL in the above example, and in that case one button always works, but the other only works once. BalusC your point on needing to have "h:message" to notify the user when the required parameter is missing, is valid, though this is indeed a different case from your point 3 on the other post. – hyang04 Mar 26 '13 at 18:19
  • 2
    Had the same problem and having the `````` on my `````` would have notified me of the "required" error and saved me countless hours... finally fixed it using ```required="#{!facesContext.postback}"``` https://stackoverflow.com/questions/43955873/jsf-ajax-called-only-1st-time/46678427#46678427 – Mike R Oct 11 '17 at 01:43
  • same problem with 5 hours :( – Mohammad Yahia Oct 14 '19 at 20:24
3

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>
hopeman
  • 153
  • 1
  • 9
1

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

Botz3000
  • 39,020
  • 8
  • 103
  • 127
0

I had the same bug, fixed it using Primeface's p:ajax instead of f:ajax.

Thomas Schmidt
  • 1,248
  • 2
  • 12
  • 37