首页 > 解决方案 > Spring WebFlux + thymeleaf: Post request redirect 获取页面返回 303 查看其他状态

问题描述

我只是使用 SpringBoot + WebFlux + thymeleaf 来编写控制器。

@RequestMapping(value = "/create", method = RequestMethod.GET)
public String createCityForm(Model model) {
    model.addAttribute("city", new City());
    model.addAttribute("action", "create");
    return CITY_FORM_PATH_NAME;
}

@RequestMapping(value = "/create", method = RequestMethod.POST)
public String postCity(@ModelAttribute City city) {
    cityService.saveCity(city);
    return REDIRECT_TO_CITY_URL;
}

我使用 thymeleaf 页面接收表单,并重定向/返回 get 方法页面,但浏览器给出 303 查看其他状态。此外,删除资源也不起作用。

标签: springspring-bootthymeleafspring-webflux

解决方案


SEE_OTHER状态实际上是在没有明确指定 HTTP 代码的情况下调用时的默认状态(RedirectView就像ThymeleafReactiveViewResolver那样)。

如果你想覆盖这个状态,RedirectView当它与redirect:视图名称中的模式匹配时,直接返回而不是让 Thymeleaf 这样做:

@RequestMapping(value = "/create", method = RequestMethod.GET)
public RedirectView createCityForm(Model model) {
    model.addAttribute("city", new City());
    model.addAttribute("action", "create");
    return new RedirectView("/target_url", HttpStatus.MOVED_PERMANENTLY);
}

推荐阅读