Java 如何从没有提交按钮调用 Struts2 操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18985596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call Struts2 action from no submit button
提问by Ousmane MINTE
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:
我需要知道如何从不提交表单的按钮调用 Struts2 操作。当我单击按钮时,它应该调用 Struts2 操作,该操作将对 SSRS 服务器(SQL Server Reporting Services)进行 Web 服务调用。服务器将报告发送到我放入响应中的流。然后它会显示一个弹出窗口来下载 PDF 文件。使用 JSF 很容易,commandButton 提供了一个“action”属性。我想要相当于这个:
<h:commandButton id="editButton" value="Edit" action="#{myBean.edit}" />
<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"}}
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.
在 Struts2 中如何?阿贾克斯?查询?道场?我是 Struts2 和 Ajax 的新手,我做了很多 JSF。
thank you
谢谢你
采纳答案by Andrea Ligios
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,因此只需对返回流结果的 Action 执行 GET 调用(并且不会更改当前页面)。
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)提供一个InputStream
被调用的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';"
/>
回答by Sean Zhao
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调用report service来获取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");
}
});