【问题标题】:dataTable does not show newly added item after save保存后dataTable不显示新添加的项目
【发布时间】:2015-09-17 07:30:44
【问题描述】:

我有一个使用 primefaces 和 JSF 的简单表单。单击保存时,它会显示另一个 jsf 页面,其中我有一个 primefaces 数据表。它应该在数据表中显示新创建的记录,但事实并非如此。为了查看新记录,我必须返回另一个页面,然后重新打开我有数据表的页面。我可以看到新记录。如何解决这个问题,以便在点击保存时我可以看到新记录?

    <p:dataTable id="dta" value="#{CarComponent.listCars}"  var="current" rows="15" paginator="true" paginatorPosition="bottom">
    </dataTable>


     //method where listCars is populated
     public String carLists(){
        listCars = new arrayList(carDao.findAll());
        return "/jsf/listOfcars.xhtml";
     }

     //save method - on clicking save it should display the datatable

      public String save(Cars car){
        carService.save(car);
       return  "/jsf/listOfcars.xhtml";
       }


      public String getlistCars() {
          return listCars;
       }

      public void setlistCars(String listCars) {
          this.listCars = listCars;
      }

【问题讨论】:

  • 您确定在保存之前/之后已将汽车添加到 carList 中吗?
  • 请 - 作为最佳实践 - 提供相关库的版本,在这种情况下,例如JSF 和 PrimeFaces。
  • primefaces 5.0 和 jsf 2.0

标签: jsf jsf-2 datatable


【解决方案1】:

我只能猜测,但我假设CarService 类至少是ViewScoped 甚至SessionScoped?无论哪种方式,正如弗里茨在评论中所说,汽车列表显然没有更新,因为视图没有重新渲染。

如果 bean 是 @ViewScoped,您可以使用 return "/jsf/listOfCars.xhtml?faces-redirect=true" 进行重定向,或者您必须将新生成的汽车添加到汽车列表中

public String saveCar(Car car) {
    carService.save(car);
    listCars.add(car);
    return "/jsf/listOfCars.xhtml";
}

【讨论】:

  • bean 是:@Scope("session") @Component("CarComponent")
  • 那么,您是否尝试过将新的/保存的汽车添加到 bean 的后备列表中?
  • 我试了一下,效果很好。但这是一个好的解决方案还是只是一种解决方法?性能怎么样?我找到了另一种解决方法:在 listCars 的 getter 方法中,我添加了这一行: listCars = new arrayList(carDao.findAll());它完成了这项工作。但我意识到每次数据表都需要在数据库中进行调用,这不是很好的性能。我说的对吗?
  • 这至少是不错的做法,保存后将其添加到前端视图模型中。您的想法不好,原因是:性能。您必须自己在两个模型(后端和前端模型)之间保持一些一致性,但这应该可以处理。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 2020-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
相关资源
最近更新 更多