Spring Boot addflashattribute未在HTML中显示消息

发布于 2025-01-25 10:26:01 字数 1363 浏览 2 评论 0原文

我一直在这里和在线搜索如何以最佳方式使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天荒地未老 2025-02-01 10:26:01

控制器中模型的名称是学生,但是在视图中,您正在使用“ param”名称。将“参数”更改为“学生”。

The name of the model in the controller is student, but in the view you are using "param" name. Change "param" to "student".

摘星┃星的人 2025-02-01 10:26:01

我终于通过更改为消息而不是param.message解决了这个问题。
我真的不知道为什么现在起作用,但这是解决我的问题的方法。

仅将HTML更改为:

<div th:if="${message}" class="alert alert-info" role="alert" th:text="${message}"></div>

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:

<div th:if="${message}" class="alert alert-info" role="alert" th:text="${message}"></div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文