jeudi 10 octobre 2019

form:checkbox not working in Spring MVC JSP page

I have the following Model class:

public class BlocchettiScratchCards implements Serializable {
      ...
      private boolean flagNuovaGestione;

      public boolean isFlagNuovaGestione() {
        return flagNuovaGestione;
      }

      public void setFlagNuovaGestione(boolean flagNuovaGestione) {
        this.flagNuovaGestione = flagNuovaGestione;
      }
}

and the Bean class:

public class BlocchettiScratchCardsBean implements Serializable {
      ...
      private boolean flagNuovaGestione;
      public boolean isFlagNuovaGestione() {
        return flagNuovaGestione;
      }

      public void setFlagNuovaGestione(boolean flagNuovaGestione) {
        this.flagNuovaGestione = flagNuovaGestione;
      }
}

in the JSP we have a <form:form> tag with the following:

<form:checkbox path="flagNuovaGestione"/>

The controller class initialize the property as follows:

BlocchettiScratchCardsBean elencoCards = new BlocchettiScratchCardsBean();        
elencoCards.setFlagNuovaGestione(true);

but when the page is loaded, the checkbox is not checked by default as I expect.

Inspecting HTML of page, I see the following:

<input id="flagNuovaGestione1" name="flagNuovaGestione" type="checkbox" value="true">
<input type="hidden" name="_flagNuovaGestione" value="on">

but the checked property is not set.

Why do I have this behaviour and how could I solve this issue?




Aucun commentaire:

Enregistrer un commentaire