简体   繁体   English

如何从无提交按钮调用Struts2操作

[英]How to call Struts2 action from no submit button

I need to know how to call a Struts2 action from a button that does not submit the form. 我需要知道如何从不提交表单的按钮调用Struts2操作。 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). 当我单击按钮时,它应该调用Struts2操作,该操作将对SSRS服务器(SQL Server Reporting Services)进行Web服务调用。 The server sends the report to a stream that I put in the Response. 服务器将报告发送到我放入响应中的流。 It then displays a popup to download the PDF file. 然后,它显示一个弹出窗口以下载PDF文件。 With JSF it's easy, the commandButton provides an "action" attribute. 使用JSF很容易,commandButton提供了一个“动作”属性。 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? 如何在Struts2中使用? Ajaxa? 阿贾克斯? JQuery? jQuery的? Dojo? 道场 I am new to Struts2 and Ajax, I do a lot of JSF. 我是Struts2和Ajax的新手,我做了很多JSF。

thank you 谢谢

Without submitting the form, you would need AJAX . 如果不提交表单,则需要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). 但是,由于您只需要下载PDF,然后只需对返回操作结果 (不会更改当前页面)的操作执行GET调用即可。

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). 指定contentDisposition: attachment ,您将询问用户在哪里下载文件(或使用哪个应用程序打开文件),而contentDisposition: inline将在浏览器中打开文件,从而更改页面(这不是您想要的)。

Then specify the action in url and use it anchor tag 然后在url中指定操作并使用它的定位标记

<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 并在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 . 在Action中,您需要(通过getter)提供一个称为inputStreamInputStream

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';"
/>

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: 在dojo中,您可以创建一个iframe并进行ajax调用以报告服务以获取pdf文件名,然后在ifrmae中下载此pdf文件:

    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: 或者,您可以使用window.open直接将pdf流下载到新窗口中,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM