Faces notifications not working - jsf

I am having a strange issue with the Faces messages.
Sometimes, the <p:growl>, <p:messages> display correctly the notification after executing some method. But there are specific JSF pages where no notification is shown.
For example:
public String create() {
try {
getFacade().create(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("NotaCreated"));
return prepareCreate();
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
The method is called in
<h:commandLink action="#{notaController.create}" value="#{bundle.CreateNotaSaveLink}"/>
And it does not show the notification. But for other entities as well as for other operations the messages are displayed correctly. This happens also in other functions, where I need feedback and it is not shown as expected.
I've been searching around and read this post and tried some of the suggested solutions, including setting the ajax=false attribute in commandButtons and commandLinks or using the scripts posted, but none of that worked for me, so I ask this question hoping that someone can help me.
Do I have to configure something to make it work correctly? I need this to get fixed by tomorrow and I don't know what else to do.
Update:
These are the JsfUtil methods:
public static void addSuccessMessage(String msg) {
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
FacesContext.getCurrentInstance().addMessage("successInfo", facesMsg);
}
public static void addErrorMessage(Exception ex, String defaultMsg) {
String msg = ex.getLocalizedMessage();
if (msg != null && msg.length() > 0) {
addErrorMessage(msg);
} else {
addErrorMessage(defaultMsg);
}
}
This is the Create.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<ui:composition template="/template.xhtml">
<ui:define name="title">
<h:outputText value="#{bundle.CreateNotaTitle}"></h:outputText>
</ui:define>
<ui:define name="body">
<h:panelGroup id="messagePanel" layout="block">
<h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
</h:panelGroup>
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="#{bundle.CreateNotaLabel_fecha}" for="fecha" />
<p:calendar value="#{notaController.selected.fecha}" id="fecha"/>
<h:outputLabel value="#{bundle.CreateNotaLabel_observaciones}" for="observaciones" />
<p:inputTextarea rows="4" cols="30" id="observaciones" value="#{notaController.selected.observaciones}" title="#{bundle.CreateNotaTitle_observaciones}" />
<h:outputLabel value="#{bundle.CreateNotaLabel_tipoNota}" for="tipoNota" />
<p:selectOneMenu id="tipoNota" value="#{notaController.selected.tipoNota}" required="true" requiredMessage="#{bundle.CreateNotaRequiredMessage_tipoNota}">
<f:selectItem itemLabel="---"/>
<f:selectItem itemValue="debito" itemLabel="Débito"/>
<f:selectItem itemValue="credito" itemLabel="Crédito"/>
</p:selectOneMenu>
<h:outputLabel value="#{bundle.CreateNotaLabel_monto}" for="monto" />
<p:inputText id="monto" value="#{notaController.selected.monto}" title="#{bundle.CreateNotaTitle_monto}" required="true" requiredMessage="#{bundle.CreateNotaRequiredMessage_monto}"/>
<h:outputLabel value="#{bundle.CreateNotaLabel_factura}" for="factura" />
<p:selectOneMenu id="factura" converter="#{facturaController.convertidor}" value="#{notaController.selected.factura}" filter="true" filterMatchMode="startsWith" required="true" requiredMessage="#{bundle.CreateNotaRequiredMessage_factura}">
<f:selectItem itemLabel="---"/>
<f:selectItems value="#{facturaController.facturas}" var="factura" itemValue="#{factura}" itemLabel="#{factura.idFactura}" />
</p:selectOneMenu>
</h:panelGrid>
<br />
<h:commandLink action="#{notaController.create}" value="#{bundle.CreateNotaSaveLink}">
<f:setPropertyActionListener value="#{cargaController.crearCargaDefault(sesionMB.usuario)}"
target="#{notaController.selected.carga}"/>
</h:commandLink>
<br />
<br />
<h:commandLink action="#{notaController.prepareList}" value="#{bundle.CreateNotaShowAllLink}" immediate="true"/>
<br />
<br />
<h:link outcome="/inicio" value="#{bundle.CreateNotaIndexLink}"/>
</h:form>
</ui:define>
</ui:composition>

I had the same problem. The faces message wasn't rendering it was being lost. I tried this code and it worked.
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().getFlash().setKeepMessages(true);
context.addMessage("successInfo", facesMsg);
I hope it works.

Related

How to pass bean action/listener method via <ui:param> of <ui:include>

I'm using <ui:include> to load a data table (I'm using Primefaces). I want use <ui:param> in the listener into the tag <p:ajax>. I tested the code that is down, but not trigger the event onRowEdit or onRowCancel.
This is my page:
...
<ui:include src="../Componentes/tablaEditable.xhtml">
<ui:param name="columnas" value="#{tabla2FuentesHL7.dataTableColumns}" />
<ui:param name="bean" value="#{tabla2FuentesHL7.listTabla2FuenteDTO}" />
<ui:param name="aceptarEdicion" value="#{tabla2FuentesHL7.onRowEdit}" />
<ui:param name="cancelarEdicion" value="#{tabla2FuentesHL7.onRowCancel}" />
</ui:include>
...
My data table:
<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:c="http://java.sun.com/jsp/jstl/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:form >
<p:dataTable value="#{bean}" scrollable="true" scrollHeight="100" var="dato" editable="true">
<p:ajax event="rowEdit" listener="#{aceptarEdicion}" />
<p:ajax event="rowEditCancel" listener="#{cancelarEdicion}" />
<c:forEach items="#{columnas}" var="column" varStatus="loop">
<p:column headerText="#{column.header}" sortBy="#{dato[column.property]}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{dato[column.property]}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{dato[column.property]}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
</c:forEach>
<p:column style="width:32px">
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>
My bean:
#ManagedBean(name = "tabla2FuentesHL7")
#ViewScoped
enter code herepublic class Tabla2FuentesHL7 {
private static final long serialVersionUID = 1L;
private List<DataTableColumn> dataTableColumns = new ArrayList<DataTableColumn>();
private List<Tabla2FuenteDTO> listTabla2FuenteDTO = new ArrayList<Tabla2FuenteDTO>();
#PostConstruct
public void init() {
listTabla2FuenteDTO = Test.getFuenteTabla2();
dataTableColumns = CargarTablas.getTabla2FuenteHL7();
}
public List<Tabla2FuenteDTO> getListTabla2FuenteDTO() {
return listTabla2FuenteDTO;
}
public List<DataTableColumn> getDataTableColumns() {
return dataTableColumns;
}
public void onRowEdit(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Elemento modificado");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onRowCancel(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Modificación cancelada");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onCellEdit(CellEditEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
if (newValue != null && !newValue.equals(oldValue)) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed", "Old: " + oldValue + ", New:" + newValue);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
}
This is the error:
javax.el.PropertyNotFoundException: /Componentes/tablaEditable.xhtml #12,76 listener="#{cancelarEdicion}": /Modulos/hl7.xhtml #31,104 value="#{tabla2FuentesHL7.onRowCancel}": The class 'com.queres.xedoc.consola.componentes.Tabla2FuentesHL7' does not have the property 'onRowCancel'.
If I change this:
<p:ajax event="rowEdit" listener="#{aceptarEdicion}" />
<p:ajax event="rowEditCancel" listener="#{cancelarEdicion}" />
for this:
<p:ajax event="rowEdit" listener="#{tabla2FuentesHL7.onRowEdit}" />
<p:ajax event="rowEditCancel" listener="#{tabla2FuentesHL7.onRowCancel}" />
my code works fine, but I need a dynamic data table. Is there any way to pass a parameter to the listener?
Thanks!
The <ui:param> can only pass value expressions, not method expressions.
Better make use of the ability of EL to parameterize method names via brace [] notation. Then you can just declare the method names as plain vanilla String.
<ui:include src="../Componentes/tablaEditable.xhtml">
...
<ui:param name="beanEdicion" value="#{tabla2FuentesHL7}" />
<ui:param name="aceptarEdicion" value="onRowEdit" />
<ui:param name="cancelarEdicion" value="onRowCancel" />
</ui:include>
<p:ajax event="rowEdit" listener="#{beanEdicion[aceptarEdicion]()}" />
<p:ajax event="rowEditCancel" listener="#{beanEdicion[cancelarEdicion]()}" />
Update as per the comments, it appears to still not work in PrimeFaces 5.1 even though the related issue says that it's already fixed in 3.5. You'd basically need to reopen the issue.
In the meanwhile, you can workaround this with help of <o:methodParam> tag of JSF utility library OmniFaces. This basically converts a value expression to a method expression:
<ui:include src="../Componentes/tablaEditable.xhtml">
...
<ui:param name="aceptarEdicion" value="#{tabla2FuentesHL7.onRowEdit}" />
<ui:param name="cancelarEdicion" value="#{tabla2FuentesHL7.onRowCancel}" />
</ui:include>
<o:methodParam name="aceptarEdicionParam" value="#{aceptarEdicion}" />
<o:methodParam name="cancelarEdicionParam" value="#{cancelarEdicion}" />
...
<p:ajax event="rowEdit" listener="#{aceptarEdicionParam}" />
<p:ajax event="rowEditCancel" listener="#{cancelarEdicionParam}" />

How can I navigate to another page in JSF?

I am developing small web app using JSF/PrimeFaces. I am able to call bean from web, however, I am unable to return web. This is what I have:
requestLeave.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Request leave</title>
</h:head>
<h:body>
<script language="javascript">
var today = new Date();
document.write(today);
</script>
<h:form id="form" >
<h:panelGrid id= "grid" columns="2" cellpadding="5">
<f:facet name="header">
<p:messages id="msgs" />
</f:facet>
<h:outputLabel for="title" value="Leave Title:" style="font-weight:bold" />
<p:inputText value="#{requestLeave.titleLeave}" required="true" requiredMessage="title is required." />
<h:outputLabel for="type" value="Type:" style="font-weight:bold" />
<p:inputText value="#{requestLeave.typeLeave}" required="true" requiredMessage="type is required." />
<p:commandButton value="Submit" actionListener="#{requestLeave.buttonAction}" />
</h:panelGrid>
</h:form>
</h:body>
</html>
RequestLeave.java
import javax.faces.bean.ManagedBean;
#ManagedBean
public class RequestLeave {
private String titleLeave;
private String typeLeave;
public String getTitleLeave() {
return titleLeave;
}
public void setTitleLeave(String titleLeave) {
this.titleLeave = titleLeave;
}
public String getTypeLeave() {
return typeLeave;
}
public void setTypeLeave(String typeLeave) {
this.typeLeave = typeLeave;
}
public String buttonAction() {
System.out.println("leave title " + titleLeave);
System.out.println("leave type " + typeLeave);
return ("index.jsp");
}
}
With this, I cannot return to index.jsp. Can anyone help me? Thanks in advance.
Change the actionListener attribute to action:
<p:commandButton value="Submit" action="#{requestLeave.buttonAction}" />
More info:
Differences between action and actionListener?

JSF <h:selectOneMenu> issue .?

Here is my JSF .xhtml code. issue is that JSF value not sent to server when its change & save.its still send to server just earlier values
please help me to sort out this issue
thanks all :-)
<ui:decorate template="/jsf/templates/one_column.xhtml">
<ui:define name="label">
<h:outputText value="Card Status" style="font-weight:bold;" />
</ui:define>
<ui:define name="field">
<h:selectOneMenu value="#{profileController.selectedBean.title}">
<f:selectItems value="#{profileController.titles}"/>
</h:selectOneMenu>
<h:selectOneMenu
value="#{cardManagementController.selectedCardBean.cardStatusId}" >
<f:selectItem itemValue="null" itemLabel="please select"
noSelectionOption="true" />
<f:selectItems
value="#{cardManagementController.listCardTypeStatus}"
var="cardStatus" itemLabel="#{cardStatus.name}"
itemValue="#{cardStatus.statusId}" />
</h:selectOneMenu>
</ui:define>
</ui:decorate>
also here i will show my class codes
private CardBean selectedCardBean = new CardBean();
public void editCard() {
if (selectedCardBean != null) {//THIS selectedCardBean is still shows earlier records
cardService.editCardInformation(selectedCardBean);
selectedCardBean = new CardBean();
editCardEnabled = false;
}
searchCard();
}
Code Added
<!-- Buttons -->
<ui:decorate template="/jsf/templates/one_column.xhtml">
<ui:define name="label">
</ui:define>
<ui:define name="field">
<a4j:commandButton value="Submit" execute="#form"
action="#{cardManagementController.editCard()}"
render="#{rich:clientId('hpnlRes')} #{rich:clientId('hpnlSearch')} #{rich:clientId('hpnlEdit')}" />
<a4j:commandButton value="Back" style="margin-left:10px;"
action="#{cardManagementController.hideEditCard()}"
render="#{rich:clientId('hpnlRes')} #{rich:clientId('hpnlSearch')} #{rich:clientId('hpnlEdit')}" />
</ui:define>
</ui:decorate>
issue with my controller class..i fixed it thanks all
there i get item value
but pass from form its id
thatswhy
<f:selectItems
value="#{cardManagementController.listCardTypeStatus}"
var="cardStatus" itemLabel="#{cardStatus.name}"
itemValue="#{cardStatus.statusId}" />

Init method gets called twice in #Viewscoped bean

I am using schedule component of Primefaces. I am filling it with values from database and when the user selects sth from the selectonemenu an ajax event is triggered (I tried to put just the related code, if there is sth missing pls remind me):
xhtml:
<h:outputText value="Scope :" />
<h:selectOneMenu id="scope" value="#{scheduleView.scope}">
<f:selectItems value="#{lookup.scopeCombo}"/>
<p:ajax process="scope" update="schedule, scheduleForm, scheduleFormPG" listener="#{scheduleView.changeScopeType()}"/>
</h:selectOneMenu>
<p:schedule id="schedule" value="#{scheduleView.model}" editable="true"/>
Backing bean:
#ManagedBean
#ViewScoped
public class ScheduleView implements Serializable {
#PostConstruct
public void init() {
System.out.println("Init ");
scopeChange();
}
public void scopeChange(String scope){
System.out.println("scopeChange ");
model.clear();
events = (List<Event>) commonServis.bringEverythingByCriteria(Event.class, "scope" , scope);
for(int i= 0; i<events.size();i++){
model.addEvent(new DefaultScheduleEvent(events.get(i).getAd(), events.get(i).getStartDate(),events.get(i).getEndDate()));
}
public void changeScopeType() {
System.out.println("changeScopeType ");
scopeChange (scope);
}
The output of the above code is:
Init
scopeChange
When the user changes the value in the selectonemenu:
changeScopeType
Init
scopeChange
It is supposed to go into init method just once. But after the changeScopeType function is triggered it gets into the init method and fills the schedule with unrelated data. I thought it might be related to #Postconstruct annotation but I couldn't find any related explanation. Can anyone understand the reason and offer a solution?
Here is the full page:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://prime.primefaces.org/ui"
template="templates/layout.xhtml"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<ui:define name="title">#{labels.schedule}</ui:define>
<ui:define name="content">
<h:form id="scheduleForm">
<h:panelGrid id="scheduleFormPG">
<p:growl id="msgs" />
<p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false">
<h:graphicImage value="resources/images/ajax-loader.gif" />
</p:dialog>
<p:dialog showEffect="explode" hideEffect="explode" resizable="false"
header="warning" widgetVar="confirmationErase" appendToBody="true" modal="true">
<h:outputText value="Are you sure?"/>
<br/>
<p:commandButton value="Yes" actionListener="#{scheduleView.deleteEvent(AE)}"
update="msgs, scheduleFormPG, schedule, wrapperPanel"
onstart="statusDialog.show(),confirmationErase.hide()"
oncomplete="statusDialog.hide(), eventDialog.hide()" process="#parent, scope" />
<p:commandButton value="No" onclick="confirmationErase.hide()" type="button" />
</p:dialog>
<h:outputText value="scope :" />
<h:selectOneMenu id="scope" value="#{scheduleView.scope}">
<f:selectItems value="#{lookup.scopeTypeCombo}"/>
<p:ajax process="scope" update="schedule, scheduleForm, scheduleFormPG" listener="#{scheduleView.changeScopeType()}"/>
</h:selectOneMenu>
</h:panelGrid>
<p:schedule onDateSelectUpdate="wrapperPanel"
onEventSelectUpdate="wrapperPanel" onEventSelectComplete="eventDialog.show()" eventSelectListener="#{scheduleView.onEventSelect}"
onDateSelectComplete="eventDialog.show();" dateSelectListener="#{scheduleView.onDateSelect}" id="schedule" value="#{scheduleView.model}" editable="true"/>
<p:dialog id="dialog111" widgetVar="eventDialog" header="Event Information" showEffect="clip" hideEffect="clip">
<p:panel id="wrapperPanel">
<h:panelGrid id="eventDetails" columns="2">
<h:outputLabel for="eventName" value="Event Name *: " />
<p:inputText id="eventName" value="#{scheduleView.event.title}" required="true"/>
<h:outputLabel value="Start Date:" />
<p:calendar id="sKalender" value="#{scheduleView.event.startDate}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</p:calendar>
<h:outputLabel value="End Date:" />
<p:calendar id="eKalender" value="#{scheduleView.event.endDate}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</p:calendar>
<h:outputLabel value="All Day:" />
<h:selectBooleanCheckbox id="allDay" value="#{scheduleView.event.allDay}" />
<h:outputLabel value="scope: " />
<h:selectOneMenu id="scopeChoice" value="#{scheduleView.event.scope}">
<f:selectItems value="#{lookup.scopeTypeCombo}"/>
</h:selectOneMenu>
<p:commandButton onclick="confirmationErase.show()" oncomplete="eventDialog.hide()" update="wrapperPanel, msgs, schedule" type="reset" value="Delete" />
<p:commandButton value="Save" actionListener="#{scheduleView.addEvent(AE)}" process="#parent, scope" update="schedule wrapperPanel msgs scheduleForm" oncomplete="eventDialog.hide();"/>
</h:panelGrid>
</p:panel>
</p:dialog>
</h:form>
</ui:define>
</ui:composition>
The problem was this line of code:
<p:ajax process="scope" update="schedule, scheduleForm, scheduleFormPG" listener="#{scheduleView.changeScopeType()}"/>
update was refreshing the whole page, instead it should've only update schedule. When I change it to:
update="schedule"
the problem solved.
Thanks everyone for their effort.

function called many times for no reason

i have an init() function that returns a list of Domaines needed to populate a datatable, each row of this datatable have two commandlinks one for edit and another for delete, so when i press the edit command link a dialog window apear showing the Domaine information whith the possibility of editing (there is another dialog for adding new domaines), so the problem is when i press edit the init function is called 8 times then the attributes of the adding dialog are setted (even if there is no relation between the thee edit command link and the adding dialog window) after the init function is called again 4 times. i don't understand wh.
here my page code:
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<ui:composition template="gpsiTemplate.xhtml">
<ui:define name="left-menu">
<ui:include src="admin-menuGauche.xhtml" />
</ui:define>
<ui:define name="top">
<ui:include src="menu-top.xhtml" />
</ui:define>
<ui:define name="content-top">
<center>
<f:view>
<h:form id="topacteurs">
<h:panelGrid columns="14" styleClass="adminTopTable" >
<h5 class="calendar">Direction :</h5>
<p:selectOneMenu id="typeacteurs" value="#{domaine.choixDirection}" style="width: 180px">
<f:selectItem itemLabel="Direction..." itemValue="" />
<f:selectItems value="#{direction.initcomb()}" var="ta" itemValue="#{ta.codeDirection}" itemLabel="#{ta.libelleDirection}" />
<p:ajax update=":mainform:domainlist" process="topacteurs" event="change" />
</p:selectOneMenu>
<p:spacer width="20" height="0" />
<p:commandButton title="Ajouter un domaine" image="ui-icon ui-icon-disk" value="Ajouter un domaine" oncomplete="ajoutDomaine.show()"/>
</h:panelGrid>
</h:form>
</f:view>
</center>
</ui:define>
<ui:define name="content-content">
<h:form id="modifform">
<p:dialog header="Modifier un domaine" widgetVar="editDomaine" modal="true" width="400" height="200" showEffect="clip" hideEffect="clip">
<p:outputPanel id="editDomaineDetails" style="text-align:center;" layout="block">
<center>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel value="Intitulé :"/>
<p:inputText value="#{domaine.currentDomaine.libelleDomaine}" style="width: 180px"/>
<h:outputLabel value="Respensable :"/>
<p:inputText value="#{domaine.currentDomaine.nomDirecteur}" style="width: 180px"/>
<h:outputLabel value="Direction :"/>
<p:selectOneMenu id="editchoidirection" value="#{domaine.codeDirection}" style="width: 180px">
<f:selectItem itemLabel="Direction..." itemValue="" />
<f:selectItems value="#{direction.initcomb()}" var="ta" itemValue="#{ta.codeDirection}" itemLabel="#{ta.libelleDirection}" />
</p:selectOneMenu>
</h:panelGrid>
<h:panelGrid columns="2" cellpadding="5">
<p:commandButton value="Modifier" update="messages editDomaineDetails :mainform:domainlist" actionListener="#{domaine.update()}" oncomplete="editDomaine.hide()"/>
<p:commandButton value="Annuler" oncomplete="editDomaine.hide()"/>
</h:panelGrid>
</center>
</p:outputPanel>
</p:dialog>
</h:form>
<h:form id="mainform">
<p:growl id="messages" showDetail="true"/>
<p:confirmDialog message="Etes-vous sure?" width="200"
showEffect="clip" hideEffect="clip"
header="Confirmation" severity="alert" widgetVar="confirmation">
<p:commandButton value="Oui sure" update="messages :mainform:domainlist" actionListener="#{domaine.delete()}" oncomplete="confirmation.hide()"/>
<p:commandButton value="Non" onclick="confirmation.hide()" type="button" />
</p:confirmDialog>
<p:dialog header="Ajouter un domaine" widgetVar="ajoutDomaine" modal="true" width="400" height="200" showEffect="clip" hideEffect="clip">
<p:outputPanel id="ajoutDomaineDetails" style="text-align:center;" layout="block">
<center>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel value="Intitulé :"/>
<p:inputText value="#{domaine.nomDomaine}" style="width: 180px"/>
<h:outputLabel value="Respensable :"/>
<p:inputText value="#{domaine.nomDirecteur}" style="width: 180px"/>
<h:outputLabel value="Direction :"/>
<p:selectOneMenu id="ajoutchoidirection" value="#{domaine.codeDirection}" style="width: 180px">
<f:selectItem itemLabel="Direction..." itemValue="" />
<f:selectItems value="#{direction.initcomb()}" var="ta" itemValue="#{ta.codeDirection}" itemLabel="#{ta.libelleDirection}" />
</p:selectOneMenu>
</h:panelGrid>
<h:panelGrid columns="2" cellpadding="5">
<p:commandButton value="Ajouter" update="messages ajoutDomaineDetails :mainform:domainlist" actionListener="#{domaine.save()}" oncomplete="ajoutDomaine.hide()"/>
<p:commandButton value="Annuler" oncomplete="ajoutDomaine.hide()"/>
</h:panelGrid>
</center>
</p:outputPanel>
</p:dialog>
<p:dataTable id="domainlist" var="e" value="#{domaine.init()}">
<p:column headerText="Intitulé" filterBy="#{e.libelleDomaine}">
<h:outputText value="#{e.libelleDomaine}" />
</p:column>
<p:column headerText="Directeur" filterBy="#{e.nomDirecteur}">
<h:outputText value="#{e.nomDirecteur}" />
</p:column>
<p:column headerText="Direction" filterBy="#{e.directions.libelleDirection}">
<h:outputText value="#{e.directions.libelleDirection}" />
</p:column>
<p:column>
<h:panelGrid columns="3" style="border-color: #ffffff">
<p:commandLink update=":modifform:editDomaineDetails" title="Editer" oncomplete="editDomaine.show()">
<p:graphicImage value="resources/images/edit.png"/>
<f:setPropertyActionListener value="#{e}" target="#{domaine.currentDomaine}" />
</p:commandLink>
<p:commandLink title="Supprimer" oncomplete="confirmation.show()">
<p:graphicImage value="resources/images/delete.png"/>
<f:setPropertyActionListener value="#{e}" target="#{domaine.currentDomaine}" />
</p:commandLink>
</h:panelGrid>
</p:column>
</p:dataTable>
<p:stack icon="resources/images/stack/stck.png">
<p:menuitem value="Photo" icon="resources/images/stack/photo.png" url="http://localhost:8084/Gpsi/faces/profile-Photo.xhtml"/>
<p:menuitem value="Profile" icon="resources/images/stack/profile.png" url="http://localhost:8084/Gpsi/faces/profile.xhtml"/>
<p:menuitem value="Administration" icon="resources/images/stack/administration.png" url="http://localhost:8084/Gpsi/faces/admin.xhtml"/>
</p:stack>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
here the same code on Pastebin but it's a bit messed up: http://pastebin.com/W0XGa0d2
and here is my back up bean:
......
public List<Domaines> initComb()
{
.....
}
public List<Domaines> init()
{
if(choixDirection==0)
{
domaines=domainesService.getAllDomaines();
}
else
{
Directions direction=directionsService.getDirections(choixDirection);
domaines=domainesService.getDirDomaines(direction);
}
return domaines;
}
public void save()
{
......
}
public void delete()
{
......
}
public void update()
{
......
}
////////////////////////////////////////////////////////// setters & getters \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
......
public void setCodeDirection(Integer codeDirection)
{
this.codeDirection=codeDirection;
}
public Integer getCodeDirection()
{
return codeDirection;
}
public void setNomDomaine(String nomDomaine)
{
this.nomDomaine=nomDomaine;
}
public String getNomDomaine()
{
return nomDomaine;
}
public void setNomDirecteur(String nomDirecteur)
{
this.nomDirecteur=nomDirecteur;
}
public String getNomDirecteur()
{
return nomDirecteur;
}
......
}
i posted long codes cause don't where the problem is coming from.
Your mistake is that you're doing business action in a getter method instead of an action method which is called only once. You should not do business actions in getters. JSF EL uses getters to access bean properties. EL does not cache them when they're ever called once. Getters should solely return bean properties (or at highest do some lazy loading or quick and non-intensive calculation, e.g. a simple sum like return foo + bar;), but definitely not access the database or something.
Move that job to the constructor or #PostConstruct method or any event method of the bean. In this particular case, you likely want to use #PostConstruct to preload the list with help of an #EJB and let the ajax action listener point to the same method as well. In a #ViewScoped bean, this way it's called only once during first request of the view and also called only once when you change the selection.
#PostConstruct
public void init() {
if (choixDirection == 0) {
domaines = domainesService.getAllDomaines();
} else {
Directions direction = directionsService.getDirections(choixDirection);
domaines = domainesService.getDirDomaines(direction);
}
}
public List<Domaines> getDomaines() {
return domaines;
}
and fix the view accordingly
<p:ajax process="topacteurs" listener="#{domaine.init}" update=":mainform:domainlist" />
...
<p:dataTable id="domainlist" var="e" value="#{domaine.domaines}">
See also:
Why JSF calls getters multiple times

Resources