Description:
In this continuation, we will explore using query parameters to display messages in the view after handling form submissions. This involves adapting the thirdView to show both success and error messages based on query parameters.
Steps:
-
Adapt the View (thirdView.html):
-
Open the
third.htmlfile and add sections to display both success and error messages:<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <!-- Other head elements... --> </head> <body> <!-- Other body elements... --> <div th:if="${param.addedStudent}"> <p style="color: green;">Student Added Successfully!</p> </div> <div th:if="${param.error}"> <p style="color: red;">Error: <strong th:text="${param.error}"></strong></p> </div> <!-- Other content... --> </body> </html>
-
Explanation:
- The
<div th:if="${param.error}">checks if the query parametererroris present in the URL. - If the parameter is present, it displays an error message in red, retrieved from
${param.error}.
- The
-
-
Handle Form Submission with Additional Validation (PracticeController):
-
Update the
addStudentmethod inPracticeControllerto include additional validation for the student's mark using anif-elsestructure:import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; @Controller public class PracticeController { private final List<StudentModel> students = new ArrayList<>(); // Other methods... @PostMapping("/addStudent") public String addStudent(@ModelAttribute("model") StudentModel model) { // Additional validation: Check if the mark is more than 5. if (model.getMark() > 5) { // If the mark is more than 5, redirect with an error message. return "redirect:/third?error=Mark should not be more than 5"; } else { students.add(model); return "redirect:/third?addedStudent=true"; } } }
-
Your view should look like that:

