简体   繁体   English

如何在Struts2中定义StreamResult的输出名称?

[英]How can I define the output name of a StreamResult in Struts2?

Guys, I cant find this info clearly in the web. 伙计们,我在网络上找不到这些信息。 I have an action and I'm generating a text file, however is always appearing to the client as a "generatePDF.action" file. 我有一个动作,正在生成一个文本文件,但是始终以“ generatePDF.action”文件形式出现在客户端上。 I want it to show as a receipt.txt file. 我希望它显示为receive.txt文件。

Here is my annotation: 这是我的注释:

   @Action(value = "/generateTXT",
    results = {
        @Result(name = "ok", type = "stream",
        params = {"inputName", "inputStream",
                  "contentType", "application/octet-stream",
                  "contentDispostion", "attachment;filename=receipt.txt"})
    })

If you are using the conventions plug-in then lets use the following code for reference runs under "/YourApplicationContext/stream/stream-test" which then resolves to "/YourApplicationContext/stream/document.txt": 如果您使用的是约定插件,则可以在“ / YourApplicationContext / stream / stream-test”下使用以下代码作为参考运行,然后解析为“ /YourApplicationContext/stream/document.txt”:

package struts2.stream;

import com.opensymphony.xwork2.ActionSupport;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import org.apache.struts2.convention.annotation.Result;


@Result(name = ActionSupport.SUCCESS, type = "stream", params =
{
    "contentType",
    "text/hmtl",
    "inputName",
    "inputStream",
    "contentDisposition",
    "filename=document.txt"
})
public class StreamTestAction extends ActionSupport{
    public InputStream inputStream;

    @Override
    public String execute(){
    inputStream = new StringBufferInputStream("Hello World! This is a text string response from a Struts 2 Action.");      
    return SUCCESS;
    }
}

Please take note of "contentDisposition" and that its value has been set to "filename='document.txt'" changing 'document.txt' gets you what you want. 请注意“ contentDisposition”,并且其值已设置为“ filename ='document.txt'”。更改“ document.txt”即可获得所需的内容。

My annotation is basically the same, but I used a reference to set file's name: 我的注释基本上是相同的,但是我使用了引用来设置文件名:

@Result(name="export", type="stream",
   params={ "contentType", "application/octet-stream",
    "inputName", "fileInputStream",
    "contentDisposition", "attachment;filename=%{exportFilename}",
    "bufferSize", "4096"})

exportFilename is a String variable with getter and setter and it can also be put at a inheritable class, so it is possible to create an unique ExportAction and make all actions extend it. exportFilename是具有getter和setter的String变量,也可以将其放在可继承的类中,因此可以创建唯一的ExportAction并使所有操作对其进行扩展。

Probably you can create variables to set all parameters' values. 可能您可以创建变量来设置所有参数的值。

The original annotation is fine, it only contains a typo: 原始注释很好,它只包含一个错字:

"contentDispostion" should read "contentDisposition" “ contentDispostion”应显示为“ contentDisposition”

I took me ages to figure this out, so I thought I'd make it clear :-) 我花了很长时间才弄清楚这一点,所以我想我要说清楚了:-)

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

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