UF01-1844
Subject
Create a Spring MVC Thyemeleaf with all CRUD operations for a single object. No persistante needed.
Model
Student class and attributes. Lombok annotation has been used to create setters, getters and constructors.
Expand to reveal code
Student.java
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private String studentID;
private String firstName;
private String lastName;
private String email;
}
Utils class
Created a utils class that will help to create unique ID’s for each object and populating our non-persistent DB.
Expand to reveal code
Utils.java
public class Utils {
public static String createUUID() {
UUID uuid = UUID.randomUUID();
String id = uuid.toString();
return id;
}
public static HashMap<String, Student> populateStudents(int qty, HashMap<String, Student> students ) {
= new Faker();
Faker faker ;
Student newStudent
for (int i = 0; i < qty; i++) {
= new Student();
newStudent
String studentID = Utils.createUUID();
.setStudentID(studentID);
newStudent
String studentFirstName = faker.name().firstName();
.setFirstName(studentFirstName);
newStudent
String studentLastName = faker.name().lastName();
.setLastName(studentLastName);
newStudent
.setEmail("fake@email.com");
newStudent
.put(studentID, newStudent);
students
}
return students;
}
Service
Service class with all the busines logic needed for CRUD operations
Expand to reveal code
Service.java
@Service
public class StudentService {
public static HashMap<String, Student> students = new HashMap<>();
static {
.populateStudents(5, students);
Utils}
public static void populateDB(int qty) {
.populateStudents(qty,students);
Utils}
public void createStudent(Student student) {
String studentID = Utils.createUUID();
.setStudentID(studentID);
student.put(studentID, student);
students}
public HashMap<String, Student> getAllStudents (){
return students;
}
public Student createStudent(){
= new Student();
Student student .setEmail("");
studentreturn student;
}
public Student findStudentByID(String studentID) {
return students.getOrDefault(studentID, null);
}
public void updateStudentByStudentID(Student studentFound) {
.put(studentFound.getStudentID(), studentFound);
students}
public void deleteStudentByStudentID(String studentID) {
.remove(studentID);
students}
}
Controller
Here is the controller and all the end-points needed to perform CRUD operations
Expand to reveal code
Controller.java
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
;
StudentService studentService
@RequestMapping("/students")
public String getAllUsers(Model model){
// fetch all users, add to model
.addAttribute("students", studentService.getAllStudents());
modelreturn "student/students";
}
@RequestMapping("/createStudent")
public String createUser(Student student){
.createStudent(student);
studentServicereturn "redirect:students";
}
@RequestMapping("/studentForm")
public String sendForm(){
return "student/studentForm";
}
@RequestMapping("/packedStudent")
public String packedStudents(@RequestParam("studentID") String studentID, Model model){
= studentService.findStudentByID(studentID);
Student studentFound
if (studentFound != null){
.addAttribute("studentFromController", studentFound);
model.addAttribute("message", "Student found");}
modelelse
.addAttribute("message", "Student not found");
model
return "student/studentToUpdate";
}
@PostMapping("/updateStudent/{studentID}")
public String updateUser(@PathVariable("studentID") String studentID,
) {
Student updatedStudent
= studentService.findStudentByID(studentID);
Student studentFound
if (studentFound != null) {
.updateStudentByStudentID(updatedStudent);
studentServicereturn "redirect:/student/students";
} else return "student/studentNotFound";
}
@RequestMapping("/deleteStudent")
public String deleteStudent(@RequestParam("studentID") String studentID) {
= studentService.findStudentByID(studentID);
Student studentFound
if (studentFound != null) {
.deleteStudentByStudentID(studentID);
studentServicereturn "redirect:/student/students";
} else return "student/studentNotFound";
}
@RequestMapping("/studentDetails")
public String studentDetails(@RequestParam("studentID") String studentID, Model model) {
= studentService.findStudentByID(studentID);
Student studentFound
if (studentFound != null) {
.addAttribute("student",studentFound );
modelreturn "/student/studentDetails";
} else return "student/studentNotFound";
}
@RequestMapping("/createFakeStudents")
public String createFakeStudents (@RequestParam("qty") int qty) {
.populateDB(qty);
StudentServicereturn "redirect:/student/students";
}
}
Tree folder project structure
Here’s the structure of the project. Note that there’s a Index Controller to manage the home page.
Dependencies
Expand to reveal code
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.javafaker/javafaker -->
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Source code Git Hub Repository