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 cs3220.lab8.controller;
import cs3220.lab8.model.Student;
import cs3220.lab8.model.StudentRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/student")
public class StudentController {
private final StudentRepository studentRepository;
public StudentController(StudentRepository studentRepository){
this.studentRepository = studentRepository; // Dependency injection
}
// Display the student list
@GetMapping
public String displayStudent(Model model){
List students = studentRepository.getStudents(); // Get the list of students
model.addAttribute("students", 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
@GetMapping("/new")
public String showNewStudentForm(Model model){
model.addAttribute("title", "Register New Student");
return "register"; // Render the register form page
}
// Process the form submission to add a new student
@PostMapping("/add")
public String addStudent(
@RequestParam String name,
@RequestParam int birthYear,
@RequestParam String session,
@RequestParam String level,
@RequestParam String preferredTime1,
@RequestParam String preferredTime2
){
// Create a new student object with the form data
Student newStudent = new Student(name, session, birthYear, level, preferredTime1, preferredTime2);
// Add the new student to the repository
studentRepository.addStudent(newStudent);
// Redirect to the student listing page
return "redirect:/students"; // Redirects to the student list page
}
}
package cs3220.lab8.model;
import java.time.Year;
public class Student {
private static int nextId =1;
private final int id;
private final String name;
private final String session;
private final int birthYear;
private final String level;
private final String preferredTime1;
private final String preferredTime2;
public Student(String name, String session, int birthYear, String level, String preferredTime1, String preferredTime2){
this.id = nextId++;
this.name = name;
this.session = session;
this.birthYear = birthYear;
this.level = level;
this.preferredTime1= preferredTime1;
this.preferredTime2= preferredTime2;
}
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 getPreferredTime1(){
return preferredTime1;
}
public String getPreferredTime2(){
return preferredTime2;
}
public int getAge(){
return Year.now().getValue()- birthYear;
}
}
package cs3220.lab8.model;
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.addAll(Arrays.asList(
new Student("Alice Smith", "Session 1",2000, "starfish", "10 AM","9 AM"),
new Student("Bob Johnson", "Session 2",1999, "seahorse", "10 AM","1 PM"),
new Student("Charlie Brown", "Session 3",2001, "Dolphin", "1 PM","2 PM"),
new Student("Sebastian Magana", "Session 4",2003, "seahorse", "12 PM","2 PM")
));
}
// Add a new student to the repository
public void addStudent(Student student){
students.add(student);
}
public List getStudents(){
return new ArrayList>(students); // Return a copy to prevent modification
}```
Register.jte
Registration
Using Intellij, fix the provided code so that I

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!