【发布时间】:2018-01-18 20:12:09
【问题描述】:
以下控制器提供一个简单的 html 页面,显示存储库中的所有人员。
问题:我在 get-query 上使用了验证约束。如果查询无效(在我的示例中:lastname 参数丢失),那么 spring 会自动抛出异常作为对浏览器的响应。
但我仍然想渲染 persons.html 页面,只显示存储库内容之外的错误。
问题:我怎样才能做到这一点?因为如果验证失败,下面的方法甚至都无法访问。
@Controller
@RequestMapping("/persons")
public class PersonController {
@GetMapping //note: GET, not POST
public String persons(Model model, @Valid PersonForm form) {
//on the persons.html page I want to show validation errors
model.addAttribute("persons", dao.findAll());
return "persons";
}
}
public class PersonForm {
private String firstname;
@NotBlank
private String lastname;
}
旁注:我使用thymeleaf 作为模板引擎。但同样的问题也适用于jsp 或jsf 引擎。
【问题讨论】:
标签: java spring jsp spring-mvc thymeleaf