struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Li Ying <liying.cn.2...@gmail.com>
Subject Re: Struts2 - Generate XML in an Action, then direct user to download .xml
Date Wed, 13 Oct 2010 05:02:47 GMT
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
View raw message