Spring 验证器注释和错误重定向
Spring validator annotation and error redirection
我实际上正在研究 Spring 框架以及 Spring Boot 和 Sprint MVC。
我正在做一个表单,其中 post 一些数据来完成一个对象,我需要验证一些值。
事实是验证有效,事实上,如果我不尊重验证,我会 "redirected"(实际上不是,URL 不会改变)error.html内容。
我应该如何正确管理我的重定向?这样不行:
@RequestMapping(value = "/print", method = RequestMethod.POST)
public String printPost(@ModelAttribute("printerentity") @Valid PrinterEntity print, Model model, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "redirect:/formPrinter";
}
model.addAttribute("printed", print.getName());
model.addAttribute("printerentity", new PrinterEntity());
return "index";
}
和表格:
<form method="post" th:action="@{/print}" th:object="${printerentity}">
<input type="text" th:field="*{name}"/>
<button type="submit">Valider</button>
<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</p>
</form>
我做错了什么?
编辑:似乎当我有错误时,我没有传入控制器代码:o =>
@RequestMapping(value = "/print", method = RequestMethod.POST)
public String printPost(@ModelAttribute("printerentity") @Valid PrinterEntity print, Model model, BindingResult bindingResult) {
System.out.println("I dont passe here when error but I m redirected");
if (bindingResult.hasErrors()) {
return "formPrinter";
}
model.addAttribute("printed", print.getName());
model.addAttribute("printerentity", new PrinterEntity());
return "index";
}
感谢提前
How should I do to manage my redirection correctly ?
当你重定向请求时,当前请求被销毁,并创建一个新的请求对象来处理请求,所以根据 Sotirios Delimanolis 在评论中提到的,模型属性是请求属性,可用于仅针对每个请求,如果您想在请求之间的 HTTP 会话中存储模型属性,请使用 @SessionAttributes, or a FlashAttributes.
根据您的 html 表格,您有:
<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</p>
你的意思是你想在同一页面中显示验证错误,然后不要重定向 return 同一视图。
if (bindingResult.hasErrors()) {
//return "redirect:/formPrinter";
return "your html form view";
}
然后,在您的视图中,您可以呈现所有验证错误消息,例如:
<p th:each="err : ${#fields.errors('*')}" th:text="${err}"></p>
我有同样的问题,通过将我的@PostMapping 方法中的参数重新排序为 (@Valid @ModelAttribute Form form, BindingResult bindingResult, Model model)
当参数与您的控制器(@Valid @ModelAttribute Form 表单、Model 模型、BindingResult bindingResult)的顺序相同时,我也被重定向到 /error 而没有调用控制器 @PostMethod。
我实际上正在研究 Spring 框架以及 Spring Boot 和 Sprint MVC。 我正在做一个表单,其中 post 一些数据来完成一个对象,我需要验证一些值。
事实是验证有效,事实上,如果我不尊重验证,我会 "redirected"(实际上不是,URL 不会改变)error.html内容。
我应该如何正确管理我的重定向?这样不行:
@RequestMapping(value = "/print", method = RequestMethod.POST)
public String printPost(@ModelAttribute("printerentity") @Valid PrinterEntity print, Model model, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "redirect:/formPrinter";
}
model.addAttribute("printed", print.getName());
model.addAttribute("printerentity", new PrinterEntity());
return "index";
}
和表格:
<form method="post" th:action="@{/print}" th:object="${printerentity}">
<input type="text" th:field="*{name}"/>
<button type="submit">Valider</button>
<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</p>
</form>
我做错了什么?
编辑:似乎当我有错误时,我没有传入控制器代码:o =>
@RequestMapping(value = "/print", method = RequestMethod.POST)
public String printPost(@ModelAttribute("printerentity") @Valid PrinterEntity print, Model model, BindingResult bindingResult) {
System.out.println("I dont passe here when error but I m redirected");
if (bindingResult.hasErrors()) {
return "formPrinter";
}
model.addAttribute("printed", print.getName());
model.addAttribute("printerentity", new PrinterEntity());
return "index";
}
感谢提前
How should I do to manage my redirection correctly ?
当你重定向请求时,当前请求被销毁,并创建一个新的请求对象来处理请求,所以根据 Sotirios Delimanolis 在评论中提到的,模型属性是请求属性,可用于仅针对每个请求,如果您想在请求之间的 HTTP 会话中存储模型属性,请使用 @SessionAttributes, or a FlashAttributes.
根据您的 html 表格,您有:
<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</p>
你的意思是你想在同一页面中显示验证错误,然后不要重定向 return 同一视图。
if (bindingResult.hasErrors()) {
//return "redirect:/formPrinter";
return "your html form view";
}
然后,在您的视图中,您可以呈现所有验证错误消息,例如:
<p th:each="err : ${#fields.errors('*')}" th:text="${err}"></p>
我有同样的问题,通过将我的@PostMapping 方法中的参数重新排序为 (@Valid @ModelAttribute Form form, BindingResult bindingResult, Model model)
当参数与您的控制器(@Valid @ModelAttribute Form 表单、Model 模型、BindingResult bindingResult)的顺序相同时,我也被重定向到 /error 而没有调用控制器 @PostMethod。