`
dragonxiangfu
  • 浏览: 156804 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics