Client.java
package com.example.demo.entities;
import java.io.Serializable;
import java.util.Collection;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Table(name = "clients")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Client implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private java.lang.String nom;
@Column(name = "adress")
private java.lang.String adresse;
private String mail;
private String telephone;
public Client(String nom, String adresse) {
super();
this.nom = nom;
this.adresse = adresse;
}
public Client(String nom, String adresse, String mail, String telephone) {
super();
this.nom = nom;
this.adresse = adresse;
this.mail = mail;
this.telephone = telephone;
}
public Client() {};
}
ClientController.java
package com.example.demo.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import com.example.demo.entities.Client;
import com.example.demo.repository.ClientRepository;
@Controller
public class ClientController{
@Autowired
public ClientRepository cr;
@GetMapping("/")
public String viewHomePage(Model model) {
List <Client> list= cr.findAll();
model.addAttribute("list",list);
return "home";
}
}
ClientRepository.java
package com.example.demo.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.entities.Client;
@Repository
public interface ClientRepository extends JpaRepository<Client,Long>{
}
main.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.example.demo.entities.Client;
import com.example.demo.repository.ClientRepository;
@SpringBootApplication
public class Shop1Application {
public static void main(String[] args) {
ApplicationContext crt=SpringApplication.run(Shop1Application.class, args);
ClientRepository cr=crt.getBean(ClientRepository.class);
Client c1=new Client("Bilel","Hammamet","myemailis","123456");
Client c2=new Client("Wissal","Tunis","herrmail","1151");
Client c3=new Client("Mohamed","nabeul","hisemail","444");
Client c4=new Client("anas","Hammamet","his","55222");
Client c5=new Client("mourad","tunis","fffff","44");
cr.save(c1);
cr.save(c2);
cr.save(c3);
cr.save(c4);
cr.save(c5);
}
}
home.html
<!DOCTYPE html>
<html xmlns:th="http//www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 > Liste des clients de la base </h1>
<div>
<table>
<tr>
<th>ID</th><th>Nom</th><th>Adresse</th><th>Mail</th><th>telephone</th>
</tr>
<tr th:each="c:${list}">
<td th:text="${c.id}"></td>
<td th:text="${c.nom}"></td>
<td th:text="${c.adresse}"></td>
<td th:text="${c.mail}"></td>
<td th:text="${c.telephone}"></td>
</tr>
</table>
</div>
</body>
</html>
This is an ss of error in localhost:8080
I tried to change in the home.html file but nothing happened I think the problem is in the home.html (Thymeleaf section). I think I have a problem calling the id, adresse, telephone from the repository which found in the h2 database to make them appear in a table in the thymeleaf page.