1

I want to know if there is a way to download the generated HTML from a Struts2 action (the final HTML after processing the jsp), using the normal result types. I don't want de page to be displayed, but instead to download it.

What I have tried:

<result name="success" type="stream">
   <param name="location">/jsp/dummy.jsp</param>
   <param name="contentType">application/text</param>
   <param name="contentDisposition">attachment; filename="page.html"</param>
</result>

I don´t want to use something like:

UrlReader.read("generateHTMLAction")

and putting that in an input stream, because I'm having some encoding issues. Any idea will be apreciated. Thank you everyone.

Germán
  • 328
  • 6
  • 15
  • why would you want that from the browser, where you can just view source already? In any case, no, not using the default result types. you could always use a plain filter to change the mime type. – Dave Newton Nov 28 '12 at 15:34
  • Can't you just use the standard dispatcher result type and set the HTTP headers appropriately using the JSP directives? – chad Nov 28 '12 at 16:05
  • @DaveNewton My code is used to generate jqgrid list from several tables. They are generate using reflection, to adjust to the Bean attributes names and types. I want the user to be able to download the HTML for each table, and use it at desired. – Germán Nov 28 '12 at 19:29
  • @chad Will try with that aproach. Hope struts don't get in the way. – Germán Nov 28 '12 at 19:32
  • @German Struts won't get in your way; it's more likely the browser that will give you some trouble. – chad Nov 28 '12 at 19:55
  • @chad Used your way, and it's working fine. Added this: `<%response.setHeader("Content-Disposition", "attachment; filename=test.html");%>` to the jsp. Its not the struts way, so I will keep testing for a little longer, but at least it's working. Thank you so much!! – Germán Nov 28 '12 at 20:11
  • @German I wrote an official answer, you should edit it with the specifics of how you accomplished your goal. – chad Nov 28 '12 at 20:31
  • The best answer has been provided by chad, to go further you would need to buffer the result. This answer addresses the issues http://stackoverflow.com/questions/1075827/execute-jsp-directly-from-java/1076056#1076056 – Quaternion Nov 29 '12 at 04:26

2 Answers2

1

The handling of the returned response is mostly determined by HTTP headers and how they are interpreted by the browser. So, you can use the standard dispatcher result type and a JSP, as far as I know. You can use JSP directives to set the appropriate HTTP headers to make the browser treat the response payload in whatever manner you like.

chad
  • 7,369
  • 6
  • 37
  • 56
  • Well, finally I implemented `ServletResponseAware` in the Action, and added the line
    `response.setHeader("Content-Disposition", "attachment; filename=" + bean +".jsp");` to avoid modifing the jsp. The struts.xml remained simple. `/jsp/page.jsp` Thank you so much everyone for your help @chad @DaveNewton
    – Germán Nov 30 '12 at 13:26
  • No sweat. Out of curiosity, why did you want to keep the header config out of the jsp? – chad Nov 30 '12 at 15:11
  • I'm using the jsp both for display and download, so I have 2 different actions which calls the same jsp. So to avoid adding logic to the jsp, i modified only one of the Actions. I known, I know, i'm messy, but see my others questions, and you will know I'm doing generic stuff. – Germán Nov 30 '12 at 17:00
0

I'm not aware IF or HOW this can be accomplished;

what I can say to you is that:

  • it does not exist any location param here: it is from other result types (DispatchResult, FreeMarker, etc), not from Stream type. Nice try, but I'm pretty sure there is nothing out-of-the-box among Struts2 result types that fits your needs;

  • You can't use Interceptor's PreResultListener feature, because it works on the final Result but before it is rendered, then jump off the Interceptors completely;

    My 2 cents:

    IF you want to do this for debug purpose, and not programmatically, and then the real problem is that you can't use View Source browser's feature because the result is already "contaminated" by the browser parsing, THEN you can try to use the PlainText result type (untested, it's just an idea, I've never used it), to get the raw content of the JSP eventually setting your charSet, and then read the raw JSP with an appropriate editor (Eclipse, Notepad++, etc).

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • 1
    The plaintext result returns unparsed JSP/etc. View source isn't the same as viewing the DOM, e.g., if you turn on web developer markup it's not in the source, but is in the DOM when you open up the inspector. – Dave Newton Nov 28 '12 at 15:48
  • Thanks, that's *real* raw then :) – Andrea Ligios Nov 28 '12 at 16:04