jsf 2 - refresh lazy loaded Primefaces datatable when a method performs an update -
when update data in database jdbc method, persisted primefaces datatable doesn't refreshed. refreshing page doesn't show change either change seen when server restarted , init has been called again. there way make primefaces refresh it's datatable content after persist data using jdbc method?
note: jdbc method different managed bean add/update/delete database entity.
i hope question clear enough.
here datatable:
<p:datatable value="#{warehousemanagedbean.lazymodel}" var="showstock" widgetvar="warehousetable" rows="10" rowsperpagetemplate="20,30,50" paginator="true" lazy="true" paginatortemplate="{currentpagereport} {firstpagelink} {previouspagelink} {pagelinks} {nextpagelink} {lastpagelink} {rowsperpagedropdown}"> <p:column headertext="raw material" sortby="#{showstock.rwcode}" filterby="#{showstock.rwcode}" filterstyle="display:none; visibility:hidden;"> <h:outputtext value="#{showstock.rwcode}"/> </p:column> <p:column headertext="stock balance"> <h:outputtext value="#{showstock.quantity}"/> </p:column> <p:column headertext="cost"> <h:outputtext value="#{showstock.cost}"/> </p:column> <p:column headertext="value"> <h:outputtext value="#{showstock.stockvalue}"><f:convertnumber type="currency" currencysymbol="$"/></h:outputtext> </p:column> <p:column rendered="false"> <h:outputtext value="#{showstock.stockid}"/> </p:column> </p:datatable>
here jdbc method(if needed)
public void updatewarehouse(double stockbalance, int id) throws sqlexception{ conman = new premierconnection(); string sql = "update warehouse set quantity = ? stockid = ?"; connection conn = null; preparedstatement stm = null; try { conn = conman.getdbconnection(); stm = conn.preparestatement(sql); stm.setdouble(1, stockbalance); stm.setint(2, id); stm.executeupdate(); } catch (sqlexception e) { system.out.println(e.getmessage()); } { if (stm != null) { stm.close(); } if (conn != null) { conn.close(); } } }
update: warehousemanagedbean
@named(value = "warehousemanagedbean") @viewscoped public class warehousemanagedbean implements serializable{ private lazydatamodel<warehouse> lazymodel; private warehouse stock = new warehouse(); @ejb private warehouseejb ejb; @postconstruct public void init(){ lazymodel = new lazywarehousedatamodel(ejb); } public string newstock(){ try { stock = ejb.create(stock); jsfutil.addsuccessmessage("stock created successfully"); } catch (ejbexception e){ jsfutil.adderrormessage("raw material exists"); return "addstock.xhtml"; } catch (exception e) { logger.getlogger(warehousemanagedbean.class.getname()).log(level.severe, "error creating new stock", e); } return "warehouse.xhtml"; } public string newnextstock(){ try { stock = ejb.create(stock); jsfutil.addsuccessmessage("stock created successfully"); } catch (ejbexception e){ jsfutil.adderrormessage("raw material exists"); } catch (exception e) { logger.getlogger(warehousemanagedbean.class.getname()).log(level.severe, "error creating new stock", e); } return "addstock.xhtml"; } public string savestock(){ try { ejb.edit(stock); jsfutil.addsuccessmessage("stock updated successfully"); } catch (ejbexception e){ jsfutil.adderrormessage("error updating stock"); return "editstock.xhtml"; } catch (exception e) { logger.getlogger(warehousemanagedbean.class.getname()).log(level.severe, "error updating stock", e); } return "warehouse.xhtml"; } public void deletestock(warehouse stock){ try { ejb.remove(stock); jsfutil.addsuccessmessage("stock deleted successfully"); } catch (ejbexception e){ jsfutil.adderrormessage("error deleting stock"); }catch (exception e) { logger.getlogger(warehousemanagedbean.class.getname()).log(level.severe, "error deleting stock", e); } } public warehouse getstock() { return stock; } public list<warehouse> getwarehouselist() { return warehouselist; } public lazydatamodel<warehouse> getlazymodel() { return lazymodel; } }
the problem lazymodel
still has old data after update database. try calling init()
again after update stock (is savestock()
?), , of course should update <p:datatable>
component after action finished (if not set up, add datatable
's id update
attribute of button calling savestock()
).
Comments
Post a Comment