18

Primefaces 3.5, Omnifaces 1.6

I have a group of buttons with icons. The buttons have an action to do on the page (such as delete or add new row in datatable). I want to add new "something" that looks exactly as buttons do, but with an external link. If I click on this new button, new tab/window have to be opened. For that purpose I am using p:commandButton and h:outputLink.

<p:commandButton action="#{bean.do1}" icon= ...>
<p:commandButton action="#{bean.do2}" icon= ...>

<h:outputLink value="#{bean.url}" target="_blank"> 
  <i class="icon-external-link"></i> 
</h:outputLink>

How can I achieve this ?

Aritz
  • 30,971
  • 16
  • 136
  • 217
Tony
  • 2,266
  • 4
  • 33
  • 54
  • Related: [How to create an HTML button that acts like a link?](http://stackoverflow.com/q/2906582) – BalusC Jun 09 '16 at 10:02

1 Answers1

45
  • Use a p:button, which acts like a link:

    <p:button href="http://www.stackoverflow.com" value="Go to SO" />
    
  • If you want a blank target and starting from Primefaces 3.5.5, there's the chance to use the target attribute directly:

    <p:button target="_blank" href="http://www.stackoverflow.com" value="Go to SO" />
    
  • When being below PF 3.5.5, you could do some javascript to open it in a blank target:

    <p:button value="Go to SO" onclick="window.open('http://www.stackoverflow.com')" />
    
  • All of the choices above use javascript to change the browser's window location. In order to generate a bot-harvestable HTML link element, make use of a h:outputLink (or just a plain HTML a element), and style it using Primefaces' classes:

    <h:outputLink value="http://www.stackoverflow.com"
        styleClass="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">
        <span class="ui-button-text">Go to SO</span>
    </h:outputLink>
    

See also:

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217
  • 3
    The `` already supports `target` attribute by itself. Noted should be that this all uses JS under the covers which is on contrary to the link not searchbot-indexable per se. – BalusC Sep 20 '13 at 12:36
  • @BalusC which PF version? Not for me in 3.5 :-( – Aritz Sep 20 '13 at 12:38
  • 1
    Sorry, without I really realized, I've somehow 4.0 RC1 in my playground project. This is indeed absent in 3.5. – BalusC Sep 20 '13 at 12:46
  • @XtremeBiker Thank you for your answer! I've used your suggestion. But in PF 3.5.14 the "target"-attribute is available, so I can actually use it! – Tony Sep 20 '13 at 12:57
  • @BalusC Your comment has also helped me, because in PF 3.5.14 the "target" attribute is available. Also thank you for nice CDI features in OF1.6. – Tony Sep 20 '13 at 12:59
  • You seriously saved my day... Thank you! @XtremeBiker – allegjdm93 Dec 19 '13 at 21:35