I've a data table with editMode="cell". My problem is when I edit a cell (in either one of the editable columns), submit it, go to the listener method, and try to obtain the modified value using:
event.getNewValue()
It's not reflecting the edited value. It actually always returns the old value. Consequently the bean doesn't get updated. Any clues of what I'm doing wrong? I'm using JavaServer Faces 2.2, Primefaces 5.0 and Spring Framework 4.0.3.
Thanks for any help.
Here's the XHTML code:
<p:dataTable id="nieoTable" var="nieo" value="#{nieoController.nieos}"
editable="true" editMode="cell" widgetVar="cellNieo"
selectionMode="single" selection="#{nieoController.selectedNieo}"
rowKey="#{nieo.nieoNumber}" tableStyle="width:auto">
<p:ajax event="rowSelect" update=":nieoForm:wasIsGrid :nieoForm:paperModsumGrid :nieoForm:effectivityGrid" />
<p:ajax event="cellEdit" listener="#{nieoController.onEditNieo}" update=":nieoForm:nieoTable :nieoForm:growl" />
<p:column headerText="Nieo Number">
<h:outputText value="#{nieo.nieoNumber}" />
</p:column>
<p:column headerText="PLM Action Number">
<p:cellEditor>
<f:facet name="output">
<h:outputText id="plmActionNumberOutput" value="#{nieo.plmActionNumber}" />
</f:facet>
<f:facet name="input">
<h:inputText id="plmActionNumberInput" value="#{nieo.plmActionNumber}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="SAP Change Master">
<p:cellEditor>
<f:facet name="output">
<h:outputText id="sapChangeMasterOutput" value="#{nieo.sapChangeMaster}" />
</f:facet>
<f:facet name="input">
<h:inputText id="sapChangeMasterInput" value="#{nieo.sapChangeMaster}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Command">
<p:commandButton title="Remove NIEO" icon="ui-icon-trash"
actionListener="#{nieoController.deleteNieo(nieo)}"
update=":nieoForm:nieoTable :nieoForm:growl" />
</p:column>
</p:dataTable>
Here's the code for the managed bean (controller):
public class NieoController {
@Autowired
private NIEOService nieoService;
public void onEditNieo(CellEditEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
if(newValue != null && !newValue.equals(oldValue)) {
FacesContext context = FacesContext.getCurrentInstance();
NIEO nieo = context.getApplication().evaluateExpressionGet(context, "#{nieo}", NIEO.class);
nieoService.updateNieo(nieo);
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Nieo updated", "Old: " + oldValue + ", New:" + newValue);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
I found the problem with my code. In my managed bean I had the getter for the list of nieos, but not the actual list. Of course it would never work like this. My list was never kept in the session. So, I had this (without the actual list):
@Controller
@ManagedBean
@SessionScoped
public class NieoController {
...
@Autowired
private NIEOService nieoService;
...
public List<NIEO> getNieos(){
return nieoService.listNieos();
}
...
Now I have this:
@Controller
@ManagedBean
@SessionScoped
public class NieoController {
...
@Autowired
private NIEOService nieoService;
...
public List<NIEO> nieos;
public List<NIEO> getNieos(){
if (nieos == null)
nieos = nieoService.listNieos();
return this.nieos;
}
...
Finally, I was forgetting to give my managed bean something to hold on to (if you get what I mean, ;) ). Now it works! Thank you guys for all help. Sometimes we get stuck on the simplest things.