我有一个非常简单的问题,关于使用 thymeleaf
将参数从 Controller
发送到 View
。
我有 HTML 页面,其中包含以下行:
<p th:text="${xyz}"></p>
当我访问页面http://localhost:8080/myproject-web/mypage
时,此段落
为空。
当我单击提交按钮时,它会转到 Controller :
@RequestMapping(value = "/subscribeth", params = { "save" })
public String save(final MyMainObject subscription, final BindingResult bindingResult, final ModelMap model) {
System.out.println("I am here"); // it is displayed
model.addAttribute("xyz", "Hello");
return "redirect:/mypage";
}
因此,它再次转到mypage
,但paragraph
仍然是空的。
关于我忘记的事情......?预先感谢您
最佳答案
您实际上正在重定向页面。即,这将要求客户端将请求重新发送到给定页面。但是,您需要的只是 mypage
的前向/普通 View 渲染。所以,你必须改变这个
return "redirect:/mypage";
到
return "forward:/mypage";
注意:您还可以使用 RedirectAttributes
在重定向请求中设置值
关于java - 如何从 Controller 发送参数以使用thymeleaf查看和显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22856020/