46- Implement Delete Student Feature


Final Image


1-  Create a delete button next to update button in    students.html      

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>

<a th:href="@{/students/new}" class="btn btn-primary">Add Student</a>
<br><br><br>

<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>

        <th scope="col">Actions</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>
      <td><a th:href = "@{/students/edit/{id}(id=${student.id})}"
             class = "btn btn-success">Update</a>
             

<a th:href = "@{/students/delete/{id}(id=${student.id})}"
             class = "btn btn-danger">Delete</a></td>
   
</tr>
   


  </tbody>
</table>

</body>
</html>



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();
   
    Student saveStudent(Student student);
   

    Student getStudentById(Long id);
   
    Student updateStudent(Student student);

    void deleteStudentById(long id);
}


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();
    }

    @Override
    public Student saveStudent(Student student) {
    return  studentRepository.save(student);
       
    }

    @Override
    public Student getStudentById(Long id) {
        return studentRepository.findById(id).get();
    }

    @Override
    public Student updateStudent(Student student) {
        return studentRepository.save(student);
    }

    @Override
    public void deleteStudentById(long id) {
        studentRepository.deleteById(id);
   
    }

}


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";
    }
   
    @GetMapping("/students/new")
    public String createStudent(Model model) {
        Student student= new Student();
        model.addAttribute("student", student);
        return "create_student";
       
    }
    @PostMapping("/students")
    public String saveStudent(@ModelAttribute("student")Student student) {
       
    studentService.saveStudent(student);
    return "redirect:/students";
    }
   
    @GetMapping("/students/edit/{id}")
    public String editStudentForm(@PathVariable long id,Model model) {
        model.addAttribute("student",studentService.getStudentById(id));
        return "edit_student";
    }
   
    @PostMapping("/students/{id}")
    public String updateStudent(@PathVariable long id,@ModelAttribute ("student") Student student,Model model) {
       

        Student existingStudent=studentService.getStudentById(id);

        //Changing values of previous employee
        existingStudent.setId(id);
        existingStudent.setFirstName(student.getFirstName());
        existingStudent.setLastName(student.getLastName());
        existingStudent.setEmail(student.getEmail());
       
        //Saving changes made
        studentService.updateStudent(existingStudent);
        return "redirect:/students";
       
    }
    @GetMapping("/students/delete/{id}")
    public String deleteStudent(@PathVariable long id) {
        studentService.deleteStudentById(id);
        return "redirect:/students";
    }
   
}




Comments

Popular posts from this blog

INDEX OF Zeek Spring Boot