tags:

views:

104

answers:

2

I need to make a link which opens print version of current page in a new tab. I already have correspondent css-file. But I don't know how to specify when this file should be used instead of standard.

The simplest way is quite good. If I was using JSP I would simply add get parameter to print-link URL. Is there any way to achieve similar results with jsf?

+1  A: 

You can add get parameters to any JSF link by using the f:param tag.

<h:outputLink value="/somepage.xhtml" target="_blank">
   <h:outputText value="Link to Some Page"/>
   <f:param name="someparam" value="somevalue">
</h:outputLink>

This will render something basically like this:

<a href="/somepage.xhtml?someparam=somevalue" target="_blank">Link to Some Page</a>

You can add multiple params with more f:param fields. Alternatively, if it's static, you can just add it as part of the outputLink itself.

<h:outputLink value="/somepage.xhtml?someparam=somevalue" target="_blank">
   <h:outputText value="Link to Some Page"/>
</h:outputLink>

The problem, of course, being that you cannot do this and trigger server-side events. I've yet to figure out how to do this from a POST back and get it in a new tab.

Drew
+2  A: 

Use EL to specify the CSS file dynamically, here's an example which checks the presence of the print request parameter (thus, <h:outputLink value="page.jsf?print" target="_blank"> would suffice):

<link rel="stylesheet" type="text/css" href="${not empty param.print ? 'print.css' : 'normal.css'}" />

You can also retrieve it as a bean proprerty the usual JSF way:

<link rel="stylesheet" type="text/css" href="<h:outputText value="#{bean.cssFile}" /> " />

If you're on Facelets instead of JSP, then you can also use unified EL in template text:

<link rel="stylesheet" type="text/css" href="#{bean.cssFile}" />

If you actually don't need a "print preview" tab/page, then you can also just specify the media attribute in the CSS link and let the link/button invoke window.print() during onclick instead of opening in a new tab.

<link rel="stylesheet" type="text/css" href="normal.css" media="screen, handheld, projection" />
<link rel="stylesheet" type="text/css" href="print.css" media="print" />

When the page is about to be printed, the one specified by media="print" will automatically be used instead.

BalusC
I've already specified the media attribute but for some reasons client wants a link.
Roman
Fine, I expanded the answer with few more possibilties than just `${param}`.
BalusC