1

I need to know how to call a Struts2 action from a button that does not submit the form. When I click on the button it should call a Struts2 action that will make a web service call to a SSRS server (SQL Server Reporting Services). The server sends the report to a stream that I put in the Response. It then displays a popup to download the PDF file. With JSF it's easy, the commandButton provides an "action" attribute. I'd like the equivalent of this:

<h:commandButton id="editButton" value="Edit" action="#{myBean.edit}" />

public class MyBean { public String edit() call web service, put the stream on the response return "OK"}}

How in Struts2? Ajaxa? JQuery? Dojo? I am new to Struts2 and Ajax, I do a lot of JSF.

thank you

Ousmane MBINTE
  • 664
  • 4
  • 15
  • 37

2 Answers2

3

Without submitting the form, you would need AJAX.

But since you simply need to download a PDF, then simply perform a GET call to an Action that returns a Stream Result (and won't change the current page).

Read more here

Specifying contentDisposition: attachment, you will ask the user where to download the file (or which application to use to open it), while contentDisposition: inline would open it inside the browser, changing the page (that is not what you want).

Then specify the action in url and use it anchor tag

<s:url action="downloadAction.action" var="url">
    <s:param name="param1">value1</s:param>
</s:url>
<s:a href="%{url}" >download</s:a>

and in Struts.xml

<result name="success" type="stream">
   <param name="contentType">application/pdf</param>
   <param name="contentDisposition">attachment;filename="yourPDF.pdf"</param>
</result>

In the Action you need to provide (through a getter) an InputStream called inputStream.

EDIT

If you want to do it from a button, like you asked in the comments:

<input  type = "button" 
       value = "download" 
     onclick = "javascript:location.href='downloadAction.action';"
/>
Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thank you, this is a good starting point, but if I have to use the input type button ? He doesnt define action attribute. How do ? – Ousmane MBINTE Sep 25 '13 at 15:17
  • hello this solution works, except that when I click on the button, the data entered in the form are not setted. It is always null. – Ousmane MBINTE Sep 30 '13 at 12:53
  • You haven't provided enough informations to help you. Post the relevant parts of: your Action code, your Struts.xml config and your JSP snippet. Btw, you must have getters and setters for each field with the proper name, matching in jsp, and pass through an Interceptor Stack with at least the basic interceptors to work. – Andrea Ligios Sep 30 '13 at 12:58
  • I posted a topic with code examples. You can find it here : http://stackoverflow.com/questions/19121675/not-update-the-model-from-the-data-entered-in-the-form-after-submit-in-struts2 – Ousmane MBINTE Oct 01 '13 at 21:59
  • Please remember to accept answers when helpful (and to upvote them after you reach 15 reputation points)... – Andrea Ligios Dec 17 '13 at 14:44
0

In dojo, you can create an iframe and make an ajax call to report service to get back a pdf file name, then download this pdf file in ifrmae:

    var reportCP = new dijit.layout.ContentPane({
        content: dojo.create("iframe")
    });   

    new dijit.Button({
        label: "Report",
        onClick: function(){

            dojo.request.post(yourServiceUrl, {
                data : yourData,
                handleAs: "json"
            }).then( 
                function(response){
                    reportCP.content.src=response.fileName;   
                },
                function(error){
                    alert(error);
                }
            );
        }
    });  

Or you can use window.open to download the pdf stream into a new window directly , like:

new dijit.Button({
    label: "Report",
    onClick: function(){
        window.open(yourReportServiceUrl,"","height=600, width=1000, scrollbars=1");
    }
});  
Sean Zhao
  • 1,506
  • 12
  • 12