Spring portlet MVC. Validation before @ResourceMapping. @ResourceMapping -> @RenderMapping
My case: a user can download files. There is a list of files he can select. There is a spring mapping:
@ResourceMapping(DOWNLOAD)
public void downloadSelected(ResourceRequest request, ResourceResponse response, AuditView auditView, BindingResult bindingResult) {
}
auditView
has a list of files.
If user didn't select any I need to validate and display the same page with error displayed.
i can validate: validator开发者_如何学C.validate(auditView, bindingResult);
The question is how to forward to Render phase in case of errors?
It might be late to answer but, It may be helpful to others.
There is no way you can forward a
Request
toRenderPhase
fromResourcePhase
.
Please refer this link for a solution to a similar requirement.
I only tested this with WebSphere Liberty Profile's portlet container, so I don't know if it works with other containers:
@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\"");
/* ... */
}
}
However, it seems, that binding result gets lost and error messages aren't appearing in the render phase if Spring MVC's <form:errors />
JSP tag is used.
just check for errors and return the form view and annotate the AuditView with @Valid and @ModelAttribute anotations. @Valid annotation will triger validate method of controler validator. @ModelAttribute will put the AuditView into the model.
@ResourceMapping(DOWNLOAD)
public void downloadSelected(ResourceRequest request, ResourceResponse response,@Valid @ModelAttribute("auditView") AuditView auditView, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "thedownloadpage";
}
精彩评论