2017-10-14 36 views
0

我試圖在更新AJAX(觸發數據庫中的更新請求) 在我的程序中用戶單擊commandLink或更新後更新dataTable的內容數據庫中的「odm.valide_manag1」字段(使用The Datatable執行的請求通過不幸地測試此字段將圖像(項目符號)從紅色更改爲綠色,此階段未正確執行,測試始終保持未選中狀態(不更新領域的吸氣劑) 我調用在@PostConstruct的listodm對象的再生,在吸氣劑,在驗證方法中,我改變了範圍沒有成功爲什麼<h:dataTable>的內容沒有刷新afterb Ajax Action

@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; 
    } 

} 

的XHTML

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:c="http://java.sun.com/jsp/jstl/core"> 
<h:head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"></link> 
<title>Insert title here</title> 

</h:head> 
<h:body> 
    <ui:debug /> 

    <h:form id="formulaire1"> 
    <h:inputHidden value="#{listeOdm.n_odm}" /> 
    <h:inputHidden value="#{listeOdm.operation}" /> 
    <p:growl id="msgs" showDetail="true"/> 
    <h:dataTable id="basicDT" var="odm" value="#{listeOdm.listOdm}" binding="#{listeOdm.dataTable}"  > 

     <h:column headerText="Etat" id= "bulet"> 

     <h:panelGroup rendered="#{odm.valide_manag1 eq '2'}"> 
     <h:graphicImage library="img" name="if_bullet-green_44189.png" width="24" height="24" style="border-style: none"/> 
     </h:panelGroup> 
     <h:panelGroup rendered="#{odm.valide_manag1+odm.valide_manag2 eq '1'}"> 
      <h:graphicImage library="img" name="orange.gif" width="24" height="24" style="border-style: none"/> 
     </h:panelGroup> 
      <h:panelGroup rendered="#{odm.valide_manag1+odm.valide_manag2 eq '0'}"> 
      <h:graphicImage library="img" name="if_bullet-red_44191.png" width="24" height="24" style="border-style: none"/> 
     </h:panelGroup> 
     </h:column> 
      <h:column headerText="valid"> 
      <h:outputText value="#{odm.valide_manag1}" /> 
     </h:column> 
     <h:column headerText="Num Odm"> 
      <h:outputText value="#{odm.num_odm}" /> 
     </h:column> 
     <h:column headerText="Client"> 
      <h:outputText value="#{odm.client}" /> 
     </h:column> 
     <h:column headerText="Station"> 
      <h:outputText value="#{odm.station}" /> 
     </h:column> 
     <h:column headerText="Destination"> 
      <h:outputText value="#{odm.destination}" /> 
     </h:column> 
     <h:column headerText="date dep"> 
      <h:outputText value="#{odm.date_dep}" pattern="dd/MM/yyyy"/> 
     </h:column> 
     <h:column headerText="date retour"> 
      <h:outputText value="#{odm.date_ret}" pattern="dd/MM/yyyy"/> 
     </h:column> 
     <h:column headerText="Cient"> 
      <h:outputText value="#{odm.client}" /> 
     </h:column> 
     <h:column headerText="Edit" width="24"> 

       <h:commandLink  
        actionListener="#{listeOdm.editOdm}" update=":formulair1:basicDT" ajax="true"> 
        <f:param name="numOdm" value="#{odm.num_odm}"></f:param> 
        <f:param name="operation" value="edit"></f:param> 
        <h:graphicImage library="img" name="118805-128.png" width="24" height="24" style="border-style: none"/> 
        <!-- <i class="fa fa-envelope"></i> --> 

       </h:commandLink> 
       <h:commandLink  
        actionListener="#{listeOdm.printOdm}" update="formulaire1"> 
        <f:param name="numOdm" value="#{odm.num_odm}"></f:param> 
        <f:param name="operation" value="edit2"></f:param> 
        <h:graphicImage library="img" name="Print-2-128.png" width="24" height="24" style="border-style: none"/> 
        <!-- <i class="fa fa-envelope"></i> --> 
       </h:commandLink> 
     </h:column> 

    </h:dataTable> 

    </h:form> 

</h:body> 
</html> 

JSF2.2.9的Tomcat 8.5

+1

你能做出正確的句子?這太難以閱讀了。 –

+0

對不起,我說法語 –

+0

我試圖在AJAX調用後更新dataTable的內容 –

回答

0

我已經解決了我的問題!!!!! 我想與初學者分享這種體驗,因爲對於專家來說顯而易見。 DataTable的行是對象,執行列表程序或命令<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的更新我在DB更新記錄這樣

​​

正確的方法是:

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(); 
} 
+0

嗨,有沒有任何全球性的解決方案,數據庫中的對象和記錄同時更新? –