I have a problem with rich dataTable
actually I populate that dataTable from a database and a made a column editable by putting an inplaceInput in it the problem is when i submit to the backing bean The values doesn't change the backing bean is populated with old values that initially populated the dataTable :
here is my JSF Code of the table:
<center>
<fieldset><rich:dataTable id="tablefiles"
value="#{fileBean.files}" var="file" rows="8"
iterationStatusVar="it">
<rich:column id="Child_Code">
<f:facet name="header">
<h:outputText value="N°tiers filiale" />
</f:facet>
<h:outputText value="#{file.child_Code}" />
</rich:column>
<rich:column id="Mother_Code">
<f:facet name="header">
<h:outputText value="Nom du groupe" />
</f:facet>
<!-- <h:inputText value="#{file.mother_Code}" />-->
<rich:inplaceInput defaultLabel="Click here to edit"
showControls="false" controlsHorizontalPosition="left"
controlsVerticalPosition="bottom" id="inplaceInput"
value="#{file.mother_Code}"
>
</rich:inplaceInput>
</rich:column>
<rich:column>
<f:facet name="header" roles="#riskuser">
<h:outputText value="Détail" />
</f:facet>
<h:commandLink action="#{fileBean.accesDetail}">
<h:graphicImage value="/images/edit.png" alt="edit"
style="border:0;" title="Détail " />
<f:setPropertyActionListener target="#{fileBean.selectedFile}"
value="#{file}" />
</h:commandLink>
</rich:column>
<f:facet name="footer">
<rich:dataScroller page="#{fileBean.page}" />
</f:facet>
</rich:dataTable> <br />
<h:panelGrid columns="3" id="buttonRow">
<a4j:commandButton value="Save" render="tablefiles" execute="#form"
action="#{fileBean.update}">
</a4j:commandButton>
</h:panelGrid></fieldset>
</center>`<center>
<fieldset><rich:dataTable id="tablefiles"
value="#{fileBean.files}" var="file" rows="8"
iterationStatusVar="it">
<rich:column id="Child_Code">
<f:facet name="header">
<h:outputText value="N°tiers filiale" />
</f:facet>
<h:outputText value="#{file.child_Code}" />
</rich:column>
<rich:column id="Mother_Code">
<f:facet name="header">
<h:outputText value="Nom du groupe" />
</f:facet>
<!-- <h:inputText value="#{file.mother_Code}" />-->
<rich:inplaceInput defaultLabel="Click here to edit"
showControls="false" controlsHorizontalPosition="left"
controlsVerticalPosition="bottom" id="inplaceInput"
value="#{file.mother_Code}"
>
</rich:inplaceInput>
</rich:column>
<rich:column>
<f:facet name="header" roles="#riskuser">
<h:outputText value="Détail" />
</f:facet>
<h:commandLink action="#{fileBean.accesDetail}">
<h:graphicImage value="/images/edit.png" alt="edit"
style="border:0;" title="Détail " />
<f:setPropertyActionListener target="#{fileBean.selectedFile}"
value="#{file}" />
</h:commandLink>
</rich:column>
<f:facet name="footer">
<rich:dataScroller page="#{fileBean.page}" />
</f:facet>
</rich:dataTable> <br />
<h:panelGrid columns="3" id="buttonRow">
<a4j:commandButton value="Save" render="tablefiles" execute="#form"
action="#{fileBean.update}">
</a4j:commandButton>
</h:panelGrid></fieldset>
</center>
Here is My bean code :
package ui.bean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import business.ISRGGestionGarantieCasa;
import common.pojo.SRGarantieCasa;
#ManagedBean
#SessionScoped
public class GarantiCasaBean implements Serializable {
private static final long serialVersionUID = -1509108399715814312L;
private ISRGGestionGarantieCasa fileService;
private int idMsg;
private int page = 1;
private String id_r;
private List<SRGarantieCasa> files = new ArrayList<SRGarantieCasa>();
private SRGarantieCasa selectedFile, file;
private List<String> suggestions = null;
public List<String> getSuggestions() {
return suggestions;
}
public void setSuggestions(List<String> suggestions) {
this.suggestions = suggestions;
}
public SRGarantieCasa getSelectedFile() {
return selectedFile;
}
public void setSelectedFile(SRGarantieCasa selectedfile) {
this.selectedFile = selectedfile;
}
public ISRGGestionGarantieCasa getFileService() {
return fileService;
}
public void setFileService(ISRGGestionGarantieCasa fileService) {
this.fileService = fileService;
}
public int getIdMsg() {
return idMsg;
}
public void setIdMsg(int idMsg) {
this.idMsg = idMsg;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public String getId_r() {
return id_r;
}
public void setId_r(String id_r) {
this.id_r = id_r;
}
public List<SRGarantieCasa> getFiles() {
return files;
}
public void setFiles(List<SRGarantieCasa> files) {
this.files = files;
}
public String viderChamps() {
// vider les champ
id_r = "";
return null;
}
public String search() {
if (id_r == "") {
files = fileService.getListeFiles();
} else {
files.clear();
files = fileService.getFile(id_r);
}
return null;
}
public String accesDetail() {
return "GCUpdate?faces-redirect=true";
}
public String saveFile() {
fileService.updateFile(selectedFile);
return "GarantieCasa?faces-redirect=true";
}
public String ajoutFile() {
fileService.addfile(file);
return "GarantieCasa?faces-redirect=true";
}
public String addFile() {
return "GarantieCasaAdd?faces-redirect=true";
}
public SRGarantieCasa getFile() {
return file;
}
public void setFile(SRGarantieCasa file) {
this.file = file;
}
public GarantiCasaBean() {
/*
* suggestions = new ArrayList<String>(); suggestions.add("Banana");
* suggestions.add("Cranberry"); suggestions.add("Blueberry");
* suggestions.add("Orange"); suggestions.add("Apple");
* suggestions.add("Strawberry");
*/
}
public void update()
{
for (SRGarantieCasa selectedFile : fileService.getListeFiles()){
System.out.println("GarantieCasaBean : Mother Code :"+selectedFile.getMother_Code());
fileService.updateFile(selectedFile);
}
//return "GarantieCasa?faces-redirect=true";
}
}
and here is the spring conf :
<!-- Service UI -->
<bean id="fileBean" class="ui.bean.GarantiCasaBean" scope="request">
<property name="fileService" ref="SO_GestionFile" />
<property name="file" ref="fileCGC" />
</bean>
<bean id="fileCGC" class="common.pojo.SRGarantieCasa" scope="session" />
I found the same issue in that topic :
Richfaces editable dataTable not setting updated values in Bean
but I the #this , #All doesn't work for me :( please Help me
Related
In my JSF I have a dataTable. Each row (showing a user) has a form that takes his ID as only parameter. The problem is that the associated backing bean never gets called (neither the methods follow/unfollow nor the constructor and the PostConstruct annotated method).
searchUserResult.xhtml
<h:dataTable value="#{searchUserMB.searchResult}" var="user"
rowClasses="tdUser"
rendered="#{not empty searchUserMB.searchResult}">
<h:column>
<f:facet name="header">
<h:outputText value="#{bundle.Username}" />
</f:facet>
<h:outputText value="#{user.username}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{bundle.Name}" />
</f:facet>
<h:outputText value="#{user.name}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{bundle.Surname}" />
</f:facet>
<h:outputText value="#{user.surname}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{bundle.Action}" />
</f:facet>
<h:form rendered="#{userMB.user.isFollower(user.id)}">
<h:commandButton value="#{bundle.Unfollow}"
action="#{followMB.unfollow}">
<f:param name="followerId" value="#{user.id}" />
</h:commandButton>
</h:form>
<h:form rendered="#{not userMB.user.isFollower(user.id)}">
<h:commandButton value="#{bundle.Follow}"
action="#{followMB.follow}">
<f:param name="followerId" value="#{user.id}" />
</h:commandButton>
</h:form>
</h:column>
</h:dataTable>
FollowMB
#ManagedBean
#RequestScoped
public class FollowMB {
#EJB
private DBObjectRemote db;
#ManagedProperty(value="#{userMB}")
private UserMB userMB;
#ManagedProperty(value="#{param.followerId}")
private int followerId;
public FollowMB() {
System.out.println("I'm in the constructor");
}
#PostConstruct
public void init() {
System.out.println("I'm in the PostConstruct");
}
public String follow() {
// DB stuff
}
public String unfollow() {
// DB stuff
}
public UserMB getUserMB() {
return userMB;
}
public void setUserMB(UserMB userMB) {
this.userMB = userMB;
}
public int getFollowerId() {
return followerId;
}
public void setFollowerId(int followerId) {
this.followerId = followerId;
}
}
Generated HTML:
<form id="j_idt15:0:j_idt30" name="j_idt15:0:j_idt30" method="post"
action="/myApp/private/searchUserResult.xhtml"
enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt15:0:j_idt30" value="j_idt15:0:j_idt30">
<script type="text/javascript"
src="/myApp/javax.faces.resource/jsf.js.xhtml?ln=javax.faces"></script>
<input type="submit" name="j_idt15:0:j_idt30:j_idt31" value="Follow"
onclick="mojarra.jsfcljs(document.getElementById('j_idt15:0:j_idt30'),
{'j_idt15:0:j_idt30:j_idt31':'j_idt15:0:j_idt30:j_idt31','followerId':'502'},'');
return false">
<input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0"
value="-6780758405072567889:8862631453580026176" autocomplete="off">
main.jsf
<h:form>
<h:inputText value="#{searchUserMB.searchTerm}" required="true"/><br/>
<h:commandButton action="#{searchUserMB.search}" />
</h:form>
SearchUserMB
#ManagedBean
#RequestScoped
public class SearchUserMB {
#EJB
private DBObjectRemote db;
private String searchTerm;
private List<User> searchResult;
public SearchUserMB() {}
public String search() {
searchResult = db.searchUser(searchTerm);
return "searchUserResult";
}
public String getSearchTerm() {
return searchTerm;
}
public void setSearchTerm(String searchTerm) {
this.searchTerm = searchTerm;
}
public List<User> getSearchResult() {
return searchResult;
}
public void setSearchResult(List<User> searchResult) {
this.searchResult = searchResult;
}
}
I'm trying to use a Primefaces dialog to insert an entity object in a database, it works the first time I use the dialog, the keeps the same bean inside the dialog when I try to use it the second time, here is my code:
this is in the file index.xhtml I have a table called patients and this is the footer:
<f:facet name="footer">
<p:outputPanel>
<p:growl id="growl" showDetail="true" />
<p:commandButton value="Add Patient" icon="ui-icon-extlink"
actionListener="#{patientsViewBean.addNewPatientDialog}">
<p:ajax event="dialogReturn"
listener="#{patientsViewBean.onDialogReturn}" update="patients" />
</p:commandButton>
</p:outputPanel>
</f:facet>
the dialog page is addNewDialog.xhtml it contains:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Add New Patient</title>
<style type="text/css">
.ui-widget {
font-size: 80%;
}
</style>
</h:head>
<h:body>
<p:panel style="margin: 2px;">
<h:form id="form" >
<p:growl id="msgs" showDetail="true" />
<p:panel title="Patient Personal Information" header="Patient Personal Information">
<p:panelGrid style="margin: 2px 2px 2px 2px" columns="2"
columnClasses="label, value">
<p:outputLabel value="First Name: *"></p:outputLabel>
<p:inputText value="#{newPatientView.patient.firstName}"
required="true" label="First Name"></p:inputText>
<p:outputLabel value="Father Name: *"></p:outputLabel>
<p:inputText value="#{newPatientView.patient.fatherName}"
required="true" label="Father Name"></p:inputText>
<p:outputLabel value="Family Name: *"></p:outputLabel>
<p:inputText value="#{newPatientView.patient.familyName}"
required="true" label="Family Name"></p:inputText>
<p:outputLabel value="Mother Name: *"></p:outputLabel>
<p:inputText value="#{newPatientView.patient.motherName}"
required="true" label="Mother Name"></p:inputText>
<p:outputLabel value="Birth Date"></p:outputLabel>
<p:calendar id="bdate" value="#{newPatientView.patient.birthDate}"
locale="ar" navigator="true" yearRange="c-120:c" effect="fade"
mask="true" pattern="yyyy-MM-dd" required="true" />
<p:outputLabel value="Birth Place: "></p:outputLabel>
<p:inputText value="#{newPatientView.patient.birthPlace}"
required="false" label="Birth Place"></p:inputText>
</p:panelGrid>
</p:panel>
<p:panel title="Contact Information" header="Contact Information">
<p:remoteCommand name="updateButtonsStatus"
process="#this contactInfoTable:buttons"
update="contactInfoTable:buttons"></p:remoteCommand>
<p:dataTable id="contactInfoTable" var="cntctInfo"
selectionMode="single" rowKey="#{cntctInfo}"
selection="#{newPatientView.cntctInfo}"
value="#{newPatientView.patient.contactInfoList}" editable="true"
widgetVar="cellCntctInfo">
<p:ajax event="rowEditInit"
listener="#{newPatientView.onRowEditInit}"
oncomplete="updateButtonsStatus()" />
<p:ajax event="rowEdit" immediate="true"
listener="#{newPatientView.onRowEdit}" update=":form:msgs"
oncomplete="updateButtonsStatus()" />
<p:ajax event="rowEditCancel"
listener="#{newPatientView.onRowCancel}"
oncomplete="updateButtonsStatus()" />
<p:column headerText="Email">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{cntctInfo.email}" />
</f:facet>
<f:facet name="input">
<p:inputText id="emailInput" value="#{cntctInfo.email}"
style="width:95%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Phone">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{cntctInfo.phone}" />
</f:facet>
<f:facet name="input">
<p:inputText id="phoneInput" value="#{cntctInfo.phone}"
style="width:95%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Type">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{cntctInfo.type}" />
</f:facet>
<f:facet name="input">
<p:selectOneMenu id="typeInput" value="#{cntctInfo.type}"
style="width:100%; height: 95%">
<f:selectItem itemLabel="Main" itemValue="Main"></f:selectItem>
<f:selectItem itemLabel="Home" itemValue="Home"></f:selectItem>
<f:selectItem itemLabel="Work" itemValue="Work"></f:selectItem>
<f:selectItem itemLabel="Perm" itemValue="Perm"></f:selectItem>
<f:selectItem itemLabel="Curr" itemValue="Curr"></f:selectItem>
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Edit" style="width:32px">
<p:rowEditor />
</p:column>
<f:facet name="footer">
<p:outputPanel id="buttons" autoUpdate="true">
<p:commandButton value="Add New" process="#this contactInfoTable"
actionListener="#{newPatientView.onAddNewContactInfo}"
update="contactInfoTable :form:msgs"
disabled="#{newPatientView.addContactInfoDisabled}" />
<p:commandButton value="Delete Seleced"
process="#this contactInfoTable"
actionListener="#{newPatientView.onDeleteSelectedContactInfo}"
update="contactInfoTable :form:msgs"
disabled="#{newPatientView.deleteSelectedContactInfoDisabled}" />
</p:outputPanel>
</f:facet>
</p:dataTable>
</p:panel>
<p:panel>
<p:commandButton value="Save" actionListener="#{newPatientView.onDialogApprove}"></p:commandButton>
<p:commandButton value="Discard" actionListener="#{newPatientView.onDialogCancel}"></p:commandButton>
</p:panel>
</h:form>
</p:panel>
</h:body>
</html>
I have two baking beans newPatientView with this code:
package me.bttb.crs.beans.patient;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;
import me.bttb.crs.model.ContactInfo;
import me.bttb.crs.model.Ptnt;
#Named
#ManagedBean
#ViewScoped
public class NewPatientView {
#Inject
private PatientDAO patientDAO;
private Ptnt patient;
private List<ContactInfo> cntctInfoList;
private ContactInfo cntctInfo;
private boolean addContactInfoDisabled = false;
private boolean deleteSelectedContactInfoDisabled = true;
boolean confirm = false;
public NewPatientView() {
init();
}
public void init() {
this.setPatient(new Ptnt());
this.setCntctInfoList(new ArrayList<>());
this.getPatient().setContactInfoList(this.getCntctInfoList());
}
/////////////// EVENTS//////////////////
public void onAddNewContactInfo(ActionEvent event) {
if (patient.getContactInfoList() == null)
patient.setContactInfoList(new ArrayList<>());
boolean hasNewRow = patient.getContactInfoList().stream()
.filter(ci -> ci.getEmail() == null && ci.getPhone() == null).count() >= 1;
if (hasNewRow) {
FacesMessage msg = new FacesMessage("Can't add new row", "There is an empty row already!!");
FacesContext.getCurrentInstance().addMessage(null, msg);
} else {
ContactInfo cntctInfo = new ContactInfo();
cntctInfo.setType("Main");
cntctInfo.setPrsn(patient);
patient.getContactInfoList().add(cntctInfo);
}
}
public void onDeleteSelectedContactInfo(ActionEvent event) {
if (cntctInfo != null) {
cntctInfoList.remove(cntctInfo);
cntctInfo = null;
}
this.setDeleteSelectedContactInfoDisabled(true);
this.setAddContactInfoDisabled(false);
}
public void onRowEdit(RowEditEvent event) {
this.setDeleteSelectedContactInfoDisabled(true);
this.setAddContactInfoDisabled(false);
ContactInfo editedContactInfo = (ContactInfo) event.getObject();
boolean moreThanOne = patient.getContactInfoList().stream().filter(ci -> ci.equals(editedContactInfo))
.count() > 1;
if (moreThanOne) {
patient.getContactInfoList().remove(editedContactInfo);
FacesMessage msg = new FacesMessage("Row Removed", "The contact information provided is redundent!!");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
public void onRowEditInit(RowEditEvent event) {
if (event.getObject() instanceof ContactInfo) {
this.setCntctInfo((ContactInfo) event.getObject());
this.setDeleteSelectedContactInfoDisabled(false);
}
this.setAddContactInfoDisabled(true);
}
public void onRowCancel(RowEditEvent event) {
this.setDeleteSelectedContactInfoDisabled(true);
this.setAddContactInfoDisabled(false);
}
public void onDialogCancel() {
RequestContext.getCurrentInstance().closeDialog(null);
}
public void onDialogApprove() {
RequestContext.getCurrentInstance().closeDialog(patient);
}
public PatientDAO getPatientDAO() {
return patientDAO;
}
public void setPatientDAO(PatientDAO patientDAO) {
this.patientDAO = patientDAO;
}
public Ptnt getPatient() {
return patient;
}
public void setPatient(Ptnt patient) {
this.patient = patient;
}
public ContactInfo getCntctInfo() {
return cntctInfo;
}
public void setCntctInfo(ContactInfo cntctInfo) {
this.cntctInfo = cntctInfo;
}
public boolean isAddContactInfoDisabled() {
return addContactInfoDisabled;
}
public void setAddContactInfoDisabled(boolean addContactInfoDisabled) {
this.addContactInfoDisabled = addContactInfoDisabled;
}
public boolean isDeleteSelectedContactInfoDisabled() {
return deleteSelectedContactInfoDisabled;
}
public void setDeleteSelectedContactInfoDisabled(boolean deleteSelectedContactInfoDisabled) {
this.deleteSelectedContactInfoDisabled = deleteSelectedContactInfoDisabled;
}
public List<ContactInfo> getCntctInfoList() {
return cntctInfoList;
}
public void setCntctInfoList(List<ContactInfo> cntctInfoList) {
this.cntctInfoList = cntctInfoList;
}
}
and here is how I call the dialog in patienViewBean:
package me.bttb.crs.beans.patient;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;
import me.bttb.crs.model.Ptnt;
#Named
#ManagedBean
#ViewScoped
public class PatientsViewBean {
#Autowired
private PatientDAO patientDAO;
private List<Ptnt> patients;
private List<Ptnt> filterdPatients;
public PatientsViewBean() {
}
public long getPatientsCount() {
return patientDAO.getPatientsCount();
}
public List<Ptnt> refreshAllPatients() {
this.setPatients(patientDAO.getPatients());
return this.getPatients();
}
public void addNewPatientDialog() {
Map<String, Object> options = new HashMap<String, Object>();
options.put("resizable", false);
options.put("draggable", false);
options.put("modal", true);
//options.put("contentHeight", "95%");
//options.put("height", 400);
//options.put("width", 700);
options.put("closable", false);
RequestContext.getCurrentInstance().openDialog("newPatientDialog", options, null);
}
public void onDialogReturn(SelectEvent event) {
if (event.getObject() != null && event.getObject() instanceof Ptnt) {
patientDAO.addpatient((Ptnt) event.getObject());
refreshAllPatients();
FacesMessage msg = new FacesMessage("Patient Added", "Pateint information added in the database.");
FacesContext.getCurrentInstance().addMessage(null, msg);
} else {
FacesMessage msg = new FacesMessage("operation canceld", "Nothing is saved to the database.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
////////////////////////////////////
public PatientDAO getPatientDAO() {
return patientDAO;
}
public void setPatientDAO(PatientDAO patientDAO) {
this.patientDAO = patientDAO;
}
public List<Ptnt> getPatients() {
return patients;
}
public void setPatients(List<Ptnt> patients) {
this.patients = patients;
}
public List<Ptnt> getFilterdPatients() {
return filterdPatients;
}
public void setFilterdPatients(List<Ptnt> filterdPatients) {
this.filterdPatients = filterdPatients;
}
}
what am I doing wrong, shouldn't the newPatientView get recreated every time the dialog shows up??
please explain the life cycle of the view here.
I followed Primefaces DataGrid showcase for doing my datatable. I want an edit imagebutton for each row of datatable and after onclick I want to show an edit dialog form.
The problem is that setSelectedChannel method not fired on commandlink click , so I have this error when dialog try to access to selecteChannel fields:
javax.el.PropertyNotFoundException: /WEB-INF/includes/channels.xhtml #54,91 value="#{channelBean.selectedChannel.name}": Target Unreachable, 'selectedChannel' returned null
So this is my xhtml include page:
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:form id="channelForm">
<p:dataTable var="channel" value="#{channelBean.channels}">
<p:column headerText="Name">
<h:outputText value="#{channel.name}" />
</p:column>
<p:column headerText="Field 1">
<h:outputText value="#{channel.field1}" />
</p:column>
<p:column headerText="Field 2">
<h:outputText value="#{channel.field2}" />
</p:column>
<p:column headerText="ApiWriteKey">
<h:outputText value="#{channel.apiWriteKey}" />
</p:column>
<p:column headerText="Edit">
<p:commandLink update=":channelForm:panelEditCh" oncomplete="PF('dlg2').show();" title="Edit">
<h:outputText styleClass="ui-icon ui-icon-search" style="margin:0 auto;" />
<f:setPropertyActionListener value="#{channel}" target="#{channelBean.selectedChannel}" />
</p:commandLink>
</p:column>
</p:dataTable>
<p:dialog id="dialogNewChannel" header="New Channel" widgetVar="dlg1" modal="false" >
<h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
<p:outputLabel for="channelName" value="Channel name:" />
<p:inputText id="channelName" value="#{channelBean.name}" />
<p:outputLabel for="channelDesc" value="Channel description:" />
<p:inputText id="channelDesc" value="#{channelBean.description}" />
<p:outputLabel for="channelField1" value="Channel field 1:" />
<p:inputText id="channelField1" value="#{channelBean.field1}" />
<p:outputLabel for="channelField2" value="Channel field 2:" />
<p:inputText id="channelField2" value="#{channelBean.field2}" />
</h:panelGrid>
<p:commandButton id="saveButton" value="Save" action="#{channelBean.saveChannel}" update="channelForm" ajax="true" />
<p:commandButton value="Cancel" type="button" action="#{channelBean.resetForm}" onclick="PF('dlg1').hide();" />
</p:dialog>
<p:dialog id="dialogEditChannel" header="Edit Channel" widgetVar="dlg2" modal="false" >
<h:panelGrid id="panelEditCh" columns="2" style="margin-bottom:10px" cellpadding="5">
<p:outputLabel for="channelNameEdit" value="Channel name:" />
<p:inputText id="channelNameEdit" value="#{channelBean.selectedChannel.name}" />
<p:outputLabel for="channelDescEdit" value="Channel description:" />
<p:inputText id="channelDescEdit" value="#{channelBean.selectedChannel.description}" />
<p:outputLabel for="channelField1Edit" value="Channel field 1:" />
<p:inputText id="channelField1Edit" value="#{channelBean.selectedChannel.field1}" />
<p:outputLabel for="channelField2Edit" value="Channel field 2:" />
<p:inputText id="channelField2Edit" value="#{channelBean.selectedChannel.field2}" />
</h:panelGrid>
<p:commandButton id="saveButtonEdit" value="Save" action="#{channelBean.saveChannel}" update="channelForm" ajax="true" />
<p:commandButton value="Cancel" type="button" action="#{channelBean.resetForm}" onclick="PF('dlg2').hide();" />
</p:dialog>
<p:spacer></p:spacer>
<p:spacer></p:spacer>
<p:commandButton value="New Channel" type="button" onclick="PF('dlg1').show();" />
<!-- <p:blockUI block="dialogNewChannel" trigger="saveButton">
<p:graphicImage name="images/ajax-loader-large.gif"/>
</p:blockUI>
-->
</h:form>
and ManagedBean :
#ManagedBean
#ViewScoped
public class ChannelBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1668407926998690759L;
private String idChannel;
private String apiWriteKey;
private String name;
private String description;
private String userId;
private String field1;
private String field2;
private Channel selectedChannel;
private List<Channel> channels= new ArrayList<Channel>() ;
#PostConstruct
public void populateChannelList(){
refreshList();
}
public void refreshList(){
HttpSession session = Util.getSession();
int idUser=-1;
if(session!=null &&session.getAttribute("idUser")!=null){
idUser=(int) session.getAttribute("idUser");
}
channels=ChannelDAO.getChannels(idUser);
}
public void saveChannel(){
....
}
public void resetForm(){
idChannel="";
apiWriteKey="";
name="";
description="";
userId="";
field1="";
field2="";
}
public String getIdChannel() {
return idChannel;
}
public void setIdChannel(String idChannel) {
this.idChannel = idChannel;
}
public String getApiWriteKey() {
return apiWriteKey;
}
public void setApiWriteKey(String apiWriteKey) {
this.apiWriteKey = apiWriteKey;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
public List<Channel> getChannels() {
return channels;
}
public void setChannels(List<Channel> channels) {
this.channels = channels;
}
public Channel getSelectedChannel() {
return selectedChannel;
}
public void setSelectedChannel(Channel selectedChannel) {
this.selectedChannel = selectedChannel;
}
}
Hi trying to call the method on button click, but it doesnt work i also tried onclick instead action it also doesnt work. i couldnt understand where is the mistake please help me
here is my managed bean
package com.primefaces.managedbean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import com.primefaces.domain.KenLocation;
import com.primefaces.service.ILocationService;
#ManagedBean(name = "locationbean")
#ViewScoped
public class LocationBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1101264226039168006L;
/**
*
*/
#ManagedProperty(value = "#{LocationService}")
private ILocationService locationService;
private List<KenLocation> locationList = new ArrayList<KenLocation>();
KenLocation kenLocation;
private String locationName;
private String address1;
private String address2;
private String city;
private String pincode;
private String state;
private String contactNo;
private String organisationName;
#PostConstruct
private void init() {
locationList = locationService.onLoad();
}
public void addnewlocation() {
System.out.println("I am inside newlocation");
kenLocation = new KenLocation();
kenLocation.setLocationName(locationName);
kenLocation.setAddress1(address1);
kenLocation.setAddress2(address2);
kenLocation.setCity(city);
kenLocation.setPincode(Integer.parseInt(pincode));
kenLocation.setPhone(contactNo);
System.out.println(kenLocation);
}
public String getLocationName() {
return locationName;
}
public void setLocationName(String locationName) {
this.locationName = locationName;
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public String getOrganisationName() {
return organisationName;
}
public void setOrganisationName(String organisationName) {
this.organisationName = organisationName;
}
public ILocationService getLocationService() {
return locationService;
}
public void setLocationService(ILocationService locationservice) {
this.locationService = locationservice;
}
public List<KenLocation> getLocationList() {
return locationList;
}
public void setLocationList(List<KenLocation> a_locationList) {
locationList = a_locationList;
}
}
and JSF
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h5>Product List {9 Products can be added per mail}</h5>
<p:separator></p:separator>
<p:growl id="growlLocationtable"></p:growl>
<p:commandButton id="new_location" title="CreateLocation" type="button"
icon="ui-icon-document" onclick="PF('dlg1').show()" />
<p:commandButton id="edit_location" title="EditLocation"
icon=" ui-icon-pencil" />
<p:commandButton id="delete_location" title="DeleteLocation"
icon="ui-icon-trash" />
<h:form id="Locationdataform">
<h:panelGrid columns="2" id="Locationdatatablepanel" resizable="false"
size="600">
<p:dataTable id="Locationdatatable" var="odt"
value="#{locationbean.locationList}">
<p:column headerText="Location Name">
<h:outputText value="#{odt.locationName}" />
</p:column>
<p:column headerText="Address Line1">
<h:outputText value="#{odt.address1}" />
</p:column>
<p:column headerText="Address Line2">
<h:outputText value="#{odt.address2}" />
</p:column>
<p:column headerText="City">
<h:outputText value="#{odt.city}" />
</p:column>
<p:column headerText="Pincode">
<h:outputText value="#{odt.pincode}" />
</p:column>
<p:column headerText="State">
<h:outputText value="#{odt.state}" />
</p:column>
<p:column headerText="Contact No">
<h:outputText value="#{odt.phone}" />
</p:column>
<p:column headerText="Organisation Name">
<h:outputText value="#{odt.ken_Org_ID.name}" />
</p:column>
</p:dataTable>
</h:panelGrid>
</h:form>
<!-- dialog -->
<p:dialog header="Add Location" widgetVar="dlg1" minHeight="40"
modal="true">
<h:form id="addLocationForm">
<h:panelGrid columns="2" id="grid">
<h:outputLabel value="Location Name"></h:outputLabel>
<p:inputText id="txt_Name" value="#{locationbean.locationName}" />
<h:outputLabel value="Address1"></h:outputLabel>
<p:inputText id="txt_address1" value="#{locationbean.address1}" />
<h:outputLabel value="Address2"></h:outputLabel>
<p:inputText id="txt_address2" value="#{locationbean.address2}" />
<h:outputLabel value="City"></h:outputLabel>
<p:inputText id="txt_city" value="#{locationbean.city}" />
<h:outputLabel value="Pincode"></h:outputLabel>
<p:inputText id="txt_pincode" value="#{locationbean.pincode}" />
<h:outputLabel value="State"></h:outputLabel>
<p:inputText id="txt_state" value="#{locationbean.state}" />
<h:outputLabel value="Contactno"></h:outputLabel>
<p:inputText id="txt_contactno" value="#{locationbean.contactNo}" />
<h:outputLabel value="Organistaion name"></h:outputLabel>
<p:inputText id="txt_orgname"
value="#{locationbean.organisationName}" />
<p:commandButton id="addlocatin_btn" value="Save"
action="#{locationbean.addnewlocation}" type="submit" />
</h:panelGrid>
</h:form>
</p:dialog>
I've been strugling with an issue I can't find the way to solve it and I can't find anything related on the web.
I'm using Netbeans with Primefaces to develop a JSF web app.
Everything works fine except that sometimes the call to the actionlisteners is being done several times.
For example this is my JSF page with primefaces:
<h:body>
<ui:composition template="home.xhtml">
<ui:define name="content">
<h2>Administración de alertas SMS</h2>
<p:panel id="display">
<p:selectBooleanCheckbox itemLabel="Enviar alertas SMS" value="#{smsConfigBean.alertsEnabled}"/>
<p>
<h:outputText value="Mensaje de Texto: " /><br /><br />
<p:inputTextarea rows="5" cols="50" counterTemplate="{0} caracteres restantes." counter="counter" maxlength="160" value="#{smsConfigBean.smsMessage}" id="smsMessage"/>
<br />
<h:outputText id="counter" />
</p>
<p>
<h:outputText value="Dispositivo de envio SMS: " />
<p:selectOneRadio id="options" value="#{smsConfigBean.usePhone}">
<f:selectItem itemLabel="Modem USB" itemValue="false" />
<f:selectItem itemLabel="Telefono Android" itemValue="true" />
</p:selectOneRadio>
</p>
<br />
<p:tabView id="tabView">
<p:tab id="tab1" title="Modem USB">
<h:panelGrid id="modemGrid" columns="2" cellpadding="4">
<h:outputText value="Puerto: " />
<p:inputText id="port" value="#{smsConfigBean.port}"/>
<h:outputText value="Bit Rate (Opcional): " />
<p:inputText id="bitRate" value="#{smsConfigBean.bitRate}"/>
<h:outputText value="Nombre de modem (Opcional): " />
<p:inputText id="modemName" value="#{smsConfigBean.modemName}"/>
<h:outputText value="PIN: " />
<p:inputText id="pin" value="#{smsConfigBean.modemPin}"/>
<h:outputText value="Centro de Mensajes: " />
<p:inputText id="smsc" value="#{smsConfigBean.SMSC}"/>
</h:panelGrid>
</p:tab>
<p:tab id="tab2" title="Telefono Android">
<h:panelGrid id="phoneGrid" columns="2" cellpadding="4">
<h:outputText value="Path ADB:" />
<p:inputText id="adbPath" value="#{smsConfigBean.adbPath}"/>
<h:outputText value="Directorio de Configuracion:" />
<p:inputText id="configPath" value="#{smsConfigBean.configPath}"/>
<h:outputText value="Modo de conexion " />
<p:selectOneRadio id="connectOptions" value="#{smsConfigBean.useWifi}">
<f:selectItem itemLabel="USB" itemValue="false"/>
<f:selectItem itemLabel="WiFi" itemValue="true" />
</p:selectOneRadio>
<h:outputText value="Direccion IP (Solo para WiFi): " />
<p:inputText id="ipDir" value="#{smsConfigBean.ipDir}"/>
</h:panelGrid>
</p:tab>
</p:tabView>
</p:panel>
<br />
<p:commandButton id="saveSmsAlerts" value="Guardar" update="messages" action="#{smsConfigBean.saveChanges}" actionListener="#{smsConfigBean.saveChanges}"/>
</ui:define>
</ui:composition>
</h:body>
And this is my backend bean:
package Beans;
import helpers.Global; import javax.faces.bean.ManagedBean; import
javax.faces.bean.SessionScoped;
#ManagedBean #SessionScoped public class SmsConfigBean {
private Boolean alertsEnabled;
private Boolean usePhone;
private Boolean useWifi;
private String port;
private int bitRate;
private String modemName;
private String modemPin;
private String SMSC;
private String adbPath;
private String configPath;
private String ipDir;
private String smsMessage;
public SmsConfigBean() {
alertsEnabled = Global.getAlertsEnabled();
port = Global.getPort();
bitRate = Global.getBitRate();
modemName = Global.getModemName();
modemPin = Global.getModemPin();
SMSC = Global.getSMSC();
usePhone = Global.getUsePhone();
adbPath = Global.getAdbPath();
configPath = Global.getConfigPath();
useWifi = Global.getUseWifi();
ipDir = Global.getIpDir();
smsMessage = Global.getSmsMessage();
}
public Boolean getAlertsEnabled() {
return alertsEnabled;
}
public void setAlertsEnabled(Boolean alertsEnabled) {
this.alertsEnabled = alertsEnabled;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public int getBitRate() {
return bitRate;
}
public void setBitRate(int bitRate) {
this.bitRate = bitRate;
}
public String getModemName() {
return modemName;
}
public void setModemName(String modemName) {
this.modemName = modemName;
}
public String getModemPin() {
return modemPin;
}
public void setModemPin(String modemPin) {
this.modemPin = modemPin;
}
public String getSMSC() {
return SMSC;
}
public void setSMSC(String SMSC) {
this.SMSC = SMSC;
}
public Boolean getUsePhone() {
return usePhone;
}
public void setUsePhone(Boolean usePhone) {
this.usePhone = usePhone;
}
public String getAdbPath() {
return adbPath;
}
public void setAdbPath(String adbPath) {
this.adbPath = adbPath;
}
public String getConfigPath() {
return configPath;
}
public void setConfigPath(String configPath) {
this.configPath = configPath;
}
public Boolean getUseWifi() {
return useWifi;
}
public void setUseWifi(Boolean useWifi) {
this.useWifi = useWifi;
}
public String getIpDir() {
return ipDir;
}
public void setIpDir(String ipDir) {
this.ipDir = ipDir;
}
public String getSmsMessage() {
return smsMessage;
}
public void setSmsMessage(String smsMessage) {
this.smsMessage = smsMessage;
}
public void saveChanges(){
Global.setSmsMessage(smsMessage);
Global.setIpDir(ipDir);
Global.setUseWifi(useWifi);
Global.setConfigPath(configPath);
Global.setAdbPath(adbPath);
Global.setUsePhone(usePhone);
Global.setSMSC(SMSC);
Global.setModemPin(modemPin);
Global.setModemName(modemName);
Global.setBitRate(bitRate);
Global.setPort(port);
Global.setAlertsEnabled(alertsEnabled);
Global.sendInfoResponseMessage("Cambios guardados con exito");
}
}
The problem is that when saving the changes of this form by hitting the commandButton 'saveSmsAlerts' the call is being made twice. Resulting in the growl message to be shown twice too.
(You won't see the growl component because it was defined in the template)
This happens also in another page that I did for the same app that uploads a file using fileuploader. The file is uploaded 4 times!!
I'm using chrome to test the application and netbeans to develop it.
You should remove one of action or actionListener attribute.
<p:commandButton id="saveSmsAlerts" value="Guardar" update="messages" action="#{smsConfigBean.saveChanges}"/>
if you remove action attribute you should change the action method signature
<p:commandButton id="saveSmsAlerts" value="Guardar" update="messages" actionListener="#{smsConfigBean.saveChanges}"/>
Like this
public void saveChanges(ActionEvent e){
...
}
here is the difference between action and actionListener
Differences between action and actionListener