当方法执行更新时刷新延迟加载的 Primefaces 数据表
Posted
技术标签:
【中文标题】当方法执行更新时刷新延迟加载的 Primefaces 数据表【英文标题】:refresh lazy loaded Primefaces datatable when a method performs an update 【发布时间】:2015-01-30 05:16:42 【问题描述】:当我通过 JDBC 方法更新数据库中的数据时,它会被持久化,但 primefaces 数据表不会被刷新。刷新页面也不会显示此更改,但仅当重新启动服务器并且再次调用我的 init 时才能看到此更改。在我使用这种 JDBC 方法保存数据后,有没有办法让 Primefaces 刷新它的数据表内容?
注意:JDBC 方法不同于为数据库实体执行添加/更新/删除的托管 bean。
我希望我的问题足够清楚。
这是我的数据表:
<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>
这是我的 JDBC 方法(如果需要)
public void updateWarehouse(double stockbalance, int id) throws SQLException
conman = new PremierConnection();
String sql = "update warehouse set quantity = ? where 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());
finally
if (stm != null)
stm.close();
if (conn != null)
conn.close();
更新: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;
【问题讨论】:
可以发warehouseManagedBean
的代码吗?
@PredragMaric 我刚刚做了
【参考方案1】:
问题可能是lazyModel
即使在您更新数据库后仍然有旧数据。更新库存后尝试再次调用init()
(是saveStock()
?),当然您应该在该操作完成后更新<p:dataTable>
组件(如果尚未设置,请添加dataTable
的ID到调用saveStock()
的按钮的update
属性。
【讨论】:
以上是关于当方法执行更新时刷新延迟加载的 Primefaces 数据表的主要内容,如果未能解决你的问题,请参考以下文章
当磁盘上的文件发生更改时,如何让 Emacs 自动刷新所有缓冲区?