Spring MVC, load objects attributes in a jsp

时间:2018-02-26 18:00:40

标签: spring jsp spring-mvc models

I got an issue when trying to load an object attribute in a jsp file.

The model contains a list of objects of type "Evaluation", for each element in the list, all the attributes are correctly loaded except the ones that it has to fetch from another table.

The .jsp file :

<div class="container">
<h1>Liste des Evaluations pour ${etudiant.username}</h1>

<table class="tbl">
    <thead>
        <th>Module</th>
        <th>Note</th>
        <th>Remarque</th>
    </thead>
    <tbody>
    <c:forEach items="model.evaluations" var="ev">
        <tr>
            <td>${ev.examen.module.code}</td> --> Error occurs here
            <td>${ev.note}</td>
            <td>${ev.remarque}</td>             
        </tr>.
    </c:forEach>
    </tbody>
</table>    

The Controller :

@RequestMapping(value="/{username}", method=RequestMethod.GET)
public String etudiantEvaluations(
            @PathVariable String username, Model model) {

    List<Evaluation> evaluations = evalDAO.findAllByEtudiant(username);
    Etudiant etudiant = etDAO.findByUsername(username);

    model.addAttribute("evaluations", evaluations);
    model.addAttribute("etudiant", etudiant);

    return "etudiants/listEvaluations";
}

The Evaluation entity :

@Data 
@NoArgsConstructor 
@EqualsAndHashCode
@Immutable @Entity(name="TEVALUATION")
public class Evaluation {

private enum evaldeliberation {
    REUSSITE,
    AJOURNEMENT,
    REFUS,
    ABANDON
}

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
protected Long id;

@NotNull
@Min(0) @Max(100)
@Column(updatable = true)
private Double note;

@Column(name="deliberation")
private evaldeliberation delib;

@Column(name="remarque")
private String remarque;

@Column(name="module_code") 
private String moduleCode;
private Long examenId;
private String etudiantUsername;

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "FKExamen", 
            insertable = false, 
            updatable = false)
protected Examen examen;

@ManyToOne
@JoinColumn(name = "FKEtudiant", 
            insertable = false, 
            updatable = false)
protected Etudiant etudiant;

@OneToMany(mappedBy="evaluation")
protected List<EvalComp> evalComps = new ArrayList<>();


public Evaluation(Double note, Examen examen, Etudiant etudiant) {
    super();
    this.examen = examen;
    this.etudiant = etudiant;
    examen.getEvaluations().add(this);
    etudiant.getEvaluations().add(this);

}

}

Examen:

@Data
@EqualsAndHashCode
@NoArgsConstructor
@Entity(name="TEXAMEN")
public class Examen {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@OneToMany(mappedBy = "examen")
private Set<Evaluation> evaluations = new HashSet<>();

@NotNull
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="FKmodule")
protected Module module;


public Examen(Module module) {
    this.module = module;
}

}

The Query:

@Query("SELECT ev FROM TEVALUATION ev JOIN FETCH ev.examen ex JOIN FETCH ex.module m WHERE ev.etudiant=?1")
List<Evaluation> findAllByEtudiantId(String username);

The getters and setters are generated by Lombok(also tried without it).

Any idea how can I load the attributes ?

Thanks in advance.

0 个答案:

没有答案