有条件地渲染 JSF 组件以进行打印 [英] Conditionally render JSF components for printing

查看:29
本文介绍了有条件地渲染 JSF 组件以进行打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想打印网页的特定部分(而不是整个页面).我如何在 JSF 中实现这一点?

I want to print only a certain part of my webpage (so not the whole page). How can I achieve this in JSF?

推荐答案

这通常由 CSS 控制,display: none|block.检查 CSS 媒体规则.

This is normally to be controlled by CSS with display: none|block. Check the CSS media rules.

例如,作为默认 CSS 文件中的 @media print {}:

For example, as @media print {} inside a default CSS file:

@media print {
    #header, #footer, #menu { 
        display: none;
    }
}

(上面的例子将隐藏 ID 为 headerfootermenu 的 HTML 元素)

(the above example will hide HTML elements with IDs header, footer and menu)

或者通过通用样式类:

@media screen {
    .printonly { 
        display: none;
    }
}

@media print {
    .noprint { 
        display: none;
    }
    .printonly { 
        display: block;
    }
}

然后将 styleClass="noprint" 添加到您想要隐藏以防止打印的那些,并将 styleClass="printonly" 添加到您想要的那些仅以印刷形式显示.

You then add styleClass="noprint" to those which you'd like to hide from print, and styleClass="printonly" to those which you'd like to show in print only.

您还可以将特定于打印的 CSS 放在自己的样式表文件中,并使用 <link media="print"><h:outputStylesheet media="print"> 如下:

You can also put the print specific CSS in its own stylesheet file and reference it using <link media="print"> or <h:outputStylesheet media="print"> as below:

<link rel="stylesheet" href="#{request.contextPath}/css/print.css" media="print" />
<!-- Or -->
<link rel="stylesheet" href="#{resource['css/print.css']}" media="print" />
<!-- Or -->
<h:outputStylesheet name="css/print.css" media="print" />

#header, #footer, #menu { 
    display: none;
}

注意应该是 属性只在 JSF 2.1 中添加,所以如果你还在使用 JSF 2.0,考虑升级到至少 2.1(应该是 100%webapp 本身无需任何代码和配置更改即可兼容).否则,只需采用纯 HTML 方法.

Noted shoud be that <h:outputStylesheet media> attribute was only added in JSF 2.1, so if you're still on JSF 2.0, consider upgrading to at least 2.1 (should be 100% compatible without any code and configuration changes in the webapp itself). Otherwise just go for the plain HTML <link> approach.

这篇关于有条件地渲染 JSF 组件以进行打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆