Spring Boot addflashattribute未在HTML中显示消息
我一直在这里和在线搜索如何以最佳方式使用AddFlashattribute,无论我做什么,它都不适合我。我有一个学生模型,我正在尝试更新和成功的消息。数据库和所有查询都可以工作,我被重定向到ShowStudentDetail页面,这只是没有显示的消息。这是代码:
StudenterVice:
public String updateStudent(Student student) {
if(studentDAO.update(student))
return "update student was successful!";
return "error: failed to update student, please try again";
}
public Student getStudentInfo(String username) {
Student student = studentDAO.get(username);
return student;
}
studentController:
@PostMapping("/updateStudent")
public String updateStudent(@ModelAttribute("student") Student student, RedirectAttributes redirectAttrs) {
String res = studentService.updateStudent(student);
redirectAttrs.addAttribute("username", student.getUsername()).addFlashAttribute("message", res);
return "redirect:/students/{username}/details";
}
@GetMapping("/students/{username}/details")
public String showDetailPage(@PathVariable String username, Model model) {
Student student = studentService.getStudentInfo(username);
model.addAttribute("student", student);
return "student_detail";
}
student_detail(div不显示)
<div>
<div th:if="${param.username}" class="alert alert-info" role="alert" th:text="${param.message}"></div>
</div>
I've been searching here and online how to use addFlashAttribute in the best way and it's not working for me no matter what I do. I have a Student model which I'm trying to update and flash message of success or fail. The database and all the queries do work and I get redirected to the showStudentDetail page, it's just the message that doesn't show. Here is the code:
StudentService:
public String updateStudent(Student student) {
if(studentDAO.update(student))
return "update student was successful!";
return "error: failed to update student, please try again";
}
public Student getStudentInfo(String username) {
Student student = studentDAO.get(username);
return student;
}
StudentController:
@PostMapping("/updateStudent")
public String updateStudent(@ModelAttribute("student") Student student, RedirectAttributes redirectAttrs) {
String res = studentService.updateStudent(student);
redirectAttrs.addAttribute("username", student.getUsername()).addFlashAttribute("message", res);
return "redirect:/students/{username}/details";
}
@GetMapping("/students/{username}/details")
public String showDetailPage(@PathVariable String username, Model model) {
Student student = studentService.getStudentInfo(username);
model.addAttribute("student", student);
return "student_detail";
}
student_detail (div not showing)
<div>
<div th:if="${param.username}" class="alert alert-info" role="alert" th:text="${param.message}"></div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
控制器中模型的名称是学生,但是在视图中,您正在使用“ param”名称。将“参数”更改为“学生”。
The name of the model in the controller is student, but in the view you are using "param" name. Change "param" to "student".
我终于通过更改为消息而不是param.message解决了这个问题。
我真的不知道为什么现在起作用,但这是解决我的问题的方法。
仅将HTML更改为:
I finally solved this problem by changing to message instead of param.message.
I really don't know why now it's working but that's the solution to my problem.
only change the html to this: