How to open a printable version of a site on a new page using jsf?

I need to create a link that opens a new version of the current page in a new tab. I already have a correspondent css file. But I do not know how to indicate when this file should be used instead of the standard one.

The easiest way is not bad. If I were using JSP, I would just add a get parameter for the print URL. Is there a way to achieve similar results using jsf?

+4
source share
2 answers

Use EL to dynamically specify the CSS file, here is an example that checks for the presence of the print request parameter (thus, <h:outputLink value="page.jsf?print" target="_blank"> is sufficient):

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

You can also get the regular JSF method as a bean proprerty:

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

If you use Facelets instead of JSP, you can also use a unified EL in the template text:

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

If you really don't need the print preview tab / page, you can also just specify the media attribute in the CSS link and let the link / button call 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 printed, only the one indicated by media="print" will be used instead.

+3
source

You can add parameters for any JSF link 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> 

It will look something like this:

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

You can add several parameters with a large number of f: param fields. Alternatively, if it is static, you can simply add it as part of outputLink itself.

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

The problem, of course, is that you cannot do this and trigger events on the server side. I have yet to figure out how to do this with POST back and get it in a new tab.

+1
source