Question

I am trying to work out a way to provide a CSV download through a Spring 3 Portlet. I have a method that uses the @ResourceMapping annotation to define a handler that takes some report params in the form of a @ModelAttribute, builds the report, and returns it. The catch-22 I am running into is validating the parameters being send in from the client form.

If I make the handler a @ResourceMapping, I can set the headers and write out the report as using the ResourceResponse, but I can't seem to figure out how to redirect the user back to the Portlet view with errors when their input fails validation. However, if I make it an @ActionMapping, I can then check the BindingResults and forward them back to the form as needed, but the ActionResponse doesn't allow me to set the Content-Disposition header nor write out the CSV bytes, which is sort of critical for sending the report back.

I am at a total loss here, as I don't even know what my options are. Is it even possible to do what I am trying to do with a Portlet? Are there other examples I could look at for a possible work-around?

Was it helpful?

Solution

I suggest you to use both @ActionMapping and @ResourceMapping to fulfill your requirement.

As you said you were able to handle the validation errors using the @ActionResponse, I'll tell you how to handle the Resource Streaming.

As you know every @ActionResponse is followed by a @RenderResponse, just return the same view but, with a hidden iframe this time whose src points to the ResourceURL.

Now the Request you receive in @ResourceMapping is something which is already Validated. So, you can now serve your CSV.

I dont know how complex is your UI and if you are using jsp as views in your application. If nicely managed, Validation can be handled by @ResourceMapping.

Thank you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top