【问题标题】:Spring-Boot: Redirect and Refresh model and pageSpring-Boot:重定向和刷新模型和页面
【发布时间】:2018-03-23 11:50:12
【问题描述】:

我有一个 spring-boot 应用程序,带有它们。我反复更新页面,并将其重定向到同一页面,所以我希望页面的元素得到更新:

@GetMapping("/suggested-events/vote/{eventId}")         
public String voteForEvents(Model model,
                            @PathVariable("eventId") Long eventId,
                            @RequestParam(value = "message", required = false) String message ) {
    log.info("The message is: "+message);
    SuggestedEvent event = suggestedEventService.findSuggestedEventById(eventId);
    ArrayList<SuggestedEvent> events = suggestedEventService.findSuggestedEventsByArea(event.getArea());
    model.addAttribute("mainEvent",event);
    model.addAttribute("events",events);
    model.addAttribute("message",message);

    return "/suggested-event/vote";
}

当在视图中按下按钮时,它会触发以下 post 方法:

@PostMapping("/suggested-events/vote")
public String voteForASuggestedEvent(RedirectAttributes redirectAttributes){
    log.info("You have made a vote");
    redirectAttributes.addAttribute("message", "Success");

    return "redirect:/suggested-events/vote/1";
}

第二个控制器方法执行一个操作并创建一个message,并将其重定向到第一个方法。因此,它成功重定向到第一个方法并记录

log.info("The message is: "+message);

但它不会刷新我的页面,并且我没有收到作为模型的消息

当我重定向到第一种方法时,我希望它会将message 添加到我的模型中:

model.addAttribute("message",message);

但它没有添加到我的页面

【问题讨论】:

标签: spring spring-mvc redirect thymeleaf


【解决方案1】:

当在视图中按下按钮时,它会触发以下帖子 方法:

听起来这个触发器使用的是 AJAX,而不是表单提交。这样做会符合您描述的症状。

如果你 POST/suggested-events/vote 使用 AJAX,服务器会返回一个 302,浏览器会跟着它。但是,该 302 的响应仍然是 AJAX 调用的结果。您可以在成功回调中访问它,但浏览器不会为您呈现它。

但它不会刷新我的页面

如果 302 不会导致您的页面重新呈现,这也表明您正在使用 AJAX。

如果您实际上使用表单提交,浏览器使用成功重定向返回的标记重新呈现。

这可以通过使用vote.html 中的以下两个按钮来验证:

  <form action="http://localhost:8080/suggested-events/vote" method="POST">
    <input type="submit" text="Submit" />
  </form>

  <button onclick="postmessage();" >Button</button>

  <script>
    function postmessage() {
        $.ajax({
            method: 'POST',
            data: {},
            url: 'http://localhost:8080/suggested-events/vote'
        });
    }
  </script>

第一个按钮将按预期工作,第二个按钮与您描述的症状相匹配。

如果您正在已经在使用表单,请用它更新问题(或者更好的是,整个 Thymeleaf 模板)。

【讨论】:

    【解决方案2】:

    在Spring中重定向页面的方法有很多,但是请确保模型属性关闭消息是否正确传递给前端或将类似参数传递给另一个处理程序,您可以查看此文档:http://javainsimpleway.com/spring-mvc-redirecting-model-attributes-from-one-controller-to-other-controller/,希望对您有用! !

    【讨论】:

      【解决方案3】:

      我遇到了与 OP 描述的相同的问题,Mike 的解释使我找到了正确的方向。

      我正在读取一个数据库表并使用th:each 用百里香填充它。我想在删除项目之前添加一个 javascript 确认。发送没有事件监听器的 ajax GET 并使用 location.reload(true) 重新加载没有到达控制器中的 @GetMapping("/delete/{id}")

      这个SO-thread给了我ajax调用的答案。

      <a class="btn btn-danger" href="#" th:onclick="|confirmDeletion('${u.id}')|"></a>
      
      <script th:inline="javascript">
          function confirmDeletion(id) {
              if (confirm("Delete this id? " + id)) {
                  var http = new XMLHttpRequest();
                  http.open("GET", "/delete/" + id, true);
                  http.addEventListener("readystatechange", function() {
                      if (http.readyState === 4 && http.status === 200) {
                          window.location.reload(true);
                      }
                  });
                  http.send();
              }
          }
      </script>
      

      【讨论】:

        猜你喜欢
        • 2014-11-13
        • 2017-07-25
        • 2019-01-05
        • 1970-01-01
        • 2020-09-10
        • 2016-07-12
        • 2020-12-31
        • 2016-09-12
        • 2018-02-07
        相关资源
        最近更新 更多