Question: Using Intellij, fix the provided code so that I am able to Register a new student in the Register tab to the Student page list:
Using Intellij, fix the provided code so that I am able to Register a new student in the Register tab to the Student page list: package cslabcontroller;
import cslabmodel.Student;
import cslabmodel.StudentRepository;
import org.springframework.stereotype.Controller;
import org.springframework.uiModel;
import org.springframework.web.bind.annotation.;
import java.util.List;
@Controller
@RequestMappingstudent
public class StudentController
private final StudentRepository studentRepository;
public StudentControllerStudentRepository studentRepository
this.studentRepository studentRepository; Dependency injection
Display the student list
@GetMapping
public String displayStudentModel model
List students studentRepository.getStudents; Get the list of students
model.addAttributestudents students; Add the list of students to the model
return "student"; Return the view name for student list
Display the form to register a new student
@GetMappingnew
public String showNewStudentFormModel model
model.addAttributetitle "Register New Student";
return "register"; Render the register form page
Process the form submission to add a new student
@PostMappingadd
public String addStudent
@RequestParam String name,
@RequestParam int birthYear,
@RequestParam String session,
@RequestParam String level,
@RequestParam String preferredTime
@RequestParam String preferredTime
Create a new student object with the form data
Student newStudent new Studentname session, birthYear, level, preferredTime preferredTime;
Add the new student to the repository
studentRepository.addStudentnewStudent;
Redirect to the student listing page
return "redirect:students; Redirects to the student list page
package cslabmodel;
import java.time.Year;
public class Student
private static int nextId ;
private final int id;
private final String name;
private final String session;
private final int birthYear;
private final String level;
private final String preferredTime;
private final String preferredTime;
public StudentString name, String session, int birthYear, String level, String preferredTime String preferredTime
this.id nextId;
this.name name;
this.session session;
this.birthYear birthYear;
this.level level;
this.preferredTime preferredTime;
this.preferredTime preferredTime;
public int getId
return id;
public String getName
return name;
public String getSession
return session;
public int getBirthYear
return birthYear;
public String getLevel
return level;
public String getPreferredTime
return preferredTime;
public String getPreferredTime
return preferredTime;
public int getAge
return Year.nowgetValue birthYear;
package cslabmodel;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Component
public class StudentRepository
private final List students;
public StudentRepository
students new ArrayList;
initializeStudents;
Initialize the repository with some default students
private void initializeStudents
students.addAllArraysasList
new StudentAlice Smith", "Session "starfish", AM AM
new StudentBob Johnson", "Session "seahorse", AM PM
new StudentCharlie Brown", "Session "Dolphin", PM PM
new StudentSebastian Magana", "Session "seahorse", PM PM
;
Add a new student to the repository
public void addStudentStudent student
students.addstudent;
public List getStudents
return new ArrayListstudents; Return a copy to prevent modification
Register.jte
Registration
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
