45- Implement Update Student Feature

 

1- Adding an Update button in   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>
         
    </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);

}


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


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";
    }

   
   
}


NOTE

  From this point you have got the id and information of student after this we update student in   edit_student.html        

<!DOCTYPE html>
<html xmlns:th="//www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Student Form</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>





<div class = "container">
        <div class = "row">
            <div class ="col-lg-6 col-md-6 col-sm-6 container justify-content-center card">
           
           
                <h1 class = "text-center"> Update Student </h1>
               
                <div class = "card-body">
                   
    <form th:action="@{/students/{id}(id=${student.id})}" th:object = "${student}" method="POST">
                   
                   
                        <div class ="form-group">
                            <label> Student First Name </label>
                            <input
                            type = "text"
                            name = "firstName"
                            th:field = "*{firstName}"
                            class = "form-control"
                            placeholder="Enter Student First Name"
                            />
                        </div>
                       
                        <div class ="form-group">
                            <label> Student Last Name </label>
                            <input
                            type = "text"
                            name = "lastName"
                            th:field = "*{lastName}"
                            class = "form-control"
                            placeholder="Enter Student Last Name"
                            />
                        </div>
                       
                        <div class ="form-group">
                            <label> Student Email </label>
                            <input
                            type = "text"
                            name = "email"
                            th:field = "*{email}"
                            class = "form-control"
                            placeholder="Enter Student Email"
                            />
                        </div>
                       
                        <div class = "box-footer">
                            <button type="submit" class = "btn btn-primary">
                                Submit
                            </button>
                        </div>
                    </form>
               
                </div>
            </div>
        </div>
    </div>






</body>
</html>


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) {
       
//Changing values of previous employee

        Student existingStudent=studentService.getStudentById(id);
        existingStudent.setId(id);
        existingStudent.setFirstName(student.getFirstName());
        existingStudent.setLastName(student.getLastName());
        existingStudent.setEmail(student.getEmail());
       
//Saving changes made

        studentService.updateStudent(existingStudent);
        return "redirect:/students";
       
    }




Comments

Popular posts from this blog

INDEX OF Zeek Spring Boot