vendredi 3 juillet 2015

So i'm building the creation form for an entity called Reservation, it has a many-to-many relathionship with another entity (composant), and i'm trying to bind a collection of checkboxes inside a with the model attribute used to persist the entity, but i'm getting a 400 Bad request Http error on form submit. I'm passing the entity's (composant) id as a value to the checkbox, and using the foreach loop index to write the checkbox's path.

here are the relevent pieces of code:

edit.jsp :

<c:forEach var="comp" items="${listComps }" varStatus="loop">
                <tr>
                    <td> ${ comp.nom }</td>
                    <td>${ comp.libelle }</td>
                    <td>${ comp.typeComposant }</td>
                    <c:choose>
                        <c:when test="${empty comp.reservations }">
                            <td><span class="glyphicon glyphicon-ok"></span></td>
                        </c:when>
                        <c:otherwise>
                            <td><span class="glyphicon glyphicon-remove"></span></td>
                        </c:otherwise>
                    </c:choose>
                    <td><form:checkbox value="${comp.idComp}" path="composants[${loop.index}]"/></td>
                </tr>
            </c:forEach>

controller.java

binder.registerCustomEditor(List.class, "listComps",new CustomCollectionEditor(List.class){

        @Override
        protected Object convertElement(Object element) {
            Composant comp = new Composant();

            if (element != null) {
                Integer id =  Integer.valueOf(element.toString());
                comp.setIdComp(id);
            }
            return comp;
        }

@RequestMapping(value="/reservations/save",method=RequestMethod.POST)
public ModelAndView saveReservation(@ModelAttribute Reservation resa) {
    //System.out.println("reservation utilisateur login : "+resa.getUtilisateur().getLogin());
    Reservation resaFinale = this.resService.validerDateCreation(resa);
    this.resService.saveOrUpdate(resaFinale);
    return new ModelAndView("redirect:/reservations/");
}

Reservation.java ( the entity being created) :

@Entity
@NamedQuery(name="Reservation.findAll", query="SELECT r FROM Reservation r")
public class Reservation implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_reserv")
private int idReserv;

private int actif;

private String commentaire;

@Column(name="date_creation")
private Timestamp dateCreation;

@Column(name="date_liberation")
private Timestamp dateLiberation;

private int indice;

private String libelle;

private String nom;

//bi-directional many-to-one association to ReservAction
@OneToMany(mappedBy="reservation")
private List<ReservAction> reservActions;

//bi-directional many-to-one association to Environnement
@ManyToOne
@JoinColumn(name="id_env")
private Environnement environnement;

//bi-directional many-to-one association to Utilisateur
@ManyToOne
@JoinColumn(name="id_user")
private Utilisateur utilisateur;

//bi-directional many-to-many association to Composant
@ManyToMany
@JoinTable(
    name="resa_comp"
    , joinColumns={
        @JoinColumn(name="id_reserv")
        }
    , inverseJoinColumns={
        @JoinColumn(name="id_comp")
        }
    )
private List<Composant> composants;
// getter and setters 

and Finally here is what the http response looks like :

//the values here represent the comp.idComp in the checkbox value.
composants[0]=1
_composants[0]=on
 composants[1]=2
_composants[1]=on

Any help would be greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire