lundi 4 mai 2020

How to set checkboxes status with information from the server on a jsp using spring boot?

I am using spring boot and Jpa I have two objects with a many to many relationship. I can add into the database, but when I edit record I need to have the checkbox checked or unchecked depending the value in the database.

Object 1

public class EventAllotement {
    @Id
    @GeneratedValue
    private int idAllotement;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Temporal(TemporalType.DATE)
    private Date dateContractionAllotement;

    private String nomHotel;
    private String allotement;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Temporal(TemporalType.DATE)
    private Date dateRetroCessionAllotement;

    private String categorieHotel;
    private String capaciteChambre;
    private String capaciteSuite;

    @ManyToOne
    private Event event;

    @ManyToMany(mappedBy = "eventAllotements")
    private List<CategorieChambre> categorieChambres = new ArrayList<>();

Object 2

@Entity
public class CategorieChambre {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int idCategorieChambre;
    private String designationCategorieChambre;

    @Transient
    private boolean selected;

    @ManyToMany
    @JoinTable(name = "CATEGORIE_CHAMBRE_EVENT_ALLOTEMENT", joinColumns = @JoinColumn(name = "ID_CATEGORIE_CHAMBRE"), inverseJoinColumns = @JoinColumn(name = "ID_EVENT_ALLOTEMENT"))
    private List<EventAllotement> eventAllotements = new ArrayList<>();`

This is the controller

@RequestMapping(value = "/modifiereventallotement", method = RequestMethod.GET)
    public String afficherModifierDetailArticle(@RequestParam int idAllotement, ModelMap model) {

        List<CategorieChambre> categorieChambres = categorieChambreRepository.findAll();
        EventAllotement eventAllotement = eventAllotementRepository.findByIdAllotement(idAllotement);

        List<CategorieChambre> categorieChambreSelecteds = categorieChambreRepository
                .findCategorieChambreByEventAllotementId(idAllotement);

        List<CategorieChambre> categorieChambreNonSelecteds = new ArrayList<>(categorieChambres);
        categorieChambreNonSelecteds.removeAll(categorieChambreSelecteds);

        for (CategorieChambre categorieChambreSelected : categorieChambreSelecteds) {
            categorieChambreSelected.setSelected(true);

        }

        for (CategorieChambre categorieChambreNonSelected : categorieChambreNonSelecteds) {
            categorieChambreNonSelected.setSelected(false);

        }

        List<CategorieChambre> categorieChambreModifications = new ArrayList<>(categorieChambres);
        for (CategorieChambre categorieChambreModification : categorieChambreModifications) {
            System.out.println("Categorie chambre séléctionée et non selectionée" + categorieChambreModification);

        }

        model.put("eventAllotement", eventAllotement);
        model.put("categorieChambres", categorieChambreModifications);

        return "form_ajouter_event_allotement";
    }

This the JSP page

<div class="col-md-12 col-sm-12 col-xs-12">

                                                <div class="col-md-3 col-sm-4 col-xs-12">
                                                    <h4>Categorie Chambre</h4>
                                                    <div class="form-group">
                                                        <div class="checkbox col-md-12 col-sm-12 col-xs-12">


                                                            <c:forEach items="${categorieChambres}"
                                                                var="categorieChambre">

                                                                <c:set var="isChecked"
                                                                    value="${categorieChambre.selected}">

                                                                </c:set>



                                                                <form:label path="categorieChambres"
                                                                    class="col-md-7
                                                                    col-sm-12col-xs-12">
                                                            ${categorieChambre.designationCategorieChambre}
                                                            </form:label>

                                                                <c:out value="${categorieChambre.selected}">
                                                                </c:out>

                                                                <c:choose>
                                                                    <c:when test="${categorieChambre.selected}">

                                                                        <form:checkbox path="categorieChambres" class="js-switch  col-md-5 col-sm-12 col-xs-12"
                                                                            checked="checked"/>

                                                                    </c:when>

                                                                    <c:otherwise>
                                                                        <form:checkbox path="categorieChambres" class="js-switch  col-md-5 col-sm-12 col-xs-12"
                                                                             checked="checked" />

                                                                    </c:otherwise>  
                                                                </c:choose>


                                                            </c:forEach>

                                                        </div>

                                                    </div>
                                                </div>

The state of the checkbox element is returned to the jsp as true and false, but I cannot link it to the checkbox. I get an error message saying thatAttribute 'value' is required when binding to non-boolean values. Any help is much appreciated. As you probably can guess I am fairly new to all this. Thank you.




Aucun commentaire:

Enregistrer un commentaire