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>
Related
I want to create edit form for Primefaces table. I tested this code:
<h:form id="form">
<p:dataTable id="tbl" var="systemuser" value="#{systemusers.systemUsers}"
selectionMode="single" selection="#{systemusers.selectedSystemUser}" rowKey="#{systemuser.username}" lazy="true"
resizableColumns="true"
>
<p:ajax event="rowSelect" listener="#{systemusers.onRowSelect}" update=":form:carDetail" oncomplete="PF('carDialog').show()" />
....................
</p:dataTable>
<p:dialog id="wd" header="System User Details" widgetVar="carDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="true">
<p:outputPanel id="carDetail" style="text-align:center;">
<p:panelGrid columns="2" columnClasses="label,value">
<h:outputLabel for="id" value="ID" />
<p:outputLabel id="id" value="#{systemusers.selectedSystemUser.id}" rendered="#{not systemusers.editable}"/>
<h:inputText value="#{systemusers.selectedSystemUser.id}" rendered="#{systemusers.editable}" />
........
</p:panelGrid>
</p:outputPanel>
<f:facet name="footer">
<p:commandButton value="Edit System User" rendered="#{not systemusers.editable}"
actionListener="#{systemusers.editRecord(false)}">
<f:ajax render=":form:wd" execute=":form:wd"></f:ajax>
</p:commandButton>
<p:commandButton value="Cancel" rendered="#{systemusers.editable}" actionListener="#{systemusers.editRecord(true)}">
<f:ajax render=":form" execute=":form"></f:ajax>
</p:commandButton>
</f:facet>
</p:dialog>
<p:contextMenu for="tbl">
<p:menuitem value="View" update="carDetail" icon="fa fa-search" oncomplete="PF('carDialog').show()"/>
<p:menuitem value="Delete" update="tbl" icon="fa fa-remove" actionListener="#{systemusers.deleteSystemUser}">
<p:confirm header="Confirmation" message="Are you sure?" icon="fa fa-warning" />
</p:menuitem>
</p:contextMenu>
<p:confirmDialog global="true" showEffect="fade" hideEffect="fade">
<p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="fa fa-check" />
<p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="fa fa-remove" />
</p:confirmDialog>
</h:form>
Managed bean:
#Named
#RequestScoped
public class Systemusers implements Serializable
{
.......
public void editRecord(boolean editable)
{
SessionFactory factory = HibernateUtils.getSessionFactory();
Session session = factory.getCurrentSession();
try
{
session.getTransaction().begin();
SystemUsersModel obj = new SystemUsersModel();
obj.setId(selectedSystemUser.getId());
..........
session.update(obj);
session.getTransaction().commit();
this.editable = editable;
}
catch (Exception e)
{
e.printStackTrace();
session.getTransaction().rollback();
}
}
......
private boolean editable = false;
public boolean isEditable()
{
return editable;
}
public void setEditable(boolean editable)
{
this.editable = editable;
}
}
When I open the Edit form and I click button Edit System User the form disappears. Probably because I render and execute it. How I can execute the form and under it without closing it?
The example code is bad in multiple ways. Two things stand out:
It is not an [mcve]
<f:ajax render=":form" execute=":form"> is superfluous or might even cause the weird behaviour
OP most likely does not know that (quote from the PrimeFaces showcase , emphasis mine)
CommandButton
CommandButton extends the standard h:commandButton with ajax, partial processing and skinning features.
So the commandBotton is already ajax anabled and does not need to hava a
<f:ajax render=":form" execute=":form">
inside it. This (most likely) makes an the update run twice. One #none (Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes) and one updating the form. The second one most likely does not have any content. But trying (and debugging) will make things clear. If this is not the answer, the reason cannot be seen in the code and hence it should have been a [mcve]
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>
In Primefaces, a button will call a prepareCreate method and then open the form dialog. The issue is, this method never get called so that the form in the dialog is not shown.
Command button
<p:commandButton id="createButton1" value="#{bundle.Create}" actionListener="#{purchaseOrderController.prepareCreate}"
update=":PurchaseOrderCreateForm11" oncomplete="PF('PurchaseOrderCreateDialog11').show()"/>
Dialog
<p:dialog id="PurchaseOrderCreateDlg11" widgetVar="PurchaseOrderCreateDialog11" modal="true" resizable="false" appendTo="#(body)" header="#{bundle.CreatePurchaseOrderTitle}">
<h:form id="PurchaseOrderCreateForm11">
<h:panelGroup id="display">
<p:panelGrid columns="2" rendered="#{purchaseOrderController.selected != null}">
<p:outputLabel value="#{bundle.CreatePurchaseOrderLabel_orderNum}" for="orderNum" />
<p:inputText id="orderNum" value="#{purchaseOrderController.selected.orderNum}" title="#{bundle.CreatePurchaseOrderTitle_orderNum}" required="true" requiredMessage="#{bundle.CreatePurchaseOrderRequiredMessage_orderNum}"/>
<p:outputLabel value="#{bundle.CreatePurchaseOrderLabel_quantity}" for="quantity" />
<p:inputText id="quantity" value="#{purchaseOrderController.selected.quantity}" title="#{bundle.CreatePurchaseOrderTitle_quantity}" />
<p:outputLabel value="#{bundle.CreatePurchaseOrderLabel_shippingCost}" for="shippingCost" />
<p:inputText id="shippingCost" value="#{purchaseOrderController.selected.shippingCost}" title="#{bundle.CreatePurchaseOrderTitle_shippingCost}" />
</p:panelGrid>
<p:commandButton actionListener="#{purchaseOrderController.create}" value="#{bundle.Save}" oncomplete="handleSubmit(args,'PurchaseOrderCreateDialog11');"/>
<p:commandButton value="#{bundle.Cancel}" onclick="PurchaseOrderCreateDialog11.hide()"/>
</h:panelGroup>
</h:form>
</p:dialog>
JSF managed bean
#Named("purchaseOrderController")
#SessionScoped
public class PurchaseOrderController implements Serializable {
public PurchaseOrder prepareCreate() {
System.out.println("Purchase order prepare create");
selected = new PurchaseOrder();
initializeEmbeddableKey();
return selected;
}
}
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.
I am trying to have a button that sets edit mode and refreshes the form, but by clicking the button, nothing happens.
This is my code:
<h:form>
<h:panelGrid columns="2" cellpadding="5">
<p:commandButton type="submit" value="Edit Your Records" icon="ui-icon-edit"
update="#form" rendered="#{!bean.editMode}">
<f:setPropertyActionListener value="true" target="#{bean.editMode}"/>
</p:commandButton>
<p:commandButton type="submit" value="Exit Edit Mode" icon="ui-icon-back"
update="#form" rendered="#{bean.editMode}">
<f:setPropertyActionListener value="false" target="#{bean.editMode}"/>
</p:commandButton>
</h:panelGrid>
<p:dataTable id="table" value="#{bean.table}" var="apartment">
...
</p:dataTable>
</h:form>
Thanks!
I would suggest using
<h:form>
<p:commandButton value="Edit Your Records" update="#form" rendered="#{!bean.editMode}" action="#{bean.toggleEditMode()}" />
<p:commandButton value="Exit Edit Mode" update="#form" rendered="#{bean.editMode}" action="#{bean.toggleEditMode()}" />
<h:outputText value="#{bean.editMode}" />
and
public void toggleEditMode() {
this.editMode = !this.editMode;
}
ok, it seams that the function called in the 'action' attribute needs to return a String.
The following worked:
public String toggleEditMode() {
this.editMode = !this.editMode;
return "#";
}
Thanks!