Open a link in new tab/window without loosing the view scope of current JSF page

Shivagoy picture Shivagoy · Jan 24, 2014 · Viewed 6.9k times · Source

I need to open a new JSF page (say 2.xhtml) in new tab from current JSF page (say 1.xhtml). Which JSF component should I use for it? <h:commandLink> or <h:outputLink>?

I do not want to lose the scope of current page 1.xhtml after clicking on the link to open 2.xhtml in new tab.

The bean of 1.xhtml is @ViewScoped. Should I change it to @RequestScoped?

Answer

BalusC picture BalusC · Jan 24, 2014

In HTML, a link which opens the given URL in a new window/tab via a GET request is to be achieved using <a ... target="_blank">.

In JSF you can just write down plain vanilla HTML:

<a href="#{request.contextPath}/2.xhtml" target="_blank">Open 2.xhtml in new window</a>

You can also use <h:outputLink>, which is only beneficial if you ever want to use its rendered attribute:

<h:outputLink value="#{request.contextPath}/2.xhtml" target="_blank">Open 2.xhtml in new window/h:outputLink>

You can also use <h:link> which can take a navigation outcome instead of an URL, JSF will then generate the right URL:

<h:link value="Open 2.xhtml in new window" outcome="2" target="_blank" />

The bean scope is irrelevant to this all. Just choose the right one for the data it holds. The <h:commandLink> is insuitable as it fires a POST request instead of a GET request.

See also: