PrimeFaces Datatable CellEditor - Different values for facet input and output -
i have editable datatable entity user. table has field called username has unique constraint
<h:form id="form"> <p:datatable id="usertable" value="#{users}" var="entity" editable="true" editmode="cell" tablestyle="width:auto"> <p:ajax event="celledit" listener="#{usercontroller.oncelledit}" update="form:msgs form:display form:usertable" /> <p:column headertext="userid"> <p:outputlabel value="#{entity.id}" /> </p:column> <p:column headertext="username"> <p:celleditor> <f:facet name="output"><p:outputlabel for="username" value="#{entity.username}" /></f:facet> <f:facet name="input"> <p:inputtext id="username" value="#{entity.username}" > <f:validaterequired /> <f:validatelength maximum="50" /> </p:inputtext> </f:facet> </p:celleditor> </p:column> </p:datatable> </h:form>
in backing bean usercontroller, have following method oncelledit
public void oncelledit(celleditevent event) { object newvalue = event.getnewvalue(); string columnname = event.getcolumn().getheadertext(); facescontext = facescontext.getcurrentinstance(); user entity = facescontext.getapplication().evaluateexpressionget(facescontext, "#{entity}", user.class); if(columnname.equals("username") { entity.setusername((string) newvalue); } try { service.createorupdateentity(entity); } catch (ejbtransactionrolledbackexception ex) { throwable t = ex.getcause(); while (t != null) { t = t.getcause(); if(t.getclass().getsimplename().equals("sqlintegrityconstraintviolationexception")) { entity.setusername((string) event.getoldvalue()); facescontext.addmessage(null, new facesmessage(facesmessage.severity_warn, "duplicate entry field username. write different username", "")); break; } } return; } catch (exception e) { e.printstacktrace(); }
the problem when exception catched final value facet input different value facet output. in case facet output shows right value while facet input shows value produced exception.
you need ensure uiinput component has been reset original values. uiinput component can retrieved celleditevent
.
((uiinput)event.getcomponent()).resetvalue();
the above assumes you've checked return getcomponent()
null , type safety. here's api uiinput
Comments
Post a Comment