我的情况:用户可以下载文件。他可以选择一个文件列表。 有弹簧映射:
@ResourceMapping(DOWNLOAD)
public void downloadSelected(ResourceRequest request, ResourceResponse response, AuditView auditView, BindingResult bindingResult) {
}
auditView
有一个文件列表。
如果用户没有选择我需要验证的任何内容并显示同一页面并显示错误。
我可以验证:validator.validate(auditView, bindingResult);
问题是如果出现错误,如何转发到渲染阶段?
答案 0 :(得分:0)
答案 1 :(得分:0)
我只使用WebSphere Liberty Profile的portlet容器测试了这个,所以我不知道它是否适用于其他容器:
@ResourceMapping
public void downloadSelected(@Valid @ModelAttribute("entity") Entity entity, BindingResult bindingResult, ResourceResponse response)
{
if (bindingResult.hasErrors()) {
response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "302");
response.setProperty("Location", response.createRenderURL().toString());
} else {
response.setContentType("application/pdf");
response.setProperty("Content-disposition", "attachment; filename=\"mydownload.pdf\"");
/* ... */
}
}
但是,如果使用Spring MVC的<form:errors />
JSP标记,似乎绑定结果会丢失并且错误消息不会出现在呈现阶段。
答案 2 :(得分:-1)
只检查错误并返回表单视图,并使用@Valid和@ModelAttribute anotations注释AuditView。 @Valid注释将验证控制器验证器的方法。 @ModelAttribute将把AuditView放入模型中。
@ResourceMapping(DOWNLOAD)
public void downloadSelected(ResourceRequest request, ResourceResponse response,@Valid @ModelAttribute("auditView") AuditView auditView, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "thedownloadpage";
}