struts2force-downloadjasper-plugin

JasperReport Plugin Struts2 force download


I'm using Struts2, Spring and Hibernate for my web app. Now, I'm trying to generate PDF reports with jasper reports struts plugin. I add the result-type to struts.xml file:

    <result-types> 
        <result-type name="jasper" class="org.apache.struts2.views.jasperreports.JasperReportsResult" default="false"/> 
    </result-types>

Action definition:

    <action name="vf" class="packaget.InvoiceAction" method="seeInvoice">
         <result name="success" type="jasper">
             <param name="location">/WEB-INF/reports/invoice.jasper</param>
             <param name="dataSource">invoiceSource</param>
             <param name="format">PDF</param>
             <param name="contentDisposition">filename="Invoice.pdf</param>
             <param name="documentName">Invoice.pdf</param>
             <param name="reportParameters">invoiceParameters</param>
         </result>
    </action> 

And here is my action class:

public class InvoiceAction extends BaseAction implements Preparable {       
  ...
  public String seeInvoice(){
         List<Invoice> invoice= new ArrayList<Invoice>();
         facturas.add(new Invoice());
         invoiceParameters.put("message","test");
        return SUCCESS;
    }

}

I want to generate a download popup, I don't want to see the report in the browser. How can I force this? contentDisposition or documentName params has no effect...

Thanks in advance.


Solution

  • You have to add "attachment" to the content disposition.

    <param name="contentDisposition">attachment</param>
    

    And remove the filename="Invoice.pdf". This param does the same as documentName. Using them together will duplicate this information in the HTTP response.

    <param name="contentDisposition">filename="Invoice.pdf"</param>