post请求不起作用后重定向

ldioqlga  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(345)

我有一个简单的控制器,用于post请求,之后我想重定向到另一个页面。这是控制器:

@Controller
public class NoteController {
    private NoteService noteService;
    private UserService userService;

    public NoteController(NoteService noteService, UserService userService) {
        this.noteService = noteService;
        this.userService = userService;
    }

    @PostMapping("/notes")
    public String postNote(@ModelAttribute Note noteForm, Authentication authentication, Model model) {
        User user = this.userService.getUser(authentication.getName());
        Integer userid = user.getUserId();

        try {
            noteService.createNote(noteForm, userid);
            model.addAttribute("success",true);
            model.addAttribute("message","New note added!");
        } catch (Exception e) {
            model.addAttribute("error",true);
            model.addAttribute("message","System error!" + e.getMessage());
        }

        return "redirect:/result";
    }
}

在一个成功的请求之后,我可以看到一个项目被保存在db中,重定向失败,我得到一条消息:
出现意外错误(类型=未找到,状态=404)。无可用消息
终端没有错误。所以,我不明白为什么会这样。我对spring和java完全陌生,所以我不知道我在这里做错了什么?
更新
我尝试过为结果模板创建一个控制器,如答案中所建议的那样。

@Controller
@RequestMapping("/result")
public class ResultController {
    @GetMapping()
    public String getResultPage() {
        return "result";
    }
}

如果我从notecontroller执行重定向,如下所示:

@PostMapping("/notes")
public String postNote(@ModelAttribute Note noteForm, Authentication authentication, Model model) {
    User user = this.userService.getUser(authentication.getName());
    Integer userid = user.getUserId();

    try {
        noteService.createNote(noteForm, userid);
        model.addAttribute("success",true);
        model.addAttribute("message","New note added!");
    } catch (Exception e) {
        model.addAttribute("error",true);
        model.addAttribute("message","System error!" + e.getMessage());
    }

    return "redirect:result";
}

我被重定向到结果模板,但没有传递属性:

<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <link rel="stylesheet" type="text/css" media="all" th:href="@{/css/bootstrap.min.css}">

        <title>Result</title>
    </head>
    <body class="p-3 mb-2 bg-light text-black">
        <div class="container justify-content-center w-50 p-3" style="margin-top: 5em;">
            <div class="alert alert-success fill-parent" th:if="${success}">
                <h1 class="display-5">Success</h1>
                <span th:text="${message}">Success Message</span>
                <span>Your changes were successfully saved. Click <a th:href="@{/home}">here</a> to continue.</span>
            </div>
            <div class="alert alert-danger fill-parent" th:if="${error}">
                <h1 class="display-5">Error</h1>
                <span>Your changes were not saved</span>
                <span th:text="${message}">Error Message</span>
                <span>Click <a th:href="@{/home}">here</a> to continue.</span>
            </div>
        </div>
    </body>
</html>

如何将属性传递给此模板?

jk9hmnmh

jk9hmnmh1#

你必须创建一个 /result 控制器中的终结点。spring不可能自动生成端点。

相关问题