Primefaces commandButton download complete hide dialog - jsf

In PrimeFaces 5.1 p:commandButton press to call download action. In action to get data from DB and create pdf. When its complete to download the pdf. My problem huge data create to taken a time that time I want to show dialog box, because user that time no for further action to done. CommandButton press I show dialog but can't know how to close dialog box.If any other way to complete action to close dialog.
stud.xhtml
<p:commandButton value="download" action="#{stud.downloadAction}" onclick="PF('progressWaitDialog').show()" ajax="false" onComplete="PF('progressWaitDialog').hide();"/>
<p:dialog id="progressWaitDialog" widgetVar="progressWaitDialog" modal="true">
<h:graphicImage value="#{stud.progressWaitBar}"/>
</p:dialog>
stud.java
public string downloadAction()
{
createPdf(studenToList);
return null;
}
My doubt commanButton click open dialog but download action complete how to hide dialog?

If you are using PrimeFaces, you can take advantage of PrimeFaces.monitorDownload and <p:fileDownload>. It is documented here.
Basically, you have to change your commandButton to use an actionListener instead an action. This actionListenerprepares an org.primefaces.model.StreamedContent to stream the file you want to download. This stream has to be wired with value attribute of <p:fileDownload>.
To prepare that StreamedContent, if your createPdf(studenToList) method returns a file, it would be as easy as following:
String mime = "application/pdf";
String name = "your-file-name.pdf";
File file = createPdf(studenToList);
StreamedContent stream = new DefaultStreamedContent(file, mime, name);
The onclick event of your commandButton will call the monitorDownload javascript of PrimeFaces onclick="PrimeFaces.monitorDownload(startFaDwn, stopFaDwn);".
Finally, you have to provide your own implementation of startFaDown and stopFaDown. This is mine:
<script type="text/javascript">
function startFaDwn() {
PF('FADownloadDlg').show();
}
function stopFaDwn() {
PF('FADownloadDlg').hide();
}
</script>
As you can see, both functions open and hide a dialog as you want. You can name these functions as you want, and obviously it can do what you want, not only open/close dialog.
You won't need onComplete event, neither ajax="false" attribute.
Hope it helps!

Related

ValueChangeEvents being fired only after other components are clicked

I'm trying to use an InputFile within JSF (1.1.7) and Apache Trinidad (1.0.11). I define a change event for it but the event is not being fired when I change the file selection but when I click on another component of the form.
Here is the jsp code:
<trh:body>
<tr:panelPage>
<tr:form usesUpload="true" id="myForm">
<tr:inputFile columns="80" id="archivo"
valueChangeListener="#{myBean.changeInputFile}"
immediate="true">
</tr:inputFile>
<tr:commandButton text="Begin"/>
</tr:form>
</tr:panelPage>
</trh:body>
Here is the relevant part of the bean:
public void changeInputFile(ValueChangeEvent event) {
UploadedFile f = (UploadedFile)event.getNewValue();
}
The code only enters into the myBean.changeInputFile method when I click the Begin button (having changed the file selection previously). I would like it to enter into myBean.changeInputFile when I change the selected file in the inputFile component.
Any idea why could be this happening?
Your expextation is wrong. The valuechangelistener is a server-side action that will fire when something is submitted to the server and effectively has a different value than it did before. It is NOT telling the component to behave like modern ajax (jsf 1.1.7 and its valuechangelistener predate the ajax era). The form value is only submitted to the server when you, well, in 'old' html terms use form submission like pressing a submit button (or use some javascript to trigger that like you would in the old plain html days). And since without pressing a button or the added javascript, nothing is submitted to the server the valuechangelistener will not spontaneously do something.
So the behaviour you see is exactly as it should be.

onclick for p:outputLabel works on loading/refreshing page but doesn't work when clicking

I have some problems with calling a bean method when clicking on Label.
When page is loading or refreshing click handler function pokus() is called, but when the label is clicked it isn't.
Part of my web page:
<h:form id="pokus">
<p:outputLabel id="pokus2" value="klikni" onclick="#pozadavkyBean.pokus(5)}"/>
</h:form>
and a method in bean:
public void pokus(int i){
System.out.printf("kliknuto sloupec:%d",i);
}
I've also tried it with:
<p:ajax event="click" listener="....
with the same result - method called on loading/refreshing but not when clicking
also tried others events: mousedown, mouseup, .... with same result
using PrimeFaces 5.0
If you will check official document of Primeface regarding Tag outputLabel.You can easily get attribute onclick used for
Client side callback to execute when component is clicked.
But here you are trying to run Managed bean method directly from onclick attribute while onclick used to call JavaScript functions.
As #Mahendran Ayyarsamy Kandiar mentioned and its simple thing outputLabel is not used to call any bean method. Its Just Simply used to show something in the page.In mean time for your requirement you can use CommandButton,CommandLink or some other component but its all depend upon your requirment etc.

Is there a bean event for cancel button in PrimeFaces upload component?

We are using the primefaces for file upload handling in our web application. The upload component becomes visible, upon some event on the page (button pressed). We would like to be able to catch the event when CANCEL button is pressed, in order to use it to hide the upload component back. Any advise? Javascript or CSS would not do the work, as we also need to do some server logic for this.
Code:
<h:form id="uploadFileForm" enctype="multipart/form-data" rendered="#{filesMenu.enableFileUpload}">
<p:fileUpload style="font-size:12px;"
fileUploadListener="#{filesMenu.handleFileUpload}"
mode="advanced" dragDropSupport="false"
update="msgUpload , :formFilesListId , :formFilesListId:dataTableSimpleFilesId, :createNewFileFromSelectionForm"
sizeLimit="100000" fileLimit="1" allowTypes="/(\.|\/)(cfg)$/" />
<p:growl id="msgUpload" showDetail="true" />
</h:form>
Catch the cancel-button click with Javascript/JQuery. On the one hand you can invoke an hidden button on your jsf page which triggers the server logic. On the other hand you can trigger the following component from Javascript :
<p:remoteCommand name="functionName" action="#{yourBean.yourMethod}">
Catch the cancel button :
$(document).ready(function() {
$(".ui-fileUpload").on("click",".cancel",function() {
//Approach remoteCommand
functionName();
//Approach hidden button
$(".jQueryTohiddenButtonClass").click();
}
Probably you have to customize the jQuery path, so they fit to your demands.
Another option is to hide the cancel-button completely with
.ui-fileupload-cancel {
display: none;
}
and create your own button instead which calls an action/actionListener and hides the dialog.
I went even further and hid all 3 buttons and created my own save button too, which called PF('fileupload_widgetvar').upload(); in oncomplete. Then you'd need to have some javascript to make the page wait until the upload is finished, as the ajax is asynchonous so you can't redirect/hide the dialog right away. Let me know if you want that code.

How to open a blank page for an external url with JSF, if target url must be initialy retained from server?

Primefaces 3.5.14, Omnifaces 1.6
Is there a component in JSF/Prime that looks like a button, but that allows to open a new blank page with an external link ? So unlike a normal link, on click the new page URL must be initialy retained from server and just then new page must be opened. Like a commanButton with an action attribute, where a string result of an action method determine the new url.
Of course I can use p:button as in Styling one JSF external link to make it look like a button but in that case I must update link generated by p:button, depending on actions made on the page every time. This is not always desirable.
You need target="_blank" on the parent form in order to submit into a new window and you need to invoke ExternalContext#redirect() (or OmniFaces' Faces#redirect()) in the action method in order to let the target window send a new GET request on the given URL. Importantly, when you use <p:commandButton> instead of <h:commandButton>, you also need to set ajax="false" because it doesn't respect target="_blank".
Thus, so:
<h:form target="_blank">
<p:commandButton value="Submit" action="#{bean.submit}" ajax="false" />
</h:form>
with
public void submit() throws IOException {
String redirectURL = determineItSomehow();
Faces.redirect(redirectURL);
}

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>

Resources