values in primefaces edit dialog not getting updated - ajax

I have table Providers.I am displaying the table contents in primefaces datatable and performing create,edit and delete operations on it using Jdbc and Tomcat 7.0.
In the edit dialog there are save and cancel buttons. When I click the cancel button and click on the edit button of another row the values of previous row are getting displayed.
I am getting the following warnings when I click on the cancel button:
com.sun.faces.application.view.FaceletPartialStateManagementStrategy saveDynamicActions
WARNING: Unable to save dynamic action with clientId 'editproviderform:btnCancelProviders' because the UIComponent cannot be found
Providers table structure:
ProviderId,
ProviderFirstName and
ProviderLastName
Providers.java
public class Providers implements Serializable {
private static final long serialVersionUID = 1L;
private Integer providerId;
private String providerFirstName;
private String providerLastName;
public Providers() {
}
public Providers(Integer providerId) {
this.providerId = providerId;
}
public Providers(Integer providerId, String providerFirstName, String providerLastName) {
this.providerId = providerId;
this.providerFirstName = providerFirstName;
this.providerLastName = providerLastName;
}
public Integer getProviderId()
{
return providerId;
}
public void setProviderId(Integer providerId)
{
this.providerId=providerId;
}
public String getProviderFirstName()
{
return providerFirstName;
}
public void setProviderFirstName(String providerFirstName)
{
this.providerFirstName=providerFirstName;
}
public String getProviderLastName()
{
return providerLastName;
}
public void setProviderLastName(String providerLastName)
{
this.providerLastName=providerLastName;
}
}
ProvidersBean.java
#ManagedBean(name = "providersBean")
#SessionScoped
public class ProvidersBean implements Serializable {
private Providers selectedProvider;
public Providers getSelectedProvider() {
return selectedProvider;
}
public void setSelectedProvider(Providers selectedProvider) {
this.selectedProvider = selectedProvider;
}
private Connection con;
#Resource(name = "jdbc/abc")
private DataSource ds;
public ProvidersBean() {
try {
Context ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/ClinicalStudy");
} catch (NamingException e) {
e.printStackTrace();
}
}
public void updateProvider() {
try {
con = ds.getConnection();
PreparedStatement ps = con.prepareStatement("UPDATE Providers SET
ProviderFirstName=?,ProviderLastName=? WHERE ProviderId=?");
ps.setString(1, selectedProvider.getProviderFirstName());
ps.setString(2, selectedProvider.getProviderLastName());
ps.setInt(3, selectedProvider.getProviderId());
ps.executeUpdate();
clearSelectedProvider();
ps.close();
con.close();
} catch (SQLException e) {
System.out.println("SQLException:" + e.getMessage());
}
}
#PostConstruct
public void init()
{
selectedProvider=new Providers();
}
public void clearSelectedProvider()
{
selectedProvider = null;
}
public void close(CloseEvent event)
{
event.getComponent().getChildren().clear();
clearSelectedProvider();
}
}
Edit dialog code
<p:dialog id="_EditProvider" header="Edit Provider" widgetVar="EditProvider" modal="true"
showEffect="fade" hideEffect="fade" resizable="false" position="center"
appendToBody="true">
<p:ajax event="close" listener="#{providersBean.close}"
update=":editproviderform" global="false"/>
<h:form id="editproviderform">
<p:panelGrid columns="2" rendered="#{not empty providersBean.selectedProvider}">
<h:outputLabel for="EdtProviderFirstName" value="ProviderFirstName*:"/>
<p:inputText id="EdtProviderFirstName" value="#{providersBean.selectedProvider.providerFirstName}"
required="true" requiredMessage="FirstName must be entered" maxlength="30"
validatorMessage="FirstName must be an alphanumeric value">
<f:validateRegex pattern="[a-zA-Z0-9\s]+"/>
</p:inputText>
<h:outputLabel for="EdtProviderLastName" value="ProviderLastName*:"/>
<p:inputText id="EdtProviderLastName" value="#{providersBean.selectedProvider.providerLastName}"
required="true" requiredMessage="LastName must be entered" maxlength="30"
validatorMessage="LastName must be an alphanumeric value">
<f:validateRegex pattern="[a-zA-Z0-9\s]+"/>
</p:inputText>
<p:commandButton id="btnUpdateProvider" value="Save" actionListener="#{providersBean.updateProvider()}"
update=":providersform:providersdt :providersmessages:messages" oncomplete="handleEditProviders(xhr,status,args)"/>
<p:commandButton id="btnCancelProviders" value="Cancel" actionListener="#{providersBean.clearSelectedProvider()}"
oncomplete="EditProvider.hide()"/>
</p:panelGrid>
</h:form>
</p:dialog>
Below is the code for the edit button in datatable:
<p:column style="text-align: center;">
<p:commandButton id="btneditproviders" value="Edit" update=":editproviderform" oncomplete="EditProvider.show()" title="Edit">
<f:setPropertyActionListener value="#{item[0]}" target="#{providersController.selectedProvider}"/>
</p:commandButton>
</p:column>

Related

primefaces 6.1 select one menu is populated but refuses to show values

I am trying to filter using a Select One Menu using PrimeFaces 6.1. But the menu does not drop down to show its values. It acts as if it is disabled. I have wrapped my datable with <h:form> and checked to see if the menu is populated in the browser inspect window.
Edit:
Here is the table:
<h:form>
<p:dataTable var="left" value="#{dataFilterView.leftGrid}"
widgetVar="leftTable"
filteredValue="#{dataFilterView.filteredLeftGrid}">
<ui:debug />
<p:column headerText="Project">
<h:outputText value="#{left.projectName}" />
</p:column>
<p:column headerText="Company">
<h:outputText value="#{left.companyName}" />
</p:column>
<p:column headerText="Location" filterBy="left.projectLocation">
<h:outputText value="#{left.projectLocation}" />
</p:column>
<p:column headerText="Request Name">
<h:outputText value="#{left.requestName}" />
</p:column>
<p:column headerText="Request Type" filterBy="#{left.requestType}"
filterMatchMode="exact">
<f:facet name="filter">
<p:selectOneMenu onchange="PF('leftTable').filter()"
style="width:70%; box-sizing: border-box;">
<f:selectItem itemLabel="Select One" itemValue="#{null}"
noSelectionOption="true" />
<f:selectItems
value="#{dataFilterView.uniqueRequestTypes.entrySet()}"
var="data" itemValue="#{data.key}" itemLabel="#{data.value}" />
</p:selectOneMenu>
</f:facet>
<h:outputText value="#{left.requestType}" />
</p:column>
</p:dataTable>
</h:form>
The ManagedBean is:
#ManagedBean(name = "dataFilterView")
#SessionScoped
public class DataFilterView {
private String reqType;
private List<LeftGridBean> leftGrid;
private List<LeftGridBean> filteredLeftGrid;
private List<String> uniqueLocations;
private Map<String, String> uniqueRequestTypes;
#ManagedProperty("#{supplierRequestsService}")
private SupplierRequestsService service;
#PostConstruct
public void init() {
leftGrid = service.populateLeftGrid();
}
public void setService(SupplierRequestsService service) {
this.service = service;
}
public List<LeftGridBean> getLeftGrid() {
return leftGrid;
}
public List<LeftGridBean> getFilteredLeftGrid() {
return filteredLeftGrid;
}
public void setFilteredLeftGrid(List<LeftGridBean> filteredLeftGrid) {
this.filteredLeftGrid = filteredLeftGrid;
}
public String getReqType() {
return reqType;
}
public void setReqType(String reqType) {
this.reqType = reqType;
}
public List<String> getUniqueLocations() {
Set<String> locations = new HashSet<>();
for (LeftGridBean lg : getLeftGrid()) {
locations.add(lg.getProjectLocation());
}
uniqueLocations = new ArrayList<String>(locations);
return uniqueLocations;
}
public Map<String, String> getUniqueRequestTypes() {
Map<String, String> requestTypes = new HashMap<>();
for (LeftGridBean lg : getLeftGrid()) {
requestTypes.put(lg.getRequestType(), lg.getRequestType());
}
uniqueRequestTypes = requestTypes;
return uniqueRequestTypes;
}
}
Domain:
public class LeftGridBean {
private String projectName;
private String projectLocation;
private String requestType;
private String requestName;
private String companyName;
public String getProjectLocation() {
return projectLocation;
}
public void setProjectLocation(String projectLocation) {
this.projectLocation = projectLocation;
}
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public String getRequestName() {
return requestName;
}
public void setRequestName(String requestName) {
this.requestName = requestName;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
}
and finally the service:
#ManagedBean(name="supplierRequestsService")
#SessionScoped
public class SupplierRequestsService {
#ManagedProperty("#{userManager}")
private UserManager usr;
public List<LeftGridBean> populateLeftGrid() {
List<LeftGridBean> lgbList = usr.getSupplierLeftGridData();
return lgbList;
}
public List<RightGridBean> populateRightGrid() {
List<RightGridBean> rgbList = usr.getSupplierRightGridData();
return rgbList;
}
public void setUsr(UserManager usr) {
this.usr = usr;
}
}

How to display a record in a dataTable after saving in database?

In SQL Server, I have a schema (ade) with some tables, which two of them are:
I'm using Eclipselink ORM and Primefaces 6.2; I have a CRUD for both tables, which works fine.
In a ListDemandas.jsf, I have a datatable where the user can insert a new Demandas registry by clicking in a button. Then, a Dialog (located in the
CreateDemandas.jsf) is displayed to input data in some fields. After clicking on a save button, such Dialog is hidden and the datatable is updated, showing this new Demandas record; the same action occurs to the Anotacoes entity, using a ListAnotacoes.jsf and a CreateAnotacoes.jsf .
What I want is to insert Anotacoes registries through the ListDemandas.jsf page and, right after saving that new Anotacoes registry, it would be displayed in the Demandas datatable (which is in the ListDemandas.jsf page).
What I achieved so far to do it: I put a button in the ListDemandas.jsf to invoke the Dialog (located in CreateAnotacoes.jsf). After clicking on a save button in such Dialog, this (Anotacoes) Dialog is hidden and the data is correctly persisted in the database.
The problem is that this new Anotacoes record is not being displayed in the Demandas datatable, even updating as below:
<p:commandButton actionListener="#{anotacoesController.saveNew}" value="Save" update="display,:DemandasListForm:datalist,:DemandasListForm:DemandasDataTableContextMenu,:growl" oncomplete="handleSubmit(xhr,status,args,PF('AnotacaoCreateDialog'));" >
The ListDemandas.jsf:
<h:form id="DemandasListForm">
<p:panel id="PanelListForm" header="#{adeBundle.ListDemandasTitle}">
<p:commandButton id="createButton" value="Create Demanda" update=":DemandasCreateForm" oncomplete="PF('DemandasCreateDialog').show()" actionListener="#{demandasController.prepareCreate}" />
<p:commandButton id="createAnotacaoButton" value="Create Anotacao" update=":AnotacaoCreateForm, datalist" oncomplete="PF('AnotacaoCreateDialog').show()" />
<p:dataTable id="datalist"
value="#{demandasController.items}"
lazy="false"
rowKey="#{item.id}"
var="item"
selection="#{demandasController.selected}"
widgetVar="demandasTable">
<p:ajax event="rowSelect" update="#form:createAnotacaoButton, #form:createButton" listener="#{demandasController.resetParents}"/>
<p:ajax event="rowUnselect" update="#form:createAnotacaoButton, #form:createButton" listener="#{demandasController.resetParents}"/>
<p:ajax event="contextMenu" update="#form:createAnotacaoButton, #form:createButton" listener="#{demandasController.resetParents}"/>
<p:column id="idCod" >
<f:facet name="header">
<h:outputText value="id"/>
</f:facet>
<h:outputText value="#{item.id}"/>
</p:column>
<!-- some columns ommited -->
<p:rowExpansion>
<p:panelGrid>
<p:column>
<p:row>
<p:accordionPanel value="Mais informacoes da Demanda" multiple="true">
<p:tab title="Anotacoes}" >
<p:dataList value="#{item.anotacoesCollection}" var="anotacoesCollectionItem" itemType="none" emptyMessage="-" >
<h:outputText value="#{anotacoesCollectionItem.anotacao.toString()}" />
</p:dataList>
</p:tab>
</p:accordionPanel>
</p:row>
</p:column>
</p:panelGrid>
</p:rowExpansion>
</p:dataTable>
</p:panel>
<ui:include src="/WEB-INF/include/confirmation.xhtml"/>
</h:form>
The CreateAnotacao.jsf:
<p:dialog id="AnotacaoCreateDlg" widgetVar="AnotacaoCreateDialog" modal="true" appendTo="#(body)" header="Create Anotacoes" >
<h:form id="AnotacaoCreateForm" >
<h:panelGroup id="display" >
<p:panelGrid columns="2" columnClasses="column">
<p:outputLabel value="CreateAnotacoesLabel_anotacao" for="anotacao" />
<h:panelGroup>
<p:inputTextarea id="anotacao" value="#{anotacoesController.selected.anotacao}" title="CreateAnotacoesTitle_anotacao" />
</h:panelGroup>
<p:outputLabel value="CreateAnotacoesLabel_date" for="date" />
<h:panelGroup>
<p:calendar id="date" value="#{demandasController.currentDate}" title="CreateAnotacoesTitle_date" />
</h:panelGroup>
<!-- some other input fields ommited -->
</p:panelGrid>
</h:panelGroup>
<p:commandButton actionListener="#{anotacoesController.saveNew}" value="Save" update="display, :DemandasListForm:datalist, :growl" oncomplete="handleSubmit(xhr,status,args,PF('AnotacaoCreateDialog'));" >
<p:confirm header="Confirmation" message="Are you sure ?" />
</p:commandButton>
<p:commandButton value="Cancel" oncomplete="PF('AnotacaoCreateDialog').hide()" update="display" process="#this" immediate="true" resetValues="true"/>
</h:form>
</p:dialog>
Excerpts of the AbstractController.java class:
public abstract class AbstractController<T> implements Serializable {
private static final long serialVersionUID = 1L;
private Class<T> itemClass;
private T selected;
private Collection<T> items;
private List<T> filteredItems;
private enum PersistAction {
CREATE,
DELETE,
UPDATE
}
public AbstractController() {
}
public AbstractController(Class<T> itemClass) {
this.itemClass = itemClass;
}
public void setSelected(T selected) {
if (selected != null) {
if (this.selected == null || !this.selected.equals(selected)) {
this.selected = this.ejbFacade.findWithParents(selected);
this.setChildrenEmptyFlags();
}
} else {
this.selected = null;
}
}
protected void setEmbeddableKeys() {
}
protected void initializeEmbeddableKey() {
}
public Collection<T> getItems() {
if (itemClass.getSimpleName().equals("Demandas")) {
if (sessionPrefixo == 9600) {
items = this.ejbFacade.findAll();
} else {
items = this.ejbFacade.findAllVenceHojeByNomeUorPos(uorPosDiageJurisdiciona);
}
}
return items;
}
public void save(ActionEvent event) {
String msg = ResourceBundle.getBundle("/adeBundle").getString(itemClass.getSimpleName() + "Updated");
persist(PersistAction.UPDATE, msg);
if (!isValidationFailed()) {
// Update the existing entity inside the item list
List<T> itemList = refreshItem(this.selected, this.items);
// If the original list has changed (it is a new object)
if (this.items != itemList) {
this.setItems(itemList);
}
// Also refresh the filteredItems list in case the user has filtered the DataTable
if (filteredItems != null) {
refreshItem(this.selected, this.filteredItems);
}
}
}
public void saveNew(ActionEvent event) {
String msg = ResourceBundle.getBundle("/adeBundle").getString(itemClass.getSimpleName() + "Created");
persist(PersistAction.CREATE, msg);
if (!isValidationFailed()) {
items = null; // Invalidate list of items to trigger re-query.
lazyItems = null; // Invalidate list of lazy items to trigger re-query.
}
}
private void persist(PersistAction persistAction, String successMessage) {
if (selected != null) {
this.setEmbeddableKeys();
try {
if (persistAction != PersistAction.DELETE) {
this.ejbFacade.edit(selected);
} else {
this.ejbFacade.remove(selected);
}
this.setChildrenEmptyFlags();
JsfUtil.addSuccessMessage(successMessage);
} catch (EJBException ex) {
Throwable cause = JsfUtil.getRootCause(ex.getCause());
if (cause != null) {
if (cause instanceof ConstraintViolationException) {
ConstraintViolationException excp = (ConstraintViolationException) cause;
for (ConstraintViolation s : excp.getConstraintViolations()) {
JsfUtil.addErrorMessage(s.getMessage());
}
} else {
String msg = cause.getLocalizedMessage();
if (msg.length() > 0) {
JsfUtil.addErrorMessage(msg);
} else {
JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
}
}
}
} catch (Exception ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/adeBundle").getString("PersistenceErrorOccured"));
}
}
}
public T prepareCreate(ActionEvent event) {
T newItem;
try {
newItem = itemClass.newInstance();
this.selected = newItem;
initializeEmbeddableKey();
return newItem;
} catch (InstantiationException | IllegalAccessException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public boolean isValidationFailed() {
return JsfUtil.isValidationFailed();
}
public String getComponentMessages(String clientComponent, String defaultMessage) {
return JsfUtil.getComponentMessages(clientComponent, defaultMessage);
}
#PostConstruct
public void initParams() {
Object paramItems = FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get(itemClass.getSimpleName() + "_items");
if (paramItems != null) {
setItems((Collection<T>) paramItems);
}
}
private List<T> refreshItem(T item, Collection<T> items) {
List<T> itemList;
if (this.items instanceof List) {
itemList = (List<T>) items;
} else {
itemList = new ArrayList<>(items);
}
int i = itemList.indexOf(item);
if (i >= 0) {
try {
itemList.set(i, item);
} catch (UnsupportedOperationException ex) {
return refreshItem(item, new ArrayList<>(items));
}
}
return itemList;
}
public AbstractFacade<T> getEjbFacade() {
return ejbFacade;
}
public void setEjbFacade(AbstractFacade<T> ejbFacade) {
this.ejbFacade = ejbFacade;
}
}
Excerpt of the AbstractFacade.java class:
[...]
public void edit(T entity) {
getEntityManager().merge(entity);
}
[...]
As mentioned in the comments, this is the reference for Demandas table:
Please, can someone help me to resolve it ?

Lazy DataTable rendering is very slow

Environment:
PrimeFaces 5.1
Mojarra 2.2.2
Spring 4.0.2
Problem:
I use a p:dataTable with lazy=true and I have implemented a LazyDataModel.
All works perfectly well (pagination, filtering, etc.), but it is very slow...
The load method is called, it performes the call to the DB and returns in 3sec max. But after that, it takes up to 20sec for the page to load (render?)...
For information, the page loads through ajax: a listener give informations to the LazyModel (setters), then change the page to insert (ui:insert, value stored in the controller), and finally update the form (update=":form").
After debugging it seems that I have no control on this...
Code:
<p:dataTable id="storeTable"
value="#{dashboard.stores}"
var="store"
lazy="true"
paginator="true" rows="30"
paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
paginatorPosition="bottom">
...
<!-- Columns -->
...
<c:forEach var="date" items="#{dashboard.days}">
<c:set var="day" value="#{store.getDay(date)}"/>
<p:column styleClass="store-day open-#{store.isAmOpen(date.date)} store-day-available-#{store.isAvailable(date.date)} #{store.storeKey} #{day.date.time} am"
width="30">
<h:outputText value=" " title="#{day.amComment}" />
<h:outputText styleClass="store" value="#{store.storeKey}" style="display: none" />
<h:outputText styleClass="date" value="#{day.date.time}" style="display: none" />
<h:outputText styleClass="am_pm" value="am" style="display: none" />
</p:column>
<p:column styleClass="store-day open-#{store.isPmOpen(date.date)} store-day-available-#{store.isAvailable(date.date)} #{store.storeKey} #{day.date.time} pm"
width="30">
<h:outputText value=" " title="#{day.pmComment}" />
<h:outputText styleClass="store" value="#{store.storeKey}" style="display: none" />
<h:outputText styleClass="date" value="#{day.date.time}" style="display: none" />
<h:outputText styleClass="am_pm" value="pm" style="display: none" />
</p:column>
</c:forEach>
</p:dataTable>
Relevant code of the controller:
#Controller("dashboard")
#Scope("session")
public class DashboardController implements Serializable {
private static final long serialVersionUID = 1971543164359627877L;
private LazyStoreWebModel stores;
// Some more attributes
#PostConstruct
public void postCtr() {
stores = new LazyStoreWebModel(restit);
update();
}
private void updateGroupList() {
groupList.clear();
groupList.addAll(groupDao.findByCountry(countryFilter));
}
public void updateStoreChainTypeList() {
storeChainTypeList.clear();
storeChainTypeList.addAll(storeTypeDao.findByCountry(countryFilter));
}
public void updateStoreZoneList() {
storeZoneList.clear();
storeZoneList.addAll(storeZoneDao.findByCountryAndChainType(
countryFilter, chainTypeFilter));
}
public void updateStoreRegionList() {
storeRegionList.clear();
if (storeZoneFilter != null) {
storeRegionList.addAll(storeRegionDao.findByCountryAndZone(
countryFilter, storeZoneFilter));
}
}
public void updateDays() {
days.clear();
days.addAll(calendar.generateListDateFromMonthAndYear(
yearFilter, monthFilter.getCode()));
stores.setYear(yearFilter);
stores.setMonth(monthFilter.getCode());
Collections.sort(days);
}
public void update() {
updateGroupList();
updateStoreChainTypeList();
updateStoreZoneList();
updateStoreRegionList();
updateDays();
stores.setCountry(countryFilter);
stores.setChainType(chainTypeFilter);
stores.setRegion(storeRegionFilter);
stores.setZone(storeZoneFilter);
}
public LazyDataModel<StoreWeb> getStores() {
return stores;
}
}
The LazyDataModel
public class LazyStoreWebModel extends LazyDataModel<StoreWeb> {
private static final long serialVersionUID = -4318420518897167924L;
private RestitService restit;
private Country country;
private StoreChainType chainType;
private StoreZone zone;
private StoreRegion region;
private Integer storeIntFlagFilter;
private Integer year;
private Integer month;
public LazyStoreWebModel(RestitService restit) {
this.restit = restit;
}
#Override
public StoreWeb getRowData(String key) {
try {
return restit.visualisationStoreById(key, year, month);
} catch (CalendarException e) {
e.printStackTrace();
} catch (NotExistStoreException e) {
e.printStackTrace();
}
StoreWeb sto = new StoreWeb();
sto.setStoreDesc("Should be null");
sto.setStoreKey("NULL:" + key);
return sto;
}
#Override
public Object getRowKey(StoreWeb sto) {
return sto.getStoreKey();
}
#Override
public List<StoreWeb> load(int first, int pageSize, String sortField,
SortOrder sortOrder, Map<String, Object> fieldFilters) {
List<StoreWeb> result = new ArrayList<StoreWeb>();
Map<String, Object> mainFilters = new HashMap<String, Object>();
mainFilters.put(RestitService.COUNTRY_KEY, country);
mainFilters.put(RestitService.CHAIN_TYPE_KEY, chainType);
mainFilters.put(RestitService.ZONE_KEY, zone);
mainFilters.put(RestitService.REGION_KEY, region);
mainFilters.put(RestitService.INT_FLAG_KEY, storeIntFlagFilter);
mainFilters.put(RestitService.YEAR_KEY, year);
mainFilters.put(RestitService.MONTH_KEY, month);
try {
result.addAll(restit.load(first, pageSize, sortField, sortOrder,
mainFilters, fieldFilters));
} catch (CalendarException e) {
e.printStackTrace();
} catch (NotExistStoreException e) {
e.printStackTrace();
}
setRowCount(restit.count(mainFilters, fieldFilters));
return result;
}
}
The restit attribute do the calls to the DB.
Any idea?
Thanks.
Edits:
Added relevant code of the controller
Added code of the LazyDataModel
Added code inside the HTML: Problem seems to come from the amount of columns (60+), more precisely from the quantity of attribute access done to build those.

Ajax Call get null value in backing bean in jsf

I have two SelectoneMenu and one inputText. within inputText I called one Ajax event(blur). But this ajax method does not get selected value from SelectoneMenu. But I called the same method using CommandButton it will get selected values and worked fine.
Here is My Code:
<h:form id=HarvestRateForm>
<table width="670px;">
<tr>
<td width="100px;"><p:outputLabel value="Crushing Season" for="crushingSeason"/></td>
<td width="80px;">
<p:selectOneMenu id="crushingSeason" style="width: 110px;" value="#{harvestRateBean.selectedSeason}">
<f:selectItem itemValue="#{null}" itemLabel="Select"/>
<f:selectItems value="#{harvestRateBean.seasons}"/>
</p:selectOneMenu><p:growl for="crushingSeason"/>
</td>
<td width="60px;" align="right"><p:outputLabel value="Plant" for="plant"/></td>
<td width="80px;"><p:selectOneMenu id="plant" style="width: 85px;" value="#{harvestRateBean.selectedPlant}">
<f:selectItem itemValue="#{null}" itemLabel="Select"/>
<f:selectItems value="#{harvestRateBean.plants}"/>
</p:selectOneMenu><p:growl for="plant"/></td>
<td width="60px;" align="right"><p:outputLabel value="Plot No" for="plotNo"/></td>
<td><p:inputText id="plotNo" value="#{harvestRateBean.sapPlotNo}" size="16">
<p:ajax event="blur" listener="#{harvestRateBean.loadPlotDetails}"
update="HarvestRateForm:plotNo HarvestRateForm:ryotCode"/>
</p:inputText>
<p:growl for="plotNo"/>
</td>
</tr>
<tr>
<td><p:outputLabel value="Ryot No" for="ryotCode"/></td>
<td><p:inputText value="#{harvestRateBean.ryotNo}" size="15" id="ryotCode" readonly="true" style="background: transparent;background-color: #cccccc"/></td>
My Bean Class:
public class HarvestRateBean implements Serializable {
private Map<String, String> seasons;
private Map<String, String> plants;
private String selectedPlant;
private String selectedSeason;
private String sapPlotNo;
private String ryotNo;
public HarvestRateBean() {
seasons = new HashMap<String, String>();
plants = new HashMap<String, String>();
}
public Map<String, String> getSeasons() {
List<Season> season_list = loadSeason();
for (Iterator<Season> it = season_list.iterator(); it.hasNext();) {
Season season1 = it.next();
seasons.put(season1.getSeason(), season1.getSeason());
}
return seasons;
}
public void setSeasons(Map<String, String> seasons) {
this.seasons = seasons;
}
public List<Season> loadSeason() {
Session session = HibernateUtil.getSessionFactory().openSession();
List<Season> seasonlist = null;
try {
seasonlist = session.createCriteria(Season.class).list();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
session.close();
HibernateUtil.getSessionFactory().close();
}
return seasonlist;
}
public Map<String, String> getPlants() {
List<Plant> plant_list = loadPlants();
for (Iterator<Plant> it = plant_list.iterator(); it.hasNext();) {
Plant plant = it.next();
plants.put(plant.getId().getPlant(), plant.getId().getPlant());
}
return plants;
}
public void setPlants(Map<String, String> plants) {
this.plants = plants;
}
public List<Plant> loadPlants() {
Session session = HibernateUtil.getSessionFactory().openSession();
List<Plant> plantlist = null;
try {
plantlist = session.createCriteria(Plant.class).list();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
session.close();
}
return plantlist;
}
public String getSelectedPlant() {
return selectedPlant;
}
public void setSelectedPlant(String selectedPlant) {
this.selectedPlant = selectedPlant;
}
public String getSelectedSeason() {
return selectedSeason;
}
public void setSelectedSeason(String selectedSeason) {
this.selectedSeason = selectedSeason;
}
public String getSapPlotNo() {
return sapPlotNo;
}
public void setSapPlotNo(String sapPlotNo) {
this.sapPlotNo = sapPlotNo;
}
public String getRyotNo() {
return ryotNo;
}
public void setRyotNo(String ryotNo) {
this.ryotNo = ryotNo;
}
public void loadPlotDetails {
Session session = HibernateUtil.getSessionFactory().openSession();
List<HarvesterRate> rateList = null;
FacesContext context = FacesContext.getCurrentInstance();
try {
if (getSelectedSeason() == null || getSelectedSeason().isEmpty()) {
context.addMessage(null, new FacesMessage("Season is required", ""));
} else if (getSelectedPlant() == null || getSelectedPlant().isEmpty()) {
context.addMessage(null, new FacesMessage("Plant is required", ""));
} else if (getSapPlotNo() == null || getSapPlotNo().isEmpty()) {
context.addMessage(null, new FacesMessage("Plot No is required", ""));
} else {
rateList = session.createCriteria(HarvesterRate.class).add(Restrictions.eq("id.season", getSelectedSeason())).add(Restrictions.eq("id.plant", getSelectedPlant())).add(Restrictions.eq("id.plotNo", getSapPlotNo())).list();
if (rateList.size() > 0) {
for (Iterator<HarvesterRate> it = rateList.iterator(); it.hasNext();) {
HarvesterRate harvesterRate = it.next();
setSapPlotNo(harvestRate.getPlotNo());
setRyotNo(harvestRate.getVendorCode());
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
getSelectedSeason() and getSelectedPlant() always return null in ajax Call but I Called same method using CommandButton it return Selected value and everything works fine.
The <p:ajax> will by default process only the parent ClientBehavior component, which is in your particular case the <p:inputText>. In other words, the actual value of <p:ajax process> is #this. If you actually want to process the entire form, then you should be using #form as in
<p:inputText ...>
<p:ajax process="#form" ... />
</p:inputText>
The process attribute of <p:commandButton> defaults to #form, that's why it works therewith.
Or, if you have more input components in the form and intend to process only the input and those two dropdowns, then you can specify the (relative) client IDs space separated:
<p:inputText ...>
<p:ajax process="crushingSeason plant #this" ... />
</p:inputText>
Implement ajaxEvent in bean class
Ajax event is not like normal method rather than it invokes the listeners as input, you can configure the Blur action on a text field as following
In JSF page
<p:inputText id="name" value="#{loginBean.name}">
<f:ajax event="blur" listener="#{loginBean.ajaxEvent}" />
</p:inputText>
Ensure that the above must be inside in <h:form>
In ManagedBean class
public void ajaxEvent(AjaxBehaviorEvent e)
{
//Do your stuffs
}
Know more about JSF start from here

primefaces dataTable with inputText

I have the following dataTable:
<p:dataTable var="indentifier" value="#{identifierBean.identifiers}">
<p:column>
<f:facet name="header">
<h:outputText value="#{indentifier.key}" />
</f:facet>
<h:outputText value="#{indentifier.key}" />
<p:inputText id="userValue" required="true" value="#{identifier.userValue}">
</p:inputText>
</p:column>
</p:dataTable>
and the commandButton
<p:commandButton
value="Search" ajax="false" action="#{identifierBean.saveIdentifiers}">
</p:commandButton>
The identifierBean is the following:
public class IdentifierBean {
private List<Identifier> identifiers;
private String country;
private List<Patient> patients;
public IdentifierBean() {
identifiers = new ArrayList<Identifier>();
}
public IdentifierBean(List<Identifier> identifiers) {
this.identifiers = identifiers;
}
public List<Identifier> getIdentifiers() {
return identifiers;
}
public void saveIdentifiers(){
try {
System.out.println("-"+this.identifiers.size()+"-");
} catch (Exception ex) {
//ex.printStackTrace();
}
}
public void setIdentifiers(List<Identifier> identifiers) {
this.identifiers = identifiers;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
Vector vec = Service.getCountryIdsFromCS(this.country);
for (int i = 0; i < vec.size(); i++) {
Identifier id = new Identifier();
id.setKey(Service.getPortalTranslation(
((SearchMask) vec.get(i)).getLabel(),
LiferayUtils.getPortalLanguage()));
id.setDomain("");
this.identifiers.add(id);
}
System.out.println("-"+this.identifiers.size()+"-");
}
public List<Patient> getPatients() {
return patients;
}
public void setPatients(List<Patient> patients) {
this.patients = patients;
}
}
The table has one row (that means that identifierBean has one element in identifiers list), but when I try to use the identifiers list in the saveIdentifiers method it is empty.
Any help?
Thank you very much!!!
RequestScope is too narrow. See BalusC's description of scopes.

Resources