首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java/Spring引导-在POST请求不起作用后重定向

Java/Spring引导-在POST请求不起作用后重定向
EN

Stack Overflow用户
提问于 2020-11-20 16:55:43
回答 1查看 460关注 0票数 2

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

代码语言:javascript
复制
@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中,重定向失败了,我得到了一条消息:

有一个意外的错误(type=Not Found,status=404)。没有可用的消息

终端没有错误。所以,我不明白为什么会这样。我对Spring和Java完全陌生,所以我不知道我在这里做了什么?

更新

我尝试为结果模板创建一个控制器,如答案中所建议的那样。

代码语言:javascript
复制
@Controller
@RequestMapping("/result")
public class ResultController {
    @GetMapping()
    public String getResultPage() {
        return "result";
    }
}

那么,如果我从这样的NoteController重定向:

代码语言:javascript
复制
@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";
}

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

代码语言:javascript
复制
<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>

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

EN

回答 1

Stack Overflow用户

发布于 2020-11-20 17:17:50

您必须在控制器中创建一个/result端点。spring不可能自动生成端点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64933616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档