为什么在Ajax Action之后不刷新<h:datatable>的内容

时间:2017-10-14 08:27:52

标签: ajax jsf primefaces

我正在尝试在更新AJAX后更新dataTable的内容(在DB上触发更新请求) 在我的程序中,用户点击commandLink或更新数据库中的“odm.valide_manag1”字段(请求执行时,数据表通过测试此字段将图像(项目符号)从红色更改为绿色,此阶段未正确执行测试始终保持未选中状态(不更新字段的getter) 我在@PostConstruct中调用了listodm对象的重生,在Getter中,在验证方法中,我改变了Scope而没有成功

/Library/Application Support

Xhtml

@ManagedBean 
@ViewScoped // or RequestScoped
public class listeOdm {
    public static Logger log=  LogManager.getLogger(listeOdm.class.getName());
    private List<odm> listOdm= new ArrayList<odm>() ; // a changer vers ListDataModel

    private odmService odmservice = new odmServiceImpl();
    FacesContext fc = FacesContext.getCurrentInstance();
    HttpSession httpsession = (HttpSession) fc.getExternalContext().getSession(false);
    private String n_odm;
    private String operation;
    private HtmlDataTable dataTable;



    @PostConstruct
    public void initMyBean()
    {   listOdm=null;
        log.info("1- debut PostConstruct   :   "  );         
        listOdm = odmservice.findByMatricule("1000"); /*(String) httpsession.getAttribute("Matricule")*/
        //listOdm = odmservice.findAll();
        log.info("1- PostConstruct NOMBRE DES ODM  :   " + listOdm.size()+ "   The Object Is "+ listOdm.hashCode());
        RequestContext.getCurrentInstance().update("basicDT");
        /*  if("edit".equals(getParam("operation")))
        {
            editOdm();
        }*/
    }

    public String getParam(String name)
    {  
        Map<String, String> params=fc.getExternalContext().getRequestParameterMap();
        return params.get(name);    
    }
    public void onValid_manag(RowEditEvent event)
    {
        odmservice.validManager(((odm) event.getObject()).getNum_odm());
       FacesMessage msg = new FacesMessage("Odm validée", ((odm) event.getObject()).getNum_odm());
       FacesContext.getCurrentInstance().addMessage(null, msg);
       // ((odm) event.getObject() ).setValide_manag1("2");
       // listOdm.clear();
       // listOdm = odmservice.findAll(); 
       listOdm = odmservice.findByMatricule("1000");
       log.info("2- on valid Method la nouvelle valeur de Valide_manage1 est :" + ((odm) event.getObject()).getValide_manag1());
       log.info("2- on valid Method  :   " + listOdm.size()+ "   The Object Is "+ listOdm.hashCode());
        RequestContext.getCurrentInstance().update("basicDT");
    }

        public void onCancel_manag(RowEditEvent event) {
              FacesMessage msg = new FacesMessage("Odm Non validée", ((odm) event.getObject()).getNum_odm());
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    public void editOdm()
    {  // setN_odm(getParam("numOdm"));
       // setOperation(getParam("operation"));
        log.info("0- edit methode  :     "+getParam("numOdm"));
     odmservice.validManager(getParam("numOdm"));
        //listOdm = odmservice.findByMatricule("1000"); /*(String) httpsession.getAttribute("Matricule")*/
        log.info("edit de l'ODM  N°:   ");
       listOdm = odmservice.findByMatricule("1000");
    }
    public void modif()
    {    log.info("on Expand Start ");
         listOdm = odmservice.findByMatricule("1000");
    }
    public void printOdm()
    {}
    public List<odm> getListOdm() {
        log.info("getter list odm"+this.listOdm.hashCode());
        if (FacesContext.getCurrentInstance().getRenderResponse()) {
            log.info("getter list odm"+this.listOdm.hashCode());
            listOdm = odmservice.findByMatricule("1000");
        }
        return listOdm ;//= odmservice.findByMatricule("1000");
    }

    public void setListOdm(List<odm> listOdm) {
        this.listOdm=listOdm ;
    }

    public String getN_odm() {
        return n_odm;
    }

    public void setN_odm(String n_odm) {
        this.n_odm = n_odm;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public HtmlDataTable getDataTable() {
         dataTable.clearInitialState();
        return dataTable;
    }

    public void setDataTable(HtmlDataTable dataTable) {
        this.dataTable = dataTable;
    }

}

JSF2.2.9 Tomcat 8.5

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题!!!!! 我想与初学者分享这种经验,因为对于专家来说这很明显。 DataTable的行是Objects,listner的执行或命令<p:ajax event="rowEdit" listener="#{listeOdm.onValid_manag}" update=":formulaire1:basicDT"/>  更新数据库中的记录而不更新数据表行。

public void onValid_manag(RowEditEvent event)
{
   odmservice.validManager(((odm) event.getObject())); // herer was my error, before i passed a string  instead of the object  event.getObject().getNum_Odm
   listOdm = odmservice.findByMatricule("1000");   
}

并且在调用Hibernate Update的方法中,我更新了DB中的记录,如此

public void validManager(String num_odm)
  {

     session.beginTransaction();
     Query q = session.createQuery("update odm set valide_manag1='2' where num_odm = :num_odm");
     q.setParameter("num_odm",num_odm);
     q.executeUpdate();
      session.getTransaction().commit();
    }

正确的方法是:

public void validManager(odm odm) { //object in the selected row as parameter
     session.beginTransaction();
// updating the object in memory  
     odm.setValide_manag1("2");
//hibernate execute the update query  in DB
     session.save(odm);
     session.getTransaction().commit();
}