1 pièce(s) jointe(s)
Cannot invoke "fr.dawan.test3.service.ClientService.recupererClients()" because "this.clientService" is null
Bonjour,
J'ai l'erreur qui est dans le titre mais après de nombreuses recherche je n'arrive pas à régler ce problème voici mon code :
Arborescence du projet :
Pièce jointe 631755
Dans le package business :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package fr.dawan.test3.business;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the client database table.
*
*/
@Entity
@Table(name="client")
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id_Client;
private String nom;
public Client() {
super();
}
public Client(String nom) {
super();
this.nom = nom;
}
public int getId_Client() {
return this.id_Client;
}
public void setId_Client(int id_Client) {
this.id_Client = id_Client;
}
public String getNom() {
return this.nom;
}
public void setNom(String nom) {
this.nom = nom;
}
@Override
public String toString() {
return "Client [id_Client=" + id_Client + ", nom=" + nom + "]";
}
} |
Dans le controller :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package fr.dawan.test3.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import fr.dawan.test3.service.ClientService;
@Controller
public class ClientController {
private ClientService clientService;
public ClientController() {
super();
}
public ClientController(ClientService clientService) {
super();
this.clientService = clientService;
}
@GetMapping({ "/index", "/" })
public ModelAndView accueil(){
// On déclare un ojb de type ModeleAndView et on l'instancie.
ModelAndView mav = new ModelAndView();
// On renseigne la vue associé au ModelAndView.
// La vue est index.jsp, grâce au fichier application.properties à la ligne 13 il on est pas obligé
// de préciser l'extension .jsp.
mav.setViewName("index");
// On ajoute à l'objet mav l'objet récupéré dans la bdd.
System.out.println("clientService : "+clientService+"." );
mav.addObject("clients", clientService.recupererClients());
// On renvoi l'objet ModelAndView.
return mav;
}
} |
Dans le dao :
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| package fr.dawan.test3.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import fr.dawan.test3.business.Client;
@NoRepositoryBean
public interface ClientDao extends JpaRepository<Client, Integer> {
} |
Dans le service :
Code:
1 2 3 4 5 6 7 8 9
| package fr.dawan.test3.service;
import java.util.List;
import fr.dawan.test3.business.Client;
public interface ClientService {
List<Client> recupererClients();
} |
Et dans le service implémente :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package fr.dawan.test3.service.impl;
import java.util.List;
import org.springframework.stereotype.Service;
import fr.dawan.test3.business.Client;
import fr.dawan.test3.dao.ClientDao;
import fr.dawan.test3.service.ClientService;
@Service
public class ClientServiceImpl implements ClientService {
private ClientDao clientDao;
public ClientServiceImpl() {
super();
}
public ClientServiceImpl(ClientDao clientDao) {
super();
this.clientDao = clientDao;
}
@Override
public List<Client> recupererClients(){
return clientDao.findAll();
}
} |
Dans index.jsp :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div>
<c:forEach items="${ clients }" var="client">
<p>${ client.id_Client }</p>
<p>${ client.nom }</p>
</c:forEach>
</div>
</body>
</html> |
Si vous avez une idée merci d'avance.