JSF 1.2 to open external link in new tab. without distrubing current flow [duplicate] - jsf

I need to open a JSF page in a new window by POST on click of a <h:commandButton>. I know I can acheive this using the JavaScript. But I would like to achive this using JSF and not JavaScript.
How can I achieve this? I'm using JSF 2.0.

The only non-JS way is to set target="_blank" in the parent <h:form>.
<h:form target="_blank">
...
<h:commandButton value="Open in new Window" />
</h:form>
This however affects all non-ajax(!) actions which are performed in the very same form. So if you're smart, make the action which shouldn't open in a new window an ajax action. However, ajax is also JavaScript and you mentioned that you don't want to use JS (I hope you don't get shocked once you discover that PrimeFaces is actually full of JavaScript).
If you absolutely need to restrict it to a single action, then you really can't go around asking little help to JavaScript.
<h:form>
...
<h:commandButton value="Open in new Window" onclick="this.form.target='_blank'" />
</h:form>

When you restrict the target only to a single action, maybe you want to get the form in its initial state.
With the oncklick action you set the target of the form to a _blan page.
After the click, the page is opened in a new tab/page (triggers the action event).
At last, the onblur action is triggered and set the form again to its initial state (the rest of the buttons will open in the _self page)
With this code, you can restrict to only a h:commandbutton to open in a new page.
The rest of the buttons will be opened in the self page:
<h:commandButton
onclick="this.form.target='_blank'"
onblur="this.form.target='_self'"
id="listadoRebuts"
action="#{giaquaBusquedaCorte.abrirListadoRebuts}"
value="#{msg.seqVisorbtnRecibos}">
</h:commandButton>

Related

How to open an arbitrary URL in new window using a PrimeFaces button

I have the below output link which does its job:
<h:outputLink value="#{verDocumentoController.url()}" target="_blank">
show document
</h:outputLink>
It opens an URL obtained as a bean property in a new window.
However, I'd like to turn the link into a button in PrimeFaces look'n'feel. I tried as below:
<p:commandButton value="show document" action="#{verDocumentoController.url()}"
onclick="form.target='_blank'" ajax="false" />
But it only reopens the current page in a new window and not the URL specified as bean property. How can I achieve this anyway?
The <p:commandButton> basically submits a POST request to the URL as specified by its parent <h:form>, which defaults indeed to the current request URL (you know, "postback"). The action attribute basically invokes a bean method and uses the returned value as navigation case outcome. An URL does not necessarily represent a sensible navigation case outcome.
Just use window.open() instead on a simple <p:button>.
<p:button value="show document"
onclick="window.open('#{verDocumentoController.url()}');return false;" />
You can also do this on a <p:commandButton>, but that's unnecessarily overcomplicated.

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

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?
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:
Open 2.xhtml in new window
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:
How to choose the right bean scope?
How to navigate in JSF? How to make URL reflect current page (and not previous one)
When should I use h:outputLink instead of h:commandLink?

Two consecutive events on button click in JSF

I have to perform two operations on button click.
call a method in a bean which will return a url and it will be set in some variable.
after that I have to open that url in another tab. I am using the action attribute of CommandButton and it is setting the URL value correctly, but how do I open that url as pdf in a new tab. i.e "URL of pdf should open in a new tab"
Code sample as follows:
<div class="form-options">
<h:commandButton value="Download Report" styleClass="btn btn-primary btn-icon buttonCustom" actionListener="#{reportBean.GenerateReport}">
</h:commandButton>
</div>
Hope I unterstood you right. This worked for me:
Provide the method generating the report and the URL in the actionListener-attribute of the commandButton (what you already did, according to your sample code).
Provide the method returning the URL in the action-attribute of the commandButton (it will be called after the actionListener-method).
For opening a new tab, add target="_blank in the parent <h:form> (taken from this questions answer)
XHTML-Code:
<h:form target="_blank">
<h:commandButton value="Show Report"
actionListener="#{reportBean.generateReport}"
action="#{reportBean.getUrl}" />
</h:form>
As this approach applies the target="_blank" to all non-ajax calls, here's another way using Javascript:
<h:form>
<h:commandButton value="Show Report"
actionListener="#{reportBean.generateReport}"
action="#{reportBean.getUrl}"
onclick="this.form.target='_blank'"/>
</h:form>
Java-Code:
public void generateReport(final ActionEvent evt) {
// ... generating report and setting url-variable
}
public String getUrl() {
return url;
}
If you have to forward to an external link, see this question
Use commandLink instead of commandButton..
When commandLink is used you need to provide value for target attribute as new window.
<h:commandLink target="new window">
Rest all the code will be as per your requirement.
Since the URL in which your report is present is already present in one of the variables you can use this variable from the bean in commandLink to provide the URL..
Elsewhile you can use an iframe also.It is generally used to display the report on the same page...
As I understand, your problem is not about redirecting the result of the action to a new tab, instead you want to show a report (if my guessing is right, you're using a JasperReport and want to show it in PDF format). To do this, you must download the file to the client computer and then let the default editor/program on the client handle the file.
I won't go into details for file download, BalusC provides good info about this matter:
How to stream a file download in a JSF backing bean?
How to download a file stored in a database with JSF 2.0
The relevant part to show PDF (or any other) files in browser is the ContentType of the response. This will say to the browser how the file must be threatened. Some content type values
PDF: application/pdf
Excel: application/vnd.ms-excel
Microsoft Word: application/ms-word
More: Internet media type
In case I'm wrong and you don't want to show (download) the file and need to open the file in a new Window, you have two ways (resuming both #JanisK. and #AngelsandDemons answers):
Setting the target of the form as "_blank", but this way will do that all the performed actions will open a new tab/window.
<h:form target="_blank">
<h:commandButton ... />
</h:form>
Using a commandLink instead of a commandButton, where you can set the target only for this link requests.
<h:form>
<h:commandLink target="_blank" ... />
</h:form>

Open new window by POST using h:commandButton

I need to open a JSF page in a new window by POST on click of a <h:commandButton>. I know I can acheive this using the JavaScript. But I would like to achive this using JSF and not JavaScript.
How can I achieve this? I'm using JSF 2.0.
The only non-JS way is to set target="_blank" in the parent <h:form>.
<h:form target="_blank">
...
<h:commandButton value="Open in new Window" />
</h:form>
This however affects all non-ajax(!) actions which are performed in the very same form. So if you're smart, make the action which shouldn't open in a new window an ajax action. However, ajax is also JavaScript and you mentioned that you don't want to use JS (I hope you don't get shocked once you discover that PrimeFaces is actually full of JavaScript).
If you absolutely need to restrict it to a single action, then you really can't go around asking little help to JavaScript.
<h:form>
...
<h:commandButton value="Open in new Window" onclick="this.form.target='_blank'" />
</h:form>
When you restrict the target only to a single action, maybe you want to get the form in its initial state.
With the oncklick action you set the target of the form to a _blan page.
After the click, the page is opened in a new tab/page (triggers the action event).
At last, the onblur action is triggered and set the form again to its initial state (the rest of the buttons will open in the _self page)
With this code, you can restrict to only a h:commandbutton to open in a new page.
The rest of the buttons will be opened in the self page:
<h:commandButton
onclick="this.form.target='_blank'"
onblur="this.form.target='_self'"
id="listadoRebuts"
action="#{giaquaBusquedaCorte.abrirListadoRebuts}"
value="#{msg.seqVisorbtnRecibos}">
</h:commandButton>

Problem With JSF 1.1 and PopUp

I am trying to popup a window when someone clicks a button on the data table.
<h:commandButton
action="#{cacheController.popupDetails}"
immediate="false"
onclick="popup()"
value="View Details"
styleClass="submit">
</h:commandButton>
The associated popup function is
function popup() {
window.open('RDDetails.jsf','popupWindow', 'dependent=yes, menubar=no, toolbar=no, height=500, width=400');
}
Now in the new 'RDDetails.jsf" file, I am trying to access the same managedBean cacheController. But the problem is, the pop-up window and JSF lifecycle is not in sync. As a result, the popup first displays blank and when I refresh, it pulls out the proper data.
Is there anyway I can click on a button which will do some processing in the managed bean and then opens a pop up which rerieves the processed data from the managed bean.
I am using JSF 1.1.
You're here basically firing two independent requests: one associated with the form submit and other which opens the RDDetails.jsf in a popup. You'll need to combine this in one request. You can achieve this in basically two ways:
Get rid of the onclick and just add target="_blank" to the <h:form> so that it get submitted into a new window/tab.
Block the default action by adding return false; to the onclick and do the business logic in the constructor of the bean associated with RDDetails.jsf. The only (major) caveat is here that the model won't be updated with the form fields. Thus, you'll need to pass the form fields as request parameters of the popup URL manually with help of JavaScript. You can then make use of managed property entries in the faces-config.xml to inject the GET request parameters into the model.
First way is obviously the easiest, but this doesn't give you a "fullworthy" popup/modal dialog. The second way is a bit harder (unless you've already a good grasp on both JavaScript and JSF). I would then consider to look for a component library which provides a ready-to-use popup component.
See my example:
<h:commandLink action="#{controller.myAction}" onmousedown="document.forms['idform'].target='_blank';">
I'm using jsf 1.1

Resources