【问题标题】:How to call Struts2 action from no submit button如何从无提交按钮调用 Struts2 操作
【发布时间】:2013-09-24 15:19:45
【问题描述】:

我需要知道如何从不提交表单的按钮调用 Struts2 动作。当我单击该按钮时,它应该调用一个 Struts2 操作,该操作将对 SSRS 服务器(SQL Server Reporting Services)进行 Web 服务调用。服务器将报告发送到我放入响应中的流。然后它会显示一个弹出窗口来下载 PDF 文件。使用 JSF 很简单,commandButton 提供了一个“action”属性。我想要这样的等价物:

<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"}}

在 Struts2 中如何?阿贾沙?查询?道场?我是 Struts2 和 Ajax 的新手,我做了很多 JSF。

谢谢

【问题讨论】:

标签: java jquery ajax struts2 dojo


【解决方案1】:

如果不提交表单,您将需要AJAX

但由于您只需要下载 PDF,因此只需对返回 Stream Result 的操作执行 GET 调用(并且不会更改当前页面)。

Read more here

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

然后在url中指定action并使用锚标签

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

在 Struts.xml 中

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

在 Action 中,您需要(通过 getter)提供一个名为 inputStreamInputStream

编辑

如果你想通过一个按钮来做,就像你在 cmets 中问的那样:

<input  type = "button" 
       value = "download" 
     onclick = "javascript:location.href='downloadAction.action';"
/>

【讨论】:

  • 谢谢,这是一个很好的起点,但如果我必须使用输入类型按钮?他没有定义动作属性。怎么办?
  • 感谢您的帮助,我会尝试这个解决方案。
  • 你好这个解决方案有效,除了当我点击按钮时,表单中输入的数据没有设置。它始终为空。
  • 您没有提供足够的信息来帮助您。发布以下相关部分:您的操作代码、您的 Struts.xml 配置和您的 JSP sn-p。顺便说一句,您必须为每个具有正确名称的字段设置 getter 和 setter,在 jsp 中匹配,并通过一个至少包含基本拦截器的拦截器堆栈才能工作。
  • 我发布了一个带有代码示例的主题。你可以在这里找到它:stackoverflow.com/questions/19121675/…
【解决方案2】:

在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);
                }
            );
        }
    });  

或者你可以使用window.open直接将pdf流下载到新窗口中,比如:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 2014-03-03
    • 2013-07-13
    • 2013-06-30
    • 2014-05-14
    • 2012-11-30
    • 2013-12-11
    相关资源
    最近更新 更多