FreeMarker,WebWork 问题

发布于 2024-08-11 15:14:43 字数 359 浏览 14 评论 0原文

我有一个使用 freemarker、webwork 和 java 编写的 Web 应用程序。现在,当用户单击“getReport”时,java 代码返回字符串变量(名为“otchet”),其中包含纯文本的整个报告,并显示以下页面:

simple.ftl:

<#if (otchet?exists)>
     ${otchet}   
<#else>
    <@ww.text name="report.none"/>
</#if>

工作正常。但是,我想以文本/纯文件下载的形式向用户提供此报告(包含在变量“otchet”中)。

我该如何解决这个问题?

I have a web application written using freemarker, webwork and java. Now when user clicks on "getReport", java code returns the string variable (named "otchet") which contains the whole report in plain text and the following page is displayed:

simple.ftl:

<#if (otchet?exists)>
     ${otchet}   
<#else>
    <@ww.text name="report.none"/>
</#if>

It is working OK. However, I would like instead to offer user this report (contained in the variable "otchet") as a text/plain file download.

How can I solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦爱 2024-08-18 15:14:43

这正是 StreamResult 结果类型的用途。

示例:

在您的 WebWork XML 中:

<result name="download" type="stream">
    <param name="contentDisposition">filename=report.txt</param>
    <param name="contentType">text/plain;charset=UTF-8</param>
    <param name="inputName">inputStream</param>
    <param name="bufferSize">1024</param>
</result>

在您的操作中:

public InputStream getInputStream() {
    try {
        return new ByteArrayInputStream(getOtchet().getBytes("UTF-8"));
    }
    catch (UnsupportedEncodingException ex) {
        // Shouldn't happen with UTF-8.
        ex.printStackTrace();
    }
}

public String doDownload() {
    if (SUCCESS.equals(execute()) {
        return "download";
    }
    else {
        return ERROR;
    }
}

This is exactly what the StreamResult result type is for.

Example:

In your WebWork XML:

<result name="download" type="stream">
    <param name="contentDisposition">filename=report.txt</param>
    <param name="contentType">text/plain;charset=UTF-8</param>
    <param name="inputName">inputStream</param>
    <param name="bufferSize">1024</param>
</result>

In your action:

public InputStream getInputStream() {
    try {
        return new ByteArrayInputStream(getOtchet().getBytes("UTF-8"));
    }
    catch (UnsupportedEncodingException ex) {
        // Shouldn't happen with UTF-8.
        ex.printStackTrace();
    }
}

public String doDownload() {
    if (SUCCESS.equals(execute()) {
        return "download";
    }
    else {
        return ERROR;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文