44- Implement Add Student Feature

 

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




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



create_student.html

student-management-system --> src/main/resources --> templates -->    create_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"> Create New Student </h1>
                <div class="card-body">

                    <form th:action="@{/students}" 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>


NOTE:

Now at this point you have create a Add button and linked it with your create_student.html form 

but

 You Also need to save your student information from form and display it in your students.html template  



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

}




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




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









Comments

Popular posts from this blog

INDEX OF Zeek Spring Boot