1

I am seeing a bizarre defect. Here is a simple test case.

The managed bean is:

@ManagedBean
public class Controller {
    private int counter = 0;
    //getter and setters...
    public String next() {
        ++counter;
        return null;
    }
    public String prev() {
        --counter;
        return null;
    }
}

The view is:

<h:form>
<p>Value: #{controller.counter}</p>
<h:inputHidden value="#{controller.counter}" />
<h:commandButton action="#{controller.prev()}" value="Previous" disabled="#{controller.counter == 0}"/>
<h:commandButton action="#{controller.next()}" value="Next"     disabled="#{controller.counter == 5}"/>
</h:form>

When the view is first displayed, the Previous button is disabled. When I click Next, the Previous button becomes enabled. So far so good. But, when I click the Previous button, the action handler method prev() never gets called. If I remove the disabled attribute for the buttons, then everything works fine. Am I doing something wrong or is there a defect in Mojarra? I am using JBoss 7.1 and Mojarra. Thanks.

RajV
  • 6,860
  • 8
  • 44
  • 62

1 Answers1

0

That can happen if your bean is request scoped and you don't preinitialize counter property during (post)construction based on a request parameter. This way the button is still disabled when JSF is about to apply the request values. As part of safeguard against tampered/hacked requests, JSF won't queue the action event then.

Placing the bean in the view scope, or making sure that you properly preinitialize the associated property in the (post)constructor based on a request parameter should fix it. The <h:inputHidden> which you've there has by the way totally no value in this construct. If you make the bean view scoped, just get rid of it. If you want to stick to the request scope, replace it by <f:param> in the both buttons and set them using @ManagedProperty.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks. I see that in my current code, count will be always 0 during apply values phase. Hence, the Previous button will always be evaluated as disabled in that phase. I changed the code to use @ViewScoped and removed the and everything works now. – RajV Oct 05 '12 at 22:51