The following code, when referenced from the beforePhase property of the f:view component, automatically opens the browser print dialog if a page is rendered as printable. I did write about this topic in the OTN Harvest summary of January 2011. To this time however I used an internal flag, which doesn't feel right. The code used in this post only uses public APIs and thus is a solution that lasts.
public void beforePhaseMethod(PhaseEvent phaseEvent) { if (phaseEvent.getPhaseId() == PhaseId.RENDER_RESPONSE){ FacesContext fctx = FacesContext.getCurrentInstance(); AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance(); if(adfFacesContext.getOutputMode()== OutputMode.PRINTABLE){ ExtendedRenderKitService erks = null; erks = Service.getRenderKitService(fctx, ExtendedRenderKitService.class); erks.addScript(fctx, "window.print();"); } } }
The managed bean method is referenced from the f:view component as follows
<f:view beforePhase="#{SampleBean.beforePhaseMethod}">
...
</f:view>
With this listener and code, when the af:showPrintableBehavior tag is used on a command item to show a printable page, the browser print dialog is automatically opened. While the same code also works for page fragments, the f:view tag is only available for JSPX documents. In this case you either set the beforePhase method property on the JSPX document hosting the page fragment, or define a global phase listener (faces-config.xml) that then works for all pages in an application.
原文:http://blogs.oracle.com/jdevotnharvest/entry/how_to_launch_browser_print