3

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.validate(auditView, bindingResult);

The question is how to forward to Render phase in case of errors?

Mark Chorley
  • 2,087
  • 2
  • 22
  • 29
user657009
  • 722
  • 2
  • 6
  • 18

3 Answers3

0

It might be late to answer but, It may be helpful to others.

There is no way you can forward a Request to RenderPhase from ResourcePhase.

Please refer this link for a solution to a similar requirement.

Community
  • 1
  • 1
Niranjan
  • 1,776
  • 1
  • 13
  • 21
0

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.

fishbone
  • 3,140
  • 2
  • 37
  • 50
-1

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";
    } 
Josef Prochazka
  • 1,273
  • 2
  • 9
  • 28