Function with param not called in PrimeFaces [duplicate] - jsf

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

Related

P:CommandButton in popup dialog did not submit

I have encountered a strange situation.
I have a xhtml looks like this:
<h:form id="frmContent">
<p:dataTable id="tbl" ..>
</p:dataTable>
<p:contextMenu for="tbl">
<p:menuitem value="Delete" update="tbl" actionListener="#{VehicleBean.deleteVehicle}"/>
<p:menuitem value="Copy" update=":frmContent:vehicleDia:VehicleDialogContent" action="#{VehicleBean.copyVehicle}" oncomplete="PF('VehicleDialog').show();"/>
</p:contextMenu>
<p:dialog header="Vehicle Detail" widgetVar="VehicleDialog" modal="true" resizable="true" appendTo="#(body)">
<h:form id="vehicleDia">
<p:panelGrid id="VehicleDialogContent" columns="2" >
...
</p:panelGrid>
<p:commandButton id="SaveButton" type="button" value="Save" update="VehicleDialogContent" actionListener="#{VehicleBean.saveVehicle}" oncomplete="PF('VehicleDialog').hide();">
</h:form>
</p:dialog>
The problem is, I cannot make the VehicleBean.saveVehicle() method fired.
I have tried to remove the nested form "vehicleDia" but not work.
I have tried to changing actionListener to action but still not work.
I have tired using h:commandButton instead of p:commandButton still no lucky.
Right now, I dont have any clue.
PS: this is my bean:
#ManagedBean(name="VehicleBean")
#ViewScoped
public class VehicleBean {
...
public void saveVehicle() {
System.out.println("VehicleBean:saveVehcile is called");
}
}
I am new to JSF and primefaces, any help is much appreciated!!

Primefaces open confirm dialog if the form is not fully filled

Hello I'm trying to solve this problem. Hope you can help me.
I have a question form with optionals question (textfields, checkbox, radiobuttons). I'm trying to open a confirm dialog ("There are some question not answered, are you sure you want to continue? YES/NO") just if there are question unfilled. I can OPEN de dialog to ask for confirmation. But I cant validate if the dialog has to be displayed or it have to be skipped.
<h:form>
<div class="col-xs-12 ctg-home-button center">
<p:commandButton
id="cancel-button"
actionListener="#{answerSurveyView.restart}"
immediate="true"
styleClass="btn btn-default"
title="#{propertiesBean.getProperty('answer.cancel')}"
value="#{propertiesBean.getProperty('answer.cancel')}"
/>
<h:outputText value=" " />
<p:commandButton
id="answer-button"
action="#{answerSurveyView.saveAnswers}"
styleClass="btn btn-default"
title="#{propertiesBean.getProperty('answer.send')}"
update="answer-form"
value="#{propertiesBean.getProperty('answer.send')}"
>
<p:confirm header="Confirmation" message="#{propertiesBean.getProperty('answer.survey-confirmation')}" icon="ui-icon-alert" />
</p:commandButton>
</div>
<p:confirmDialog global="true" showEffect="fade" hideEffect="fade">
<p:commandButton value="#{propertiesBean.getProperty('answer.send')}" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="#{propertiesBean.getProperty('answer.close')}" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>
</h:form>
Use the rendered attribute of your confirmDialog to indicate whether you want render it or not. Set to the rendered attribute a value returned from your view, indicating whether there are unasnwered questions or not.
Reload your confirmDialog each time the user asnwers a question by using an ajax on event, calling a method on your view to recalculate the unansweredQuestions attribute; and the update attribute with your confirmDialog id as value, so you can set rendered to false once the last question is ansewered.
For instance, if you were using a dropdown to give options for the answer, the code of the page might look like this:
<div class="col-xs-12 ctg-home-button center">
<p:commandButton ....
.....
</div>
<p:selectOneMenu value="#{answerSurveyView.firstQuestionAnswer}">
<p:ajax listener="#{dropdownView.onAnswerSelect}" update="myConfirm" />
<f:selectItem itemLabel="Select option" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{answerSurveyView.firstQuestionAnswerOptions}" />
</p:selectOneMenu>
<p:confirmDialog id='myConfirm' rendered="#{answerSurveyView.unansweredQuestions}" global="true" showEffect="fade" hideEffect="fade">
<p:commandButton value="#{propertiesBean.getProperty('answer.send')}" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="#{propertiesBean.getProperty('answer.close')}" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>
And the code in your view:
// declare unansweredQuestions attribute and set it to false
private boolean unansweredQuestions = false;
// unansweredQuestions getter
public boolean getUnansweredQuestions() {
return unansweredQuestions;
}
// call this method every time a question is answered
// to update the unansweredQuestions value
public void onAnswerSelect() {
unansweredQuestions = foo(); //method returns true if there are questions left and false if not.
}

Create edit form for Primefaces table

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]

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