smartGWT, OSGi export to excel and PDF

刚给公司一个web项目添加了前台export表格到excel以及pdf的功能,下面把实现过程记录在这篇blog中。

一些背景交待:

1、UI技术使用的是smartgwt,MVP模式,表格ListGrid使用的是DataSource

2、整个项目基于OSGi,下面提到的ExportService接口和ExportServiceImpl类不在同一个OSGi bundle中,原因是ExportService需要向几乎所有bundle开放,所以放到了common的bundle中。


第一步,写一个ExportForm

public class ExportForm extends DynamicForm {
    
    /**
     * The FORMAT.
     */
    public static final String FORMAT = "format";
    
    /**
     * The EXPORT.
     */
    public static final String EXPORT = "export";
    
    /**
     * Constructs a new <code>ExportForm</code> instance.
     */
    public ExportForm() {
        SelectItem exportType = new SelectItem(FORMAT, "Export Format");
        LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
        valueMap.put("pdf", "PDF");
        valueMap.put("Excel", "Excel");
        exportType.setValueMap(valueMap);
        exportType.setDefaultToFirstOption(true);
        ButtonItem exportButton = new ButtonItem(EXPORT, "Export");
        exportButton.setStartRow(false);
        setFields(exportType, exportButton);
        setNumCols(4);
        setTitleSuffix(null);
        setWidth100();
    }

}

注意:exportButton.setStartRow(false);是必要的,因为smartgwt中有些控件,比如Button,默认会另起一行,不管setNumCol(int)设置为多大。
第二步,把这个form控件加到合适的地方。(省略ExportService接口, ExportServiceImpl类, 以及Export单例类的实现,有些代码下面有引用到)
第三步,在ListGrid所在的View对应的Presenter中添加exportButton的ClickHandler处理。当用户点击这个按钮,Presenter就会new一次DataSource(因为export需要export所有的数据,而ListGrid的display只需要一页的数据,所以不能直接从ListGrid export),设置好所有的参数,然后fetch data。将response.getData(), ListGrid.getFields(), 用户选择export的format, 以及fileName传给ExportService.
dataSource.fetchData(new Criteria(), new DSCallback() {
                    
                    @Override
                    public void execute(DSResponse response, Object rawData, DSRequest request) {
                        Export.getInstance().export(response.getData(), gridDisplay.getDataGrid().getFields(), format, fileName);
                    }
                });





第四步,注册ExportService, ExportServiceImpl之间的servlet mapping。先把ExportService注册到一个service path上,有两种方法:1、使用@RemoteServiceRelativePath()这个annotation
@RemoteServiceRelativePath("export")
什么是RemoteServiceRelativePath annotation?
Associates a RemoteService with a relative path. This annotation will cause the client-side proxy to automatically invoke the ServiceDefTarget.setServiceEntryPoint(String) method with GWT.getModuleBaseURL() + value() as its argument. Subsequent calls to ServiceDefTarget.setServiceEntryPoint(String) will override this default path.

这样注册之后,每当前台call这个Export,就会找一个GWT.getModuleBaseURL() + value()的url,即http://....../发出申请的gwt project的名称/export。上面笔者提到这个service是被share的,任何一个gwt project都会call到这个service,因此需要使用第二种注册方法。


2、使用((ServiceDefTarget) exportService).setServiceEntryPoint("/.../export");这样注册之后,每当前台call这个Export,都会使用固定的URL。
private ExportServiceAsync exportService;
private static Export instance;
private Export() {
        exportService = GWT.create(ExportService.class);
        ((ServiceDefTarget) exportService).setServiceEntryPoint("/projectNameAlias/export");
    }
public static Export getInstance() {
        if (instance == null) {
            instance = new Export();
        }
        return instance;
    }

接下来在ExportServiceImpl所在的项目的plugin.xml中注册export service的implementation。

<servlet
        alias="/projectNameAlias/export"
        class="packageName.server.ExportServiceImpl">
  </servlet>
</extension>
<extension point="org.eclipse.equinox.http.registry.servlets">

第五步,在ExportServiceImpl中使用java.io实现到Excel的输出,使用itextpdf实现到PDF的输出。

关于itextpdf的使用,这里有一个很好的tutorial blog





















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值