By user2775499


2013-09-13 09:11:15 8 Comments

I have a custom tagfile with a form:

<h:form>
    <h:commandButton value="click">
        <f:ajax event="click" listener="#{bean[method]}" />
    </h:commandButton>
</h:form>

I'm conditionally rendering it by ajax as below:

<h:panelGroup id="test">
  <h:form>
    <h:commandButton value="click">
      <f:ajax event="click" listener="#{backingTest.updateFlag}" render=":test"/>
    </h:commandButton>
  </h:form>
  <h:panelGroup rendered="#{backingTest.flag}">
    <my:customtag bean="#{backingTest}" method="printMessage"/>
  </h:panelGroup>
</h:panelGroup>

This is the associated backing bean:

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

@ManagedBean
@RequestScoped
public class BackingTest {

    private boolean flag = false;

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

    public void updateFlag() {
        flag = true;
    }

    public boolean getFlag() {
        return flag;
    }
}

When I click the first command button, then the updateFlag() method is properly invoked and the second command button is properly shown. But when I then click the second command button, it never hits the printMessage() method. In the web browser's JS console and HTTP traffic monitor I can see that the click event is successfully fired and that the XHR POST request is successfully being sent.

If I remove the rendered attribute, then everything works as expected.

How is this caused and how can I solve it? I'm using Mojarra 2.1.25.

1 comments

@BalusC 2013-09-13 11:55:38

Your concrete problem is caused by 2 facts:

  1. When JSF needs to decode a form submit action, it also checks if the component is rendered or not (as safeguard against hacked/tampered requests).
  2. Request scoped beans are recreated on every HTTP request (an ajax request counts also as one request!).

In your specific case, the rendered condition has evaluated false while JSF needs to decode the form submit action and therefore the non-rendered input/command components are never processed.

Putting the bean in view scope should fix it. Below example assumes JSF 2.x.

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped

And below example assumes JSF 2.2+ with CDI:

import javax.inject.Named;
import javax.faces.view.ViewScoped;

@Named
@ViewScoped

See also:

@Arpit Parasana 2019-05-24 17:59:56

I have the similar problem. Is there any work around that you could achieve a solution with request scoped bean? As in my case, web application is already huge and the bean has to have Request scope for several other reasons.

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] jQuery AJAX submit form

1 Answered Questions

f:ajax listener not working with h:selectOneRadio

2 Answered Questions

3 Answered Questions

[SOLVED] JSF f:ajax listener not called

  • 2012-03-24 00:56:16
  • twmb
  • 14987 View
  • 0 Score
  • 3 Answer
  • Tags:   ajax jsf listener

2 Answered Questions

[SOLVED] JSF ajax event queue doesn't work on action-event after change-event

1 Answered Questions

[SOLVED] f:ajax listener not executed because of rendered="false"?

  • 2012-12-03 08:28:19
  • nablex
  • 298 View
  • 0 Score
  • 1 Answer
  • Tags:   jsf

1 Answered Questions

[SOLVED] JSF2 f:ajax render on a panel holding a ui:repeat doubles data once

  • 2012-07-31 14:51:42
  • casolorz
  • 705 View
  • 0 Score
  • 1 Answer
  • Tags:   jsf jsf-2 el mojarra

1 Answered Questions

[SOLVED] Why does commandButton action listener does not working?

1 Answered Questions

Sponsored Content