简短:我想使用 Thymeleaf 模板index.html,但 url 指向thanks.html。
深入:我正在尝试提交表单将我的用户带到页面http://localhost:8080/thanks.html。由于一些不同的原因,我不希望表单的操作成为Thanks.html,但我已经大大简化了下面的逻辑。当表单的所有验证都通过后,我想传入一个变量来指示要使用的布局。我通过使用名为 contentPage 的模型变量来实现这一点。问题是,如果我有“return“thanks.html”;”在 indexSubmit 方法中,我从 thymeleaf 收到一条错误,提示找不到模板。如果我将其更改为“返回“index.html”;一切正常,但网址是 http://localhost:8080/ 而不是 http://localhost:8080/thanks.html。
@PostMapping("/")
public String indexSubmit(Model model) {
model.asMap().clear();
model.addAttribute("contentPage","layout/thanks.html");
return "thanks.html";
}
@GetMapping("/thanks.html")
public String thanks(Model model) {
model.addAttribute("contentPage","layout/thanks.html");
return "index.html";
}
我自己找到了答案:
return "redirect:thanks.html";