struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Grant Overby <gove...@students.kennesaw.edu>
Subject Re: Struts2 - Generate XML in an Action, then direct user to download .xml
Date Wed, 13 Oct 2010 06:05:15 GMT
After some googling; I'm looking at the Result interface.

Would it be proper for the action to set a property, and use this custom
Result to read that property from ValueStack and write it to (with XStream
for isntance) ServletActionContext.getResponse().getOutputStream() ?

So Results exist for doing this for XML and/or JSON? It seems like it would
be re-occurring situation, espectially with ajax?

I'm just getting into Struts2; I'm looking to see if I'm doing things as
they ought to be done. Best practices, etc.


goverby@students.kennesaw.edu



On Wed, Oct 13, 2010 at 1:02 AM, Li Ying <liying.cn.2010@gmail.com> wrote:

> You can generate your XML dynamically, and then write it to the response
> stream.
> If you set the response headers correctly, the browser should treat
> this response as a file downloading.
>
> Code in your action looks like this:
>
> public String execute() {
>      String xmlStr = generateXML();
>
>      HttpServletResponse response = ServletActionContext.getResponse();
>
>      response.setContentType("text/xml");
>
>      response.setHeader("Content-Disposition",
>                        "attachment; filename=" + yourXmlFileName);
>
>      OutputStream out = response.getOutputStream();
>
>      out.write(xmlStr.getBytes("UTF-8"));
>
>      out.close();
>
>      return null;  // this make sure the page not to jump
> }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message