Validate Data before Export via af:exportCollectionActionListener or af:fileDownloadActionListener

ADF rich faces offer a nice and easy feature to stream data to the client (e.g. Excel) using the af:exportCollectionActionListener or af:fileDownloadActionListener component. Both of the components get the output stream from the response, so the application an add the data.
One problem is that the two components fire before the application has a chance to validate other data on the page or do some other needed work.
To overcome this shortcoming you can implement the trick I’m outlining in this blog. The idea is to use a normal af:commandbutton or af:commandToolbarButton on the page which calls an actionListener in a bean. In there you can validate other page data or do some other stuff in the model, and then queue an event inside the bean method to call another invisible button on the page which has the af:exportCollectionActionListener attached.

As it turned out (thanks Jiandong Xin for pointing it out), there is a problem if the button we queue the action event to has a client behavior tag attached. This is the case for af:exportCollectionActionListener or af:fileDownloadActionListener. These tags don’t work properly if we only queue an event to the parent button. As Jobinesh pointed out in hisblogwe need to insert a JavaScript function into the page which handles the event queuing from JavaScript and call it from the bean. This way we are able to set the needed parameters (e.g. not to wait for a response).

Now lets implement this: Lets start with a toolbar button which resides on a panel collection holding the table we want to export. The button has a binding to the bean (binding=”#{exportBean.exportCollectionButton}”) to make it easier to call it from the bean. If you don’t like to bind the button to the bean, you can search for the component by name inside the action listener we implement below. The page contains the custom handler (customHandler(event)) which we call from the bean method. This handler then queues the event to activate the button which starts the file download. The connection between the customHander and hte commandToolbarButton is the ID of the commandToolbarButton (“ctb1″). You have to change this if your button has a different ID.

  1. <af:resourcetype="javascript">
  2. functioncustomHandler(event){
  3. varexportCmd=AdfPage.PAGE.findComponentByAbsoluteId("ctb1");
  4. varactionEvent=newAdfActionEvent(exportCmd);
  5. actionEvent.forceFullSubmit();
  6. actionEvent.noResponseExpected();
  7. actionEvent.queue();
  8. }
  9. </af:resource>
  10. <af:panelCollectionid="pc1">
  11. <f:facetname="menus"/>
  12. <f:facetname="toolbar">
  13. <af:toolbarid="t2"binding="#{exportBean.toolbar}">
  14. <af:commandToolbarButtontext="Export..."id="ctb1"
  15. binding="#{exportBean.exportCollectionButton}"
  16. visible="false">
  17. <af:exportCollectionActionListenertype="excelHTML"exportedId="t1"
  18. title="Export"/>
  19. </af:commandToolbarButton>
  20. ...

The vital part is that the visible property is set to “false”, so that the button will not show up on the page, but is fully functional. Somewhere else on the page (or even on the same toolbar) we have an with an action listener pointing to a bean method (exportBean in the sample). This button is visible and used to initiate the export.

  1. ...
  2. <af:commandButtontext="exportviabean"id="cb8"
  3. actionListener="#{exportBean.exportprgActionListener}"/>
  4. ...

The exportBean actionListener looks like this

  1. //toolbarbuttonwithexportlistnerattatchboundfrompage
  2. privateRichCommandToolbarButtonexportCollectionButton;
  3. ...
  4. publicvoidexportprgActionListener(ActionEventactionEvent){
  5. //Addeventcode(validationorother)here...
  6. //queuetheeventviaaJavaScriptinsertedintothepage
  7. FacesContextcontext=FacesContext.getCurrentInstance();
  8. ExtendedRenderKitServiceerks=
  9. Service.getService(context.getRenderKit(),ExtendedRenderKitService.class);
  10. erks.addScript(context,"customHandler();");
  11. }

In the action listener above you can validate other data on the page or call service methods in your application module before starting the export, or cancel the export if you like. The export runs as if you click the af:exportCollectionActionListener directly.
You can download a sample workspaceBlogFileDownLoadTest.zip
After download remove the ‘.doc’ suffix!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值