Unable to download the zip file after clicking confirmDialog ok button - jsf

I am unable to download the zip file.
After clicking ok button in confirmDialog I am unable to download a zip file.
If I am not using confirmDialog I can able to download the file.
Could any one help me on this,
Here I added the code for the reference.
<h:form>
<p:panelGrid rendered="true">
<p:commandButton style="HEIGHT: 24px; WIDTH: 300px" id="AjaxFalse"
value="AjaxFalse"
action="#{testDownload.getZipFile}"
title="AjaxFalse" rendered="true">
<p:confirm message="Are you sure?"
header="Confirmation"
icon="ui-icon-alert" />
</p:commandButton>
</p:panelGrid>
<p:confirmDialog global="true" showEffect="fade" hideEffect="fade" width="500">
<h:panelGroup layout="block" style="text-align: center">
<p:commandButton value="Ok" type="submit" styleClass="ui-confirmdialog-yes" style="width:70px;"/>
<p:commandButton value="Cancel" type="button" styleClass="ui-confirmdialog-no" style="width:70px;"/>
</h:panelGroup>
</p:confirmDialog>
</h:form>
My backingbean is
public class TestDownload
{
//some code here
//Downloading the zip file in jsf2.x
public String getZipFile()
{
try{
//enter code here
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.setResponseHeader("Content-Disposition", "attachment;filename=\"" + Test + ".zip\"");
ec.setResponseCharacterEncoding("UTF-8");
ec.setResponseContentType("application/zip");
//This method will generate the required file.
new BulkloadImporter().generateIntegrationXSDs(getId(), ec.getResponseOutputStream());
fc.responseComplete();
return "Save";
}
catch(Exception e)
{
//Handling exception
}
}
//some code here
}

This issue is not 'download' related. Up to now (PrimeFaces 5.3/5.3.2) using a <p:confirm...> global dialog in combination with ajax="false" does not work. The server side action in general will not be called.
There are two possible solutions:
Don't use global mode and do the download on the button in the confirmDialog
Use the workaround mentioned in the issue (not tested this myself)
An example of non-global usage can be found in the PrimeFaces Documentation (adapted to this case):
Getting started with ConfirmDialog ConfirmDialog has two modes; global and non-global. Non-Global mode is almost same as the dialog
component used with a simple client side api, show() and hide().
<h:form>
<p:commandButton type="button" onclick="PF('cd').show()" />
<p:confirmDialog message="Are you sure about downloading this file?"
header="Initiating download" severity="alert"
widgetVar="cd">
<p:commandButton value="Yes Sure" action="#{testDownload.getZipFile}" ajax="false"
update="messages" oncomplete="PF('cd').hide()"/>
<p:commandButton value="Not Yet" onclick="PF('cd').hide();" type="button" />
</p:confirmDialog>
</h:form>

Related

How to upload file in jsf using primefaces <p:confirm/>

Hi am trying to upload a file using primefaces which works fine when I don't have confirm dialog in the submit button. but when I add the the managed bean is not even called.
Here is my xhtml code:
<p:panel header="New Business Registration" style=" min-height: 400px">
<h:form id="upload" enctype="multipart/form-data">
<p:messages closable="true" />
<p:panelGrid columns="2">
<p:outputLabel value="Select Image"/>
<p:fileUpload mode="simple" value="#{fileUploadMBean.uploadedFile}"/>
p:outputLabel value=""/>
<p:commandButton update="upload" ajax="false" action="#{fileUploadMBean.uploadFile}" icon="ui-icon-person" value="Register New Client">
<p:confirm header="Confirm" message="Are you sure you wish to upload this file" />
</p:commandButton>
</p:panelGrid>
</h:form>
<p:confirmDialog global="true" showEffect="fade" hideEffect="fade">
<p:commandButton value="Yes" style="color: #fff;
background-color: #5cb85c;
border-color: #4cae4c;" type="button" styleClass="ui-confirmdialog-yes" icon=" ui-icon-check" />
<p:commandButton value="No" style="background-color: #d9534f; border-color: #d43f3a;" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-closethick" />
</p:confirmDialog>
</p:panel>
And my managed bean:
#ManagedBean
#ViewScoped
public class FileUploadMBean implements Serializable {
private UploadedFile uploadedFile;
public String uploadFile() throws IOException {
System.out.println("hello:"); //method not called when i use confirm dialog but when i dont use file upload is successfull
FacesContext context = FacesContext.getCurrentInstance();
if(uploadedFile==null)
{
context.addMessnage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Blank File detected", "image"));
return "";
}
//jsf file upload here
{}//upload code here which works fine without p:confirm
return null;
}
When I use
<p:commandButton update="upload" action="#{fileUploadMBean.uploadFile}" icon="ui-icon-person" value="Register New Client">
</p:commandButton>
The uploaded file is null;
When I disable Ajax and without like
<p:commandButton update="upload" action="#{fileUploadMBean.uploadFile}" icon="ui-icon-person" ajax="false" value="Register New Client">
</p:commandButton>
Upload is Successfull.
When I now add when ajax is disabled in the submit button the managed method is not getting invoked at all
<p:commandButton update="upload" action="#{fileUploadMBean.uploadFile}" icon="ui-icon-person" ajax="false" value="Register New Client">
<p:confirm header="Confirm" message="Are you sure you wish to upload this file" />
</p:commandButton>

Function with param not called in PrimeFaces [duplicate]

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 6 years ago.
I have a form with serveral Dialog, one of them call a function with parameter, but I don't know why is not working. Other dialog are working perfectly. The dialog with parameters:
<p:dialog widgetVar="windowsConfirmOperation" position="center middle" resizable="false"
header="¡Warning!" closable="false" showEffect="fade" hideEffect="fade"
id="idWindowsConfirmOperation" modal="true">
<p:panel>
<div class="DispBlock Responsive100">
<p:outputLabel value="Are you sure?"
escape="false"/>
</div>
</p:panel>
<div class="DispBlock ui-contenedor-botones-accion Fright">
<p:commandButton value="YES"
oncomplete="PF('windowsConfirmOperation').hide();"
update="#form" process="#form"
action="#{queryView.confirmOperation('true')}">
</p:commandButton>
<p:commandButton value="NO"
oncomplete="PF('windowsConfirmOperation').hide();"
update="#form" process="#form"
action="#{queryView.confirmOperation('false')}">
</p:commandButton>
</div>
</p:dialog>
And my view class called QueryView:
#SessionScoped
#ManagedBean
public class QueryView {
....
public void confirmOperation(String confirm) {
if ("true".equals(confirm)) {
doSomeThing();
}
}
}
I debug but the dialog never call the function. I changed parameters of Boolean to String but not working. What am I doing wrong?
Regards.
Finally I modified my logic and now the code works.
<p:dialog widgetVar="windowsConfirmOperation" position="center middle" resizable="false"
header="¡Warning!" closable="false" showEffect="fade" hideEffect="fade"
id="idWindowsConfirmOperation" modal="true">
<p:panel>
<div class="DispBlock Responsive100">
<p:outputLabel value="Are you sure?"
escape="false"/>
</div>
</p:panel>
<div class="DispBlock ui-contenedor-botones-accion Fright">
<p:commandButton value="YES"
oncomplete="PF('windowsConfirmOperation').hide();" process="#this"
action="#{queryView.confirmOperation}">
</p:commandButton>
<p:commandButton value="NO"
onclick="PF('windowsConfirmOperation').hide();"
>
</p:commandButton>
</div>
</p:dialog>
I have changed the param process="#form" for process="#this". I only process the button not all form.
How NO button only close the dialog I changed it. Now only use onclick function.
How only the YES button use the function, now I don't need parameters. So my function changed to:
public void confirmOperation() {
doSomeThing();
}
Edit:
If I pass a Boolean works too.
action="#{queryView.confirmOperation('true'}">
and java file
public void confirmOperation(Boolean something) {
doSomeThing();
}
With these changes my code works fine.
Regards

How to show messages in p:dialog?

I need your help in showing an error message in the dialog. By clicking on the commandButton, no message is shown in the dialog.
Even though I tried to show the message in a dialog, but nothing is shown without any error.
So how can I produce messages in a dialog and not in the main form
Here is the JSF page code:
<h:form id="Requests">
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true"/>
<p:dialog id="c1" header="C1" widgetVar="c1">
<p:message id="messagePDFSTAR"
for=":Requests:DownloadPDFSTAR"
showDetail="true" />
<p:commandButton id="DownloadPDFSTAR"
value="Download"
ajax="false"
actionListener="#{hrd.PDFSTAR}"
update=":Requests:messagePDFSTAR" >
<p:fileDownload value="#{hrd.fileSTAR}" />
</p:commandButton>
</p:dialog>
</h:form>
Here is the java bean code:
public void PDFSTAR() {
try {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Ref is Null", "Ref is Null");
RequestContext.getCurrentInstance().showMessageInDialog(message);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Fatal!", "System Error"));
} catch (Exception e) {
e.printStackTrace();
}
}
It's because you have ajax=false at the commandButton, so, the attribute update::Requests:messagePDFSTAR isn't going to work.
You can't combine ajax=false and update="...", update attribute is only for ajax actions.
I understand you need the ajax=false because the p:fileDownload works with that, but maybe you can try another way to display the message you want.
I deal with that situation once and use a workaround, in p:dialog you can use a p:messages and autoUpdate=true.
<h:form id="Requests">
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true"/>
<p:dialog id="c1" header="C1" widgetVar="c1">
<p:messages id="messagesPDFSTAR"
autoUpdate="true"
showDetail="true" />
<p:commandButton id="DownloadPDFSTAR"
value="Download"
ajax="false"
actionListener="#{hrd.PDFSTAR}" >
<p:fileDownload value="#{hrd.fileSTAR}" />
</p:commandButton>
</p:dialog>
</h:form>
I hope that will help you.
Edit:
RequestContext.getCurrentInstance().showMessageInDialog(message);
It shows the message in a new dialog, but i think it's not what you want. You want to show the message in the same dialog of the commandButton and the fileDownLoad.
Ajax and update not working correctly
To update components as p:growl or p:messages after a download, or even at the start just use a p:remoteCommand and the PrimeFaces.monitorDownload(start, stop); as in the example provided here PrimeFaces File Download example
here is how I'd do it:
<script type="text/javascript">
function start() {
PF('statusDialog').show();
//maybe some other blah blah..
updateMyMessages();
}
function stop() {
PF('statusDialog').hide();
//maybe some other blah blah..
updateMyMessages();
}
</script>
<div class="card">
<p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false" closable="false"
resizable="false">
<i class="pi pi-spinner pi-spin" style="font-size:3rem"></i>
</p:dialog>
<h:form>
<p:messages id="messagesPDFSTAR"/>
<p:remoteCommand name="updateMyMessages" update="messagesPDFSTAR" action="#{hrd.PDFSTAR}"/>
<p:commandButton value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop);"
icon="pi pi-arrow-down" styleClass="p-mr-2">
<p:fileDownload value="#{fileDownloadView.file}"/>
</p:commandButton>
</div>
Please note that I called the remote command twice so you get two updates to your messages, also this will execute your action #{hrd.PDFSTAR} twice, once on download start and the other on download finish, so modify as you see fit.
You can always upgrade to PrimeFaces 10+ and use ajax="true" (the default) on the download command button.

Button in dialog inside p:dialog is not calling controller method

I've got a problem as described in the title.
Small description of the problem is as following:
I have button which is used to open dialog. Then, inside that dialog, there is button which opens another dialog on top of the first one. After clicking second button I want method from controller to be called but nothing happens. Value in h:outputText is read properly, so I guess it is not a problem with connection controller->view.
I'm using:
Spring web 3.1.2.RELEASE
JSF 2.2.10
Primefaces 5.1
Code:
beans.xml
<bean id="testController" class="test.TestController" />
TestController.java
public class TestController implements Serializable
{
private static final long serialVersionUID = 7028608421091861830L;
private String test;
public TestController()
{
test = "abc";
}
public void testMethod()
{
test = "cba";
}
public String getTest()
{
return test;
}
}
test.xhtml
<h:panelGrid columns="1" cellpadding="5">
<p:commandButton value="Basic" type="button" onclick="PF('dlg1').show();" />
</h:panelGrid>
<p:dialog widgetVar="dlg1">
<h:outputText value="Resistance to PrimeFaces is futile!" />
<h:panelGrid columns="1" cellpadding="5">
<p:commandButton value="Basic" type="button" onclick="PF('dlg2').show();" />
</h:panelGrid>
<p:dialog widgetVar="dlg2">
<h:outputText value="#{testController.test}" />
<p:commandButton value="Call method" type="button" actionListener="#{testController.testMethod}" />
</p:dialog>
</p:dialog>
What I tried:
adding appendToBody="true" to each p:dialog
changing from p:commandButton to p:button
changing from actionListener to action
but nothing helps.
I would be grateful for any help or advice of what can be the reason of not calling given method.
There are 3 problems.
You're nesting <p:dialog> components. This doesn't make sense. Separate them.
A <p:dialog> must have its own <h:form>, particularly when you explicitly use appendToBody="true" or appendTo="#(body)", otherwise nothing can be submitted because JavaScript would relocate the dialog out of its position in the HTML DOM tree to the end of body, causing it to not be sitting in a form anymore.
A <p:commandButton type="button"> acts as a "click" button, not as a submit button. Remove that attribute from submit buttons.
All in all, this is how it should look like:
<h:form>
<h:panelGrid columns="1" cellpadding="5">
<p:commandButton value="Basic" type="button" onclick="PF('dlg1').show();" />
</h:panelGrid>
</h:form>
<p:dialog widgetVar="dlg1">
<h:form>
<h:outputText value="Resistance to PrimeFaces is futile!" />
<h:panelGrid columns="1" cellpadding="5">
<p:commandButton value="Basic" type="button" onclick="PF('dlg2').show();" />
</h:panelGrid>
</h:form>
</p:dialog>
<p:dialog widgetVar="dlg2">
<h:form>
<h:outputText value="#{testController.test}" />
<p:commandButton value="Call method" actionListener="#{testController.testMethod}" />
</h:form>
</p:dialog>
OK. I guess I found a way to fix this problem.
It seems that the problem was:
type="button"
I deleted it from the list of attributes of each button and now it works even without h:form. Thanks for help.

Primefaces Confirm Message Updated Value

I'm trying to show an updated value in confirm dialog message but I keep getting the old value as in this scenario
<h:form>
<p:inputText value="#{bean.object.amount}"/>
<p:commandButton value="CALCULATE" update="cal" actionListener="#{bean.calculate()}"/>
<h:panelGroup id="cal">
<h:outputText value="#{bean.object.amount}"/>
<p:commandButton value="SUBMIT" actionListener="#{bean.submit()}">
<p:confirm header="Confirmation" message="Amount is : #{bean.object.amount} ?"/>
</p:commandButton>
<p:confirmDialog global="true">
<p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>
</h:panelgGroup/>
</h:form>
Bean code:
#ManagedBean(name="bean")
#ViewScoped
public class Bean implements Serializable {
private SomeClass object;
#PostConstruct
public void init(){
this.object = new SomeClass();
}
public void calculate(){
//do some colculation (not related to amount field in object)
}
public void submit(){
//submit to database
}
//getter setter
}
When I enter a value in amount, lets say 50. and update the cal component I get the updated amount in the outputtext "50". However, in the confirm button message I get amount as 0 instead 50. How can I show the updated value in the confirm message?
PS:
Primefaces-4.0
Take a look at the user guide of primefaces in the confirm dialog section, in the Non-Global mode the document mentioned:
Message facet is
useful if you need to place custom content instead of simple text.
While in the Global mode, I can't find similar sentences like that, and I've tried using the facet in Global mode and it doesn't work.So,
Do you really use this confirm dialog multiple times?
If not:
I suggest you take away the global parameter and change your code like this:
<h:form>
<p:inputText value="#{bean.object.amount}"/>
<p:commandButton value="CALCULATE" update="cal" actionListener="#{bean.calculate()}"/>
<h:panelGroup id="cal">
<h:outputText value="#{bean.object.amount}"/>
<p:commandButton value="SUBMIT" actionListener="#{bean.submit()}" oncomplete="PF('confirmDlg').show()"/>
<p:confirmDialog header="Confirmation" widgetVar="confirmDlg">
<f:facet name="message">
<h:outputText value='Amount is : #{bean.object.amount} ?'/>
</f:facet>
<p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>
</h:panelGroup>
</h:form>
.
If so:
(You do use the confirm dialog multiple times and tired of writing several dialogs with same form but different message.)
I suggest you write a dialog on your own,and you can also change the message in the dialog from backing bean like what you did:
<h:form id="myForm">
<p:inputText value="#{bean.object.amount}"/>
<p:commandButton value="CALCULATE" update="cal" actionListener="#{bean.calculate()}"/>
<h:panelGroup id="cal">
<h:outputText value="#{bean.object.amount}"/>
<ui:param name="message" value="Amount is :#{bean.object.amount}?" />
<p:commandButton value="SUBMIT" actionListener="#{bean.setMessage(message)}" action="#{bean.submit()}" update="myForm:myDialog" oncomplete="PF('myDlg').show()"/>
</h:panelGroup>
<p:dialog id='myDialog' widgetVar="myDlg" header="Confirmation" modal="true" resizable="false">
<h:panelGrid columns="3">
<h:panelGroup styleClass="ui-icon ui-icon-alert" style="float:right"/>
<h:outputText value="#{bean.message}"/>
<h:outputText/>
<h:outputText/>
<p:commandButton value="Yes" type="button" icon="ui-icon-check" oncomplete="PF('myDlg').hide()"/>
<p:commandButton value="No" type="button" icon="ui-icon-close" onclick="PF('myDlg').hide()"/>
</h:panelGrid>
</p:dialog>
</h:form>
p:confirm does not implement state saving, thus it loses its attributes' values after the first JSF lifecycle. It also evaluates EL only once at view build time.
I posted the solution in this answer.

Resources