Spring mvc 在thymeleaf表单中验证失败,但请求仍然通过
我对thymeleaf(实际上是SpringMVC)还相当陌生,但我已经阅读了文档,似乎无法理解这一点 我有一个相当标准的表格:Spring mvc 在thymeleaf表单中验证失败,但请求仍然通过,spring-mvc,thymeleaf,Spring Mvc,Thymeleaf,我对thymeleaf(实际上是SpringMVC)还相当陌生,但我已经阅读了文档,似乎无法理解这一点 我有一个相当标准的表格: <form id="form" th:action="@{/next/page}" action="#" th:object="${form}" method="post"> 我的控制器有一个具有以下签名的方法: @RequestMapping(value = "/next/page", method = RequestMethod.POST) p
<form id="form" th:action="@{/next/page}" action="#" th:object="${form}" method="post">
我的控制器有一个具有以下签名的方法:
@RequestMapping(value = "/next/page", method = RequestMethod.POST)
public String example(@Valid Form form, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response)
{
if(bindingRequest.hasErrors()) {
return page-my-form-is-on;
}
... other stuff here ...
}
提交表单时出现验证错误,页面似乎仍在导航到th:action中指定的/next/page,但加载表单时,页面仍与表单所在的页面相同,并显示验证错误消息
当出现验证错误时,如何取消请求并阻止其导航到/next/page?当出现验证错误时,我只希望它保持在同一页上并显示错误 表单中的数据是否传递给控制器?@Lukehey是的,当你说它“导航到/next/page”时,你的意思是显示的浏览器路径是/next/page吗?如果是这样的话,那么这与您的表单提交到/next/page时所预期的一样。您可以在出现错误时重定向到其他路径,尽管这会很奇怪。您描述的行为是,当发布表单而不使用ajaxy时,我希望表单验证错误能够正常工作。
@RequestMapping(value = "/next/page", method = RequestMethod.POST)
public String example(@Valid Form form, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response)
{
if(bindingRequest.hasErrors()) {
return page-my-form-is-on;
}
... other stuff here ...
}