How-to launch browser print dialog when showing printable page

本文介绍了一种方法,可以在页面呈现为可打印格式时自动打开浏览器的打印对话框。通过使用公共API,该方法实现了当使用af:showPrintableBehavior标签显示可打印页面时自动调用打印功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值