【问题标题】:Java/Spring boot - Redirect after POST request not workingJava/Spring 启动 - POST 请求不起作用后重定向
【发布时间】:2020-11-20 16:55:43
【问题描述】:

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

@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>

我应该如何将属性传递给这个模板?

【问题讨论】:

  • 你得到了结果映射?
  • 我没有,我以为 spring 会在后台自动映射到模板
  • return new ModelAndView("redirect:/redirectedUrl", model);return new RedirectView("redirectedUrl");
  • @cela 我得到一个编译器错误,如果我按照您的建议进行操作,则无法解析 ModelAndView 的构造函数
  • @Leff docs.spring.io/spring-framework/docs/current/javadoc-api/org/… 模型是一张地图。在您的model 参数上尝试将其更改为ModelMap model。然后为重定向添加你的属性

标签: java spring-boot


【解决方案1】:

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

【讨论】:

  • 如何将属性传递给该模板?我已经为该路由定义了一个控制器,但是没有传递属性,因此视图没有按照我的意愿构建。我已经用新代码更新了问题。
猜你喜欢
  • 2015-09-30
  • 2017-08-10
  • 1970-01-01
  • 2018-08-06
  • 2019-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
相关资源
最近更新 更多