42- Implement List All Students Feature

Image 1 
Note - Model  and Repository layer completed


Service Class Updates

student-management-system -->  src/main/java --> net.javaguides.springboot.service --> StudentService.java

package net.javaguides.springboot.service;

import java.util.List;

import net.javaguides.springboot.entity.Student;

public interface StudentService {

    List<Student> getAllStudents();

}



ServiceImpl  Class Updates

student-management-system -->  src/main/java --> net.javaguides.springboot.service.impl --> StudentServiceImpl.java

package net.javaguides.springboot.sevice.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import net.javaguides.springboot.entity.Student;
import net.javaguides.springboot.repository.StudentRepository;
import net.javaguides.springboot.service.StudentService;


@Service
public class StudentServiceImpl implements StudentService {
   
    private StudentRepository studentRepository;
   
    public StudentServiceImpl(StudentRepository studentRepository) {
        super();
        this.studentRepository = studentRepository;
    }

    @Override
    public List<Student> getAllStudents() {
    return  studentRepository.findAll();
    }
}



Controller Class Updates

student-management-system -->  src/main/java --> net.javaguides.springboot.controller --> StudentController.java

package net.javaguides.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import net.javaguides.springboot.entity.Student;
import net.javaguides.springboot.service.StudentService;


@Controller
public class StudentController {
   
    private StudentService studentService;

    public StudentController(StudentService studentService) {
        super();
        this.studentService = studentService;
    }

   
    @GetMapping("/students")
    public String listStudents(Model model) {
        model.addAttribute("students", studentService.getAllStudents());
        return "students";
    }
}


NOTE

1- Spring Boot Auto Configures view resolver in thymeleaf due to spring-boot-starter-thymeleaf  dependency 

2- By default Spring Boot looks for Thymeleaf html file in template folder inside of src/main/resources.

So we need to put all our html files in template folder.



students.html

student-management-system --> src/main/resources --> templates -->     students.html        

<!DOCTYPE html>
<html xmlns:th="//www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Student Management System</title>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
</head>
<body>

<h1 class=".text-success">Student Management System</h1>


<table class="table">
  <thead>
    <tr>
   
      <th scope="col">Employee First Name </th>
      <th scope="col"> Employee Last Name</th>
      <th scope="col">Employee Email</th>

</tr>
  </thead>
  <tbody>
    <tr th:each="student:${students}">
       <td th:text="${student.firstName}" ></td>
      <td th:text="${student.lastName}"></td>
      <td th:text="${student.email}"></td>
</tr>
   


  </tbody>
</table>

</body>
</html>


Comments

Popular posts from this blog

INDEX OF Zeek Spring Boot