JSF - How to get old and new value when using Primefaces RowEditEvent?

I have a datatable with 2 columns (name and description) and I want to update my database when a person updates a row and clicks a execBacking.update that runs the execBacking.update method.

In this method, I get the new values ​​using event.getObject() , but how can I get the old values? The name is my primary key, so I need the old value to find out which row is updated in db.

xhtml page

  <p:ajax event="rowEdit" listener="#{execBacking.update}" update=":execForm:execMessages" /> <p:column headerText="Name"> <p:cellEditor> <f:facet name="output"> <h:outputText value="#{exec.name}" /> </f:facet> <f:facet name="input"> <h:inputText value="#{exec.name}" /> </f:facet> </p:cellEditor> </p:column> <p:column headerText="Description"> <p:cellEditor> <f:facet name="output"> <h:outputText value="#{exec.description}" /> </f:facet> <f:facet name="input"> <h:inputText value="#{exec.description}" /> </f:facet> </p:cellEditor> </p:column> <p:column headerText="Actions"> <p:rowEditor /> <p:commandLink styleClass="ui-icon ui-icon-trash" type="submit" actionListener="#{execBacking.delete}" update=":execForm:execMessages" > <f:attribute name="execName" value="#{exec.name}" /> </p:commandLink> </p:column> </p:dataTable> 

Bean support

 public void update(RowEditEvent event) { Dao dao = new Dao(ds); Exec exec = (Exec) event.getObject(); System.out.println("name = "+exec.getName()); //New name System.out.println("desc = "+exec.getDescription()); //New desc try { // If the new exec was updated successfully if(dao.updateExec(accessBacking.getUsername(), null, exec.getName(), exec.getDescription())) { FacesContext.getCurrentInstance().addMessage("growl", new FacesMessage(FacesMessage.SEVERITY_INFO, "Success", "Exec Updated Successfully!")); } else { FacesContext.getCurrentInstance().addMessage("messages", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Error Updating Exec!")); } } catch (Exception e) { FacesContext.getCurrentInstance().addMessage("messages", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage())); } } 
+6
source share
2 answers

I would recommend not having name your primary key, but instead add the auto-incrementing id field and make your primary key, and not make this field editable by User. This ensures that you always have access to the record you want to update.

Generally speaking, it is bad practice to try to change the primary key in the database the way you are trying to do this.

+4
source

You can register a ValueChangeListener for the component you are interested in:

 <p:column headerText="Description"> <p:cellEditor> <f:facet name="output"> <h:outputText value="#{exec.name}" /> </f:facet> <f:facet name="input"> <h:inputText value="#{exec.name}"> <f:valueChangeListener binding="#{keyChangedListener}"/> <f:ajax/> </h:inputText> </f:facet> </p:cellEditor> </p:column> 

keyChangeListener will match the implementation of ValueChangeListener , and we use <f:ajax/> here to ensure that the valueChanged event valueChanged fired in the text box. Otherwise, the listener will not be notified. Your listener should look something like this:

 @ManagedBean(name="keyChangedListener") @ViewScoped public class KeyChangeListener implements ValueChangeListener{ public void processValueChange(ValueChangeEvent event) throws AbortProcessingException{ Object oldValue = event.getOldValue(); //get the old value Object newValue = event.getNewValue(); //get the new value } 

I am using @ViewScoped here, as recommended by the JSF Spec, to avoid unwanted side effects.

0
source

Source: https://habr.com/ru/post/956020/


All Articles