当方法执行更新时刷新延迟加载的Primefaces数据表

时间:2014-12-02 03:20:10

标签: jsf-2 primefaces datatable

当我从JDBC方法更新数据库中的数据时,它会保持不变,但是primefaces数据表不会刷新。刷新页面也不显示此更改,但只有在重新启动服务器并再次调用init时才会看到此更改。有没有办法让Primefaces在使用这种JDBC方法保存数据后刷新它的数据表内容?

注意: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;
    }    

}

1 个答案:

答案 0 :(得分:1)

问题可能是lazyModel在更新数据库后仍然有旧数据。更新库存后,请尝试再次调用init()saveStock()?),当然,您应该在该操作完成后更新<p:dataTable>组件(如果尚未设置,请添加dataTable调用update按钮的saveStock()属性的{id}}。