0

I have a PrimeFaces inputSwitch:

<p:inputSwitch id="snoozeSwitch" value="#{dashBController.snooze}" valueChangeListener="#{dashBController.updateSnoozeStatus}">
    <p:ajax listener="#{dashBController.updateSnoozeStatus}" update="msgSnooze" />
</p:inputSwitch>

Now I want to give this switch an initial value from my DB. I get the value with:

@PostConstruct
public void init() {
    if (!FacesContext.getCurrentInstance().isPostback()) {
        snooze = getSnoozeStatus();
    }
}

But I have trouble to update the old value with the new one.

public void updateSnoozeStatus() {
  if(snooze == true) {
    DBconnector.updateSnooze("true");
  } else {
    DBconnector.updateSnooze("false");
  }
}

The problem: My app always uses the inital value and so the switch is stucking on the state it has originally. Maybe @BalusC knows the answer :)

Marcel
  • 1,537
  • 5
  • 19
  • 38
  • Where you defined this method: `updateSnoozeStatus` that is the one you are calling on the listener attribute – Jorge Campos Aug 13 '15 at 13:20
  • I am sorry. updateSnooze() actually is updateSnoozeStatus() - missed it in my transfer to stackoverflow. – Marcel Aug 13 '15 at 13:22
  • @Tiny which work around could solve the problem? – Marcel Aug 13 '15 at 13:28
  • 1
    Why that `valueChangeListener`? Get rid of it and reverify your question. – BalusC Aug 13 '15 at 21:15
  • I removed the `valueChangeListener` - but it does not solve the problem. Can PrimeFaces inputSwitch handle objects of type **Boolean** instead of the primitive type **boolean**? (compare to answer below from @ForguesR – Marcel Aug 14 '15 at 09:00
  • I had a similar problem that i resolved using immediate="true" – ℛɑƒæĿᴿᴹᴿ Jan 22 '19 at 19:53
  • What is the scope of your bean ? What is the declaration of snooze ? See also : https://stackoverflow.com/questions/11879138/when-to-use-valuechangelistener-or-fajax-listener – fdelsert Jul 18 '19 at 10:05

1 Answers1

0

Unless I am missing something your solution seems complex for something very simple. Why don't you just use a getter and a setter?

The getter should load the value from the database and the setter should save it. I couldn't test the code below because I don't have PF 5.0.4 but it should be a good start.

public class DashBController {

  private Boolean snooze = null;

  public boolean isSnooze() {
    if (snooze == null) {
      snooze = getSnoozeStatus();
    }
    return snooze;
  }

  public void setSnooze(boolean snooze) {
    this.snooze = snooze;
    DBconnector.updateSnooze(this.snooze.toString());
  }

}

and simply :

<p:inputSwitch id="snoozeSwitch" value="#{dashBController.snooze}"/>
ForguesR
  • 3,558
  • 1
  • 17
  • 39
  • It seems that PrimeFaces can not handle Objects of type Boolean instead of the primitive data type boolean. Is that possible? Getting some errors I can not get rid of ... – Marcel Aug 14 '15 at 08:56
  • Quite possible. I edited my answer : now the getter and the setter use the primitive `boolean`. Can you give it a try? – ForguesR Aug 14 '15 at 13:25
  • If I use the primitive instead, I can not check if snooze == null. So I have changed it like: `public boolean isSnooze() { if (snooze == null) { snooze = getSnoozeStatus(); //return Boolean or Downcast } return snooze.booleanValue(); }` – Marcel Aug 14 '15 at 14:10
  • In my answer `snooze` is still a `Boolean` : I only changed the return type of the getter and the parameter type of the setter. – ForguesR Aug 14 '15 at 14:16
  • I've implemented and tested your suggestion. But if I toggle the switch, it does not run my setSnooze() method. – Marcel Aug 15 '15 at 20:17