0

我想下载 csv 文件,我把响应内容写入它,然后写在我之后,我得到的内容是页面 html,它应该是我的 csv

我在示例中看到了 throw new AbortException(); 的用法。我使用的是 6.7 版本,你知道我的版本检票口是否有一些东西而不是它吗?或者更确切地说我做错了什么......

你能帮我么 ...

 add(new Link<Void>("export") {
        @Override
        public void onClick() {

            WebResponse response = (WebResponse) getResponse();
            response.setAttachmentHeader("export.csv");
            response.setContentType("text/csv");

            OutputStream out = getResponse().getOutputStream();

            try {
                c.exportData(dataSource.getListForExport(), columns, out);

            } catch (Exception ex) {
                System.err.println(ex);
            }

        }

    });




   public <T> void exportData(List<T> list, List<IGridColumn<IDataSource<T>, T, String>> columns, OutputStream outputStream)
        throws IOException {
    long startTime = System.nanoTime();
    PrintWriter out = new PrintWriter(new OutputStreamWriter(outputStream, Charset.forName(characterSet)));
    try {
        if (isExportHeadersEnabled()) {
            boolean first = true;
            for (IGridColumn<IDataSource<T>, T, String> col : columns) {
                if (first) {
                    first = false;
                } else {
                    out.print(delimiter);
                    System.out.println(delimiter);
                }
                if (col.getId().equals("checkBox")) {
                    continue;
                }
                out.print(quoteValue(col.getId()));
                System.out.println(col.getId());
            }
            out.print("\r\n");
            System.out.println("\r\n");
        }
        Iterator<? extends T> rowIterator = list.iterator();
        while (rowIterator.hasNext()) {
            T row = rowIterator.next();

            boolean first = true;
            for (IGridColumn<IDataSource<T>, T, String> col : columns) {
                if (first) {
                    first = false;
                } else {
                    out.print(delimiter);
                    System.out.println(delimiter);
                }
                if (col.getId().equals("checkBox")) {
                    continue;
                }
                Object o = (new PropertyModel<>(row, col.getId())).getObject();// ((AbstractColumn<T,

                if (o != null) {
                    Class<?> c = o.getClass();
                    String s;
                    IConverter converter = Application.get().getConverterLocator().getConverter(c);

                    if (converter == null) {
                        s = o.toString();
                    } else {
                        s = converter.convertToString(o, Session.get().getLocale());
                    }

                    out.print(quoteValue(s));
                    System.out.println(quoteValue(s));
                }
            }
            out.print("\r\n");
            System.out.println("\r\n");
        }
    } catch (Exception ex) {
        System.out.println(ex);
    } finally {
        System.out.println(out);
        out.close();

        // measure
        System.out.print(System.nanoTime() - startTime);
    }
}
4

1 回答 1

3

最好的方法是使用动态资源。我建议您阅读这个宏伟的免费 Wicket 指南的“使用 Wicket 进行资源管理”一章:https ://code.google.com/p/wicket-guide/ 。

在这里,您在本指南的“自定义资源”部分中给出了一个类似的示例。

public class RSSProducerResource extends AbstractResource {

@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
     ResourceResponse resourceResponse = new ResourceResponse();
     resourceResponse.setContentType("text/xml");
     resourceResponse.setTextEncoding("utf-8");
     resourceResponse.setWriteCallback(new WriteCallback()
     {
        @Override
        public void writeData(Attributes attributes) throws IOException
        {
             OutputStream outputStream = attributes.getResponse().getOutputStream();
             Writer writer = new OutputStreamWriter(outputStream);
             SyndFeedOutput output = new SyndFeedOutput();
             try {
                 output.output(getFeed(), writer);
             } catch (FeedException e) {
                 throw new WicketRuntimeException("Problems writing feed to                     response...");
             }
        }
     });
     return resourceResponse;
}

// method getFeed()...

}

然后您需要在所需的页面或组件中添加链接:

add(new ResourceLink("rssLink", new RSSProducerResource()));
于 2013-05-28T00:51:40.210 回答