2014-05-12 40 views
0

我正在使用以下代碼來編輯記錄的值。代碼工作正常。如何在完成數據庫更新後在jsf頁面顯示消息

<rich:popupPanel header="Edit Company Region" id="editCompanyRegionPane" 
          domElementAttachment="parent" width="230" height="115"> 
          <h:form> 
          <h:panelGrid columns="3" id="editCompanyRegionGrid"> 
           <h:inputHidden value="#{CompanyAdminPageModel.editCompanyRegion.companyRegionId}" 
            id="editcompanyRegionId"> 
           </h:inputHidden> 
           <h:message for="editcompanyRegionId" /> 
           <h:column>       
           <h:outputText value="Name " /> 
           <h:inputText value="#{CompanyAdminPageModel.editCompanyRegion.name}" 
            id="editCompanyRegionName"> 
           </h:inputText> 
           <h:message for="editCompanyRegionName" /> 
           </h:column> 
          </h:panelGrid> 

          <h:commandButton value="Update" 
           action="#{CompanyAdminPageModel.companyRegionUpdate}" render="table" 
           oncomplete="if (#{facesContext.maximumSeverity==null}) {#{rich:component('editCompanyRegionPane')}.hide();}" /> 
          <h:commandButton value="Cancel" 
           onclick="#{rich:component('editCompanyRegionPane')}.hide(); return false;" /> 
          </h:form> 
     </rich:popupPanel> 

我想在更新數據庫中的值後在同一頁中顯示一條消息。例如:當我點擊更新按鈕時,它將調用Bean類的方法,然後更新數據庫中的值,關閉彈出面板後,它將在同一頁面內顯示一條消息(如:更新成功)。

請任何幫助。

我用follwoing Java代碼:

public String updateUser(Long usrId, String firstName, String lastName, 
     String loginName, String emailAddr) { 
    AllCompanyDAO aDAO = new AllCompanyDAO(); 
    FacesContext.getCurrentInstance().addMessage("test", new javax.faces.application.FacesMessage("Success")); 
    return aDAO.userUpdate(usrId, firstName, lastName, loginName, emailAddr); 

} 

和以下JSF代碼:

<div class="content container"> 
     <div style="padding-left:220px;"> 
      To see the relationships between users and regions <a href="#{appPath}/app/insertion/userRegionInfo.faces" >click here</a> <br/> 
      <h:message for="test"></h:message> 
     </div> 
    </div> 
+0

您可以使用'FacesContext.getCurrentInstance()。addMessage(clientId,new FacesMessage(「Success」));'將消息放入FacesContext。只需要一個'h:message'來顯示消息。參見[FacesContext api](http://docs.oracle.com/javaee/7/api/javax/faces/context/FacesContext.html#addMessage%28java.lang.String,%20javax.faces.application.FacesMessage%29 ) –

+0

你能告訴我,我在哪裏以及如何在我的代碼中使用它? – Novis

+0

只需將它放在更新偵聽器方法的末尾即可。如果更新成功,請將消息添加到FacesContext。該消息應該顯示你曾經擁有'h:message'的地方 –

回答

0

在的companyRegionUpdate()結束時,添加:

  1. 消息

    FacesContext.getCurrentInstance().addMessage("test", new FacesMessage("Success"));

測試是絕對參照<h:message>JSF組件。

  1. 要將<h:commandButton>,添加Ajax支持

<h:commandButton value="Update" > 
<a4j:support oncomplete="if (#{facesContext.maximumSeverity==null}) {#{rich:component('editCompanyRegionPane')}.hide();}" action="#{CompanyAdminPageModel.companyRegionUpdate}" event="onclick" reRender="test, table" /> 
</h:commandButton> 

測試是絕對的參考<h:message>JSF組件。

您可以簡單地使用具有ajax支持的<a4j:commandButton >組件,而不是向您的<h:commandButton>添加ajax支持。

0

我用下面的代碼解決了我的問題。 1.在java的companyRegionUpdate方法中使用以下代碼。

FacesContext.getCurrentInstance().addMessage("test", new javax.faces.application.FacesMessage("Successful")); 

然後在我想要的div中使用<h:messages /><h:messages /><h:message />不同。在我的情況下,<h:messages />工程。

就是這樣。 :-)

相關問題