gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:30:22 25 4
gpt4 key购买 nike

当我从 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();
}
}
}

更新:仓库管理bean
@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;
}

}

最佳答案

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

关于jsf-2 - 当方法执行更新时刷新延迟加载的 Primefaces 数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27241293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com