This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
How to display dialog only on complete of a successful form submit
(1 answer)
How to call JSF backing bean method only when onclick/oncomplete/on... event occurs and not on page load
(1 answer)
Closed 2 years ago.
I am trying to submit the contents of a form to a method in a managed bean, storeAppointment(), however the action attribute of the commandButton is never fired. If i use the onclick attribute, then the method in the managed bean is called, however, the onclick attribute also fires when the page is loaded which is not what I want to happen. My form is as follows :
<h:form>
<p:growl id="msgs" showDetail="true" skipDetailIfEqualsSummary="true">
<p:autoUpdate />
</p:growl>
<h:panelGrid columns="2" cellpadding="5">
<p:outputLabel for="start" value="Start Time" />
<p:datePicker id="start" value="#{createAppCtrl.startTime}" showTime="true" stepMinute="5"/>
<p:outputLabel for="finish" value="Finish Time" />
<p:datePicker id="finish" value="#{createAppCtrl.finishTime}" showTime="true" stepMinute="5"/>
<h:outputLabel for="multiple" value="Multiple:" />
<p:selectCheckboxMenu id="multiple" value="#{createAppCtrl.selectedUsers}" label="Users" multiple="true"
filter="true" filterMatchMode="startsWith" panelStyle="width:250px">
<p:ajax event="itemUnselect" listener="#{createAppCtrl.onItemUnselect}" />
<f:selectItems value="#{createAppCtrl.involvement}" />
</p:selectCheckboxMenu>
</h:panelGrid>
<p:commandButton value="Submit"
action="#{createAppCtrl.storeAppointment}"
update="displayItems"
oncomplete="PF('itemDialog').show()"/>
<p:dialog header="Selected Items" modal="true" showEffect="fade" hideEffect="fade" widgetVar="itemDialog" width="250">
<p:outputPanel id="displayItems">
<h:outputText value="Time: " />
<h:outputText value="#{createAppCtrl.startTime}" >
<f:convertDateTime pattern="MM/dd/yyyy HH:mm" />
</h:outputText>
<h:outputText value="Time: " />
<h:outputText value="#{createAppCtrl.finishTime}">
<f:convertDateTime pattern="MM/dd/yyyy HH:mm" />
</h:outputText>
<p:dataList value="#{createAppCtrl.selectedUsers}" var="city" emptyMessage="No cities selected" style="margin-bottom: 10px;">
<f:facet name="header">
Multiple
</f:facet>
#{city}
</p:dataList>
</p:outputPanel>
</p:dialog>
</h:form>
When the commandButton is clicked the update and oncomplete attributes work as expected, but the dialog that is shown by oncomplete displays only null values.
And the method that is being called by action is #{createAppCtrl.storeAppointment}:
public String storeAppointment() {
try {
System.out.print(startTime + " " + finishTime + " " + involvement);
appointmentBean.createAppointment(startTime, finishTime, involvement);
return "";
} catch (Exception e) {
System.out.print("Ctrl: " + e);
return "";
}
}
Related
This question already has an answer here:
How to show details of current row from p:dataTable in a p:dialog and update after save
(1 answer)
Closed 6 years ago.
The following code implements a datatable inside a layout, in the datatable i added an edit button in each row
<p:dataTable id="tbl" var="person" value="#{mybean.listPersons}" >
<p:column>
<f:facet name="header">
<h:outputText value="Name " />
</f:facet>
<h:outputText value="#{person.name}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Age :" />
</f:facet>
<h:outputText value="#{person.age}" />
</p:column>
<p:column>
<p:commandButton icon="ui-icon-pencil"
oncomplete="PF('dlg1').show();" action="mybean.setSelectedPerson(person)" />
</p:column>
</p:dataTable>
When i click on the edit button, the dialog box (code below) is shown but the inputs are empty, what i wanted is to show the infos of the row in the dialog bow, i'm still a beginner, i searched everywhere... but no results
<p:dialog header="Modify" widgetVar="dlg1" >
<h:form >
<p:growl id="msgs" showDetail="true" />
<h:panelGrid id="form2" value="#{myBean.person}" var="person">
<p:outputLabel value="Name :" />
<p:inputText value="#{person.name}" />
<p:outputLabel value="Age :" />
<p:inputText value="#{person.age}" />
<p:commandButton value="Submit" action="#{myBean.modifyPerson(person)}" />
</h:panelGrid>
</h:form>
</p:dialog>
#ManagedBean
#RequestScoped
public class muBean implements Serializable{
private Person selectedPerson;
//getter and setter
public void modifyPerson(Person p) {
this.selectedPerson = p;
}
}
i would be so grateful if anyone can help, i really need this
Change the command button to the following, use an actionlistener:
<p:commandButton icon="ui-icon-pencil" update=":persondlgid" oncomplete="dlg1.show();" actionListener ="mybean.findSelectedPerson">
<f:param name="personalid" value="#{person.id}" />
<p:commandButton/>
This is the dialog, add the id property to it. Then change the value of the panel grid to selectedPerson because this corresponds to the correct object in the managedbean:
<p:dialog header="Modify" widgetVar="dlg1" id="persondlgid" >
<h:form>
<p:growl id="msgs" showDetail="true" />
<h:panelGrid id="form2" value="#{myBean.selectedPerson}" var="person">
<p:outputLabel value="Name :" />
<p:inputText value="#{person.name}" />
<p:outputLabel value="Age :" />
<p:inputText value="#{person.age}" />
<p:commandButton value="Submit" action="#{myBean.modifyPerson(person)}" />
</h:panelGrid>
</h:form>
</p:dialog>
The managed bean function should look as follows. This action listener is called when the button is clicked and it then retrieves the id of the selected person and loops through the list of persons to find the one you are looking for:
public void findSelectedPerson(ActionEvent event){
if(event.getComponent().getAttributes().get("personid") != null){
Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
int personid = (params.get("personid")!= null) ? Integer.parseInt(params.get("personid")) : -1;
// Loop through the persons array
for(Person p : listPersons){
if(p.getId() == personid){
selectedPerson = p;
break;
}
}
}
This question already has an answer here:
Ajax update/render does not work on a component which has rendered attribute
(1 answer)
Closed 7 years ago.
I have an inputText and a selectOneRadio component, when form load selectOneRadio should be hidden. When user select inputText, I want to show the selectOneRadio component. I have made the selectOneRadio hidden in #PostConstruct, which should be display on select inputText.
<h:panelGrid id="panelgrid">
<p:panel id="panel" >
<h:outputLabel value="Name: " for="name" />
<p:inputText id="name" value="#{userBean.name}" immediate="true">
<p:ajax event="onselect" update="city" listener="#{userBean.showName}" />
</p:inputText>
<p:selectOneRadio id="city" value="#{userBean.city}" layout="grid" columns="3" rendered="#{userBean.displayName}" >
<f:selectItems value="#{userBean.cities}" var="c" itemLabel="#{city}" itemValue="#{city}" />
</p:selectOneRadio>
</p:panel>
</h:panelGrid>
The bean code is like:
#PostConstruct
public void init() {
displayName = false;
}
public boolean isShowName() {
return true;
}
...
But some how this is not working. I'm using JSF2.0 with primefaces 5.2.
I found the solution.
<h:form id="frm">
<h:panelGrid id="panelgrid">
<p:panel id="panel" >
<h:outputLabel value="Name: " for="name" />
<p:inputText id="name" value="#{userBean.name}" immediate="true" onkeypress="#{userBean.showName}">
<p:ajax event="change" update="frm:panelgrid" listener="#{userBean.inputcangeeListener}" />
</p:inputText>
</p:panel>
<p:panel id="p2" rendered="#{userBean.displayName}">
<p:selectOneRadio id="city" value="#{userBean.city}" layout="grid" columns="3">
<f:selectItems value="#{userBean.cities}" var="c" itemLabel="#{city}" itemValue="#{city}" />
</p:selectOneRadio>
</p:panel>
</h:panelGrid>
</h:form>
And my managed bean looks like-
#PostConstruct
public void init() {
displayName = false;
}
public boolean isShowName(){
return displayName;
}
public void inputcangeeListener(javax.faces.event.AjaxBehaviorEvent changeEvent){
setDisplayName(true);
cities = new ArrayList<>();
cities.add("pune");
cities.add("KOL");
}
Thanks for your response.
This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 7 years ago.
I want to execute a method from backing bean on onComplete attribute of p:fileUpload because I am processing some inputText values on upload action and I want those values to be added to the file in another method on the same upload click, here I don't want to use another button action.
So I am using p:remoteCommand to call the method from bean using the above javascript code. Here I'm calling javascript function on onComplete of p:fileUpload and the script calling the p:remoteCommand which in turn calling the insertProperty() method.But the insertProperty() is not getting called. How is this caused and how can I solve it? Thanks in advance.
The javascript is
<script type="text/javascript">
function addProperties(){
lazyload();
}
</script>
</h:head>
My code is
`
<h:body>
<h:form id="mainformId"
style="background: #A9CEEA !important;margin-top:5px !important;">
<div>
<h:form id="uploadformId">
<p:messages id="msg"/>
<p:remoteCommand name="lazyload" process="#this"
actionListener="#{hubDocsBean.insertProperty}" >
</p:remoteCommand>
<p:panelGrid style="width:100%;">
<p:row>
<p:column colspan="4">
<p:fileUpload fileUploadListener="#{hubDocsBean.fileUpload}"
dragDropSupport="false"
allowTypes="/(\.|\/)(txt|doc|docx|xls|xlsx|pdf)$/"
update=":mainformId:tableformId:docTableId, msg" multiple="false"
process="IagencyId,ImarketId,IvendorId,IstationId" mode="advanced" sizeLimit="52428800" oncomplete="addProperties();" />
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputText value="Agency" />
<p:spacer width="5"></p:spacer>
<p:inputText value="#{hubDocsBean.inputagency}" id="IagencyId" />
</p:column>
<p:column>
<h:outputText value="Market" />
<p:spacer width="5"></p:spacer>
<p:inputText value="#{hubDocsBean.inputmarket}" id="ImarketId" />
</p:column>
<p:column>
<h:outputText value="Vendor" />
<p:spacer width="5"></p:spacer>
<p:inputText value="#{hubDocsBean.inputvendor}" id="IvendorId" />
</p:column>
<p:column>
<h:outputText value="Station" />
<p:spacer width="5"></p:spacer>
<p:inputText value="#{hubDocsBean.inputstation}" id="IstationId" />
</p:column>
</p:row>`
By the way, you don't need to have the extra Javascript. You can just use:
oncomplete="lazyload()"
instead of
oncomplete="addProperties();"
I am using this dialog to display a Create form
<p:dialog id="dialogoGestion" header="Asignar nueva gestión" widgetVar="dlg1" >
<p:ajax event="close" update="dialogoGestion"/>
<h:form>
<h:panelGrid columns="2">
<h:outputText value="Cliente: "/>
<h:outputText value="#{miscMB.factura.cliente.nombre}"/>
<h:outputText value="Factura: "/>
<h:outputText value="#{miscMB.factura}"/>
<h:outputLabel value="#{bundle.CreateGestionLabel_descripcion}" for="descripcion" />
<p:inputText id="descripcion" value="#{miscMB.nuevaGestion.descripcion}" title="#{bundle.CreateGestionTitle_descripcion}" />
<h:outputLabel value="#{bundle.CreateGestionLabel_fechaLimiteGestion}" for="fechaLimiteGestion" />
<p:calendar locale="es" pattern="dd/MM/yyyy" id="fechaLimiteGestion" value="#{miscMB.nuevaGestion.fechaLimiteGestion}" title="#{bundle.CreateGestionTitle_fechaLimiteGestion}" required="true" requiredMessage="#{bundle.CreateGestionRequiredMessage_fechaLimiteGestion}">
</p:calendar>
<h:outputLabel value="#{bundle.CreateGestionLabel_usuario}" for="usuario" />
<p:selectOneMenu id="usuario" value="#{miscMB.nuevaGestion.usuario}" required="true" requiredMessage="#{bundle.CreateGestionRequiredMessage_usuario}">
<f:selectItems value="#{usuarioController.itemsAvailableSelectOne}"/>
</p:selectOneMenu>
</h:panelGrid>
<br />
<p:commandButton onsuccess="micalendario.update();PF('dlg1').hide();" action="#{gestionController.crearGestion()}" value="#{bundle.CreateGestionSaveLink}" >
<f:setPropertyActionListener target="#{gestionController.selected}" value="#{miscMB.nuevaGestion}"/>
<f:setPropertyActionListener target="#{gestionController.factura}" value="#{miscMB.factura}"/>
<f:setPropertyActionListener value="#{null}" target="#{miscMB.nuevaGestion}"/>
<f:actionListener binding="#{miscMB.anularNuevaGestion()}"/>
<f:actionListener binding="#{miscMB.agregarGestion(gestionController.selected)}"/>
</p:commandButton>
</h:form>
</p:dialog>
This is where I call the dialog to display
<p:commandLink value="Asignar gestión" onclick="PF('dlg1').show();return false;" action="#{gestionController.preparar()}" rendered="#{miscMB.factura!=null}">
<f:setPropertyActionListener target="#{gestionController.factura}" value="#{miscMB.factura}"/>
</p:commandLink>
What happens is that both <h:outputText value="#{miscMB.factura.cliente.nombre}"/> and <h:outputText value="#{miscMB.factura}"/> values display the properties' last state.
For example, the first time an item selected and you call the dialog, the output is none, because the state is not set. Then, when you select another item and call the dialog, the output is the state last set, which is the previous item value.
Apparently, the setPropertyActionListener in the commandLink is called after the dialog is closed or it executes an action, but not when the commandLink is clicked.
So, how can I set the properties before the dialog frame is displayed?
Suppose having a p:dialog with an <o:validateOne> on submit button where p:dialog will close after that the dialog closes even if the validation is still required
<h:form>
<h:outputText id="newValueId" value="#{myBean.newValue}/>
<p:dialog id="dialog" widgetVar="dlg" resizable="false" dynamic="true"
appendToBody="false" modal="true">
<o:validateOne id="one" components="name1 name2"
message="one is required"/>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel value="name 1 :" />
<p:inputText value="#{myBean.name1}" id="name1"/>
<h:outputLabel fvalue="name 2 :" />
<p:inputText value="#{myBean.name2}" id="name2"/>
<p:commandLink id="okId" value="ok" update="growl newValueId"
action="#{myBean.updateMyForm}"
process="dialog"
oncomplete="if(!args.validationFailed())dlg.hide();"/>
</h:panelGrid>
</p:dialog>
class Mybean{
String name1,name2,newValue;
public void updateMyForm(){
newValue=name1 + " " + name2;
}
//getter and setters
}
I want the dialog to not close if the two inputs input1 and input2 are still empty
But what what's happening is that dialog is closed and message is the displayed in the growl