0

I'm trying to create a button that once clicked will change a property in a bean.

<h:commandButton type="button" action="#{loginBean.withdraw}" id="thousand" class="buttons"     style="top:180px;left:570px;">
    <f:setPropertyActionListener target="#{loginBean.withdrawAmount}" value="1000" />
</h:commandButton>

   public class LoginBean {

       int withdrawAmount;

This method only works when I omit the type="button" from the commandButton, but with the type="button" it doesn't work and I'm not sure why.

I need the type="button" to be there , is there any way to keep it and still make it work ?

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
Dani Gilboa
  • 599
  • 4
  • 13
  • 30

2 Answers2

2

There is an error in your facelet snippet:

  • There is no such attribute as class for <h:commandButton>. Possibly you meant styleClass.

As for the problem you have, you have to:

Either provide a setter method for the withdrawAmount property

public void setWithdrawAmount(int withdrawAmount) {
    this.withdrawAmount = withdrawAmount;
}

and your facelet should look like:

<h:commandButton type="submit" 
                 action="#{loginBean.withdraw}" 
                 id="thousand" 
                 styleClass="buttons" 
                 style="top:180px;left:570px;">
    <f:setPropertyActionListener target="#{loginBean.withdrawAmount}" 
                                 value="1000" />
</h:commandButton>

Or, you can get rid of the <f:setPropertyActionListener> and add a statement the changes the value of the withdrawAmount as a first line of the #{loginBean.withdraw} method.

In this case your facelet snippet should look like:

<h:commandButton type="submit" 
                 action="#{loginBean.withdraw}" 
                 id="thousand" 
                 styleClass="buttons" 
                 style="top:180px;left:570px;" />

and your LoginBean#withdraw() method should start with the statement, that changes the withdrawAmount value:

public String withdraw() {
    this.withdrawAmount = 1000;
    //the remaining logic.
}

Personally, I would prefer the first option.


More info:

Community
  • 1
  • 1
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • 1
    `button` is [a valid option](http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/). That's the variant used to trigger GET requests – kolossus Dec 14 '13 at 19:37
2

The type is the entire reason why you're having this issue. I'm posting this answer because the accepted answer doesn't explain why you're experiencing the issue.

<h:commandButton/> is designed to work in 3 modes:

  • submit: This is the default mode that the button is set to. This mode sends an HTTP POST request to the server that triggers the JSF request processing lifecycle. It's only this mode that enables you to trigger backing bean methods(using the action or actionListener attributes).

  • button: This mode triggers a GET request in the application. As GET requests go, this mode is mostly suited for navigation, i.e. requesting another view or page. In this mode, there's no easy/straightforward way to execute backing bean code, or trigger the JSF request processing lifecycle. This is your current issue

  • reset: This mode simply resets the value of all input components within its enclosing <h:form/>

Reference:

Community
  • 1
  • 1
kolossus
  • 20,559
  • 3
  • 52
  • 104