2015-01-04 84 views
0

我在JSF中閱讀了很多關於ajax的頁面和代碼,但我仍然對此有疑問。 第一次commandbutton調用ajax,第二次不起作用。我不明白如何使用Ajax。 例如:JSF中的Ajax:在commandbutton中調用ajax

<h:commandButton styleClass="buttonGeneral" value="#{msg.update}"> 
    <f:ajax render="@form" execute="@all" listener="#{userInformationController.updateMain}"/> 
</h:commandButton> 

的,哪些是不叫方法:

public void updateMain(AjaxBehaviorEvent event){ 
    System.out.println("updateMain(event): done"); 
    queries.updateMainUserInformation(tUser); 
} 

public void updateMain(ActionEvent event){ 
    System.out.println("updateMain(actionevent): done"); 
    queries.updateMainUserInformation(tUser); 
} 

public void updateMain(){ 
    System.out.println("updateMain(): done"); 
    queries.updateMainUserInformation(tUser); 
} 

爲什麼第一次是不叫? 全.xhtml代碼:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ace="http://www.icefaces.org/icefaces/components" 
    xmlns:icecore="http://icefaces.org/tld/core" 
    template="/WEB-INF/templates/template.xhtml"> 

    <ui:define name="logedContent"> 
      <center><h:outputLabel value="#{msg.main_information}"/></center> 
       <h:form id="form1"> 
        <table class="tableGeneral"> 
         <tr> 
          <td><h:outputLabel value="#{msg.user}" /></td> 
          <td><h:inputText id="login" 
            value="#{userInformationController.tUser.login}"> 
            <f:validator validatorId="DefaultStringValidator" /> 
           </h:inputText></td> 
          <td><h:message styleClass="error" for="login" /></td> 
         </tr> 
         <tr> 
          <td><h:outputLabel value="#{msg.forename}" /></td> 
          <td><h:inputText id="forename" 
            value="#{userInformationController.tUser.forename}"> 
           </h:inputText></td> 
          <td><h:message styleClass="error" for="forename" /></td> 
         </tr> 
         <tr> 
          <td><h:outputLabel value="#{msg.surename}" /></td> 
          <td><h:inputText id="surename" 
            value="#{userInformationController.tUser.surename}"> 
           </h:inputText></td> 
          <td><h:message styleClass="error" for="surename" /></td> 
         </tr> 
         <tr> 
          <td><h:outputLabel value="#{msg.job}" /></td> 
          <td><h:inputText id="job" 
            value="#{userInformationController.tUser.job}"> 
            <f:validator validatorId="DefaultStringValidator" /> 
           </h:inputText></td> 
          <td><h:message styleClass="error" for="job" /></td> 
         </tr> 
         <tr> 
          <td><h:outputLabel value="#{msg.birthplace}" /></td> 
          <td><h:inputText id="birthplace" 
            value="#{userInformationController.tUser.birthplace}"> 
            <f:validator validatorId="DefaultStringValidator" /> 
           </h:inputText></td> 
          <td><h:message styleClass="error" for="birthplace" /></td> 
         </tr> 
         <tr> 
          <td><h:outputLabel value="#{msg.birthday}" /></td> 
          <td><ace:dateTimeEntry id="birthday" 
            value="#{userInformationController.tUser.birthdate}" 
            timeZone="Canada/Mountain" renderAsPopup="true" 
            pattern="yyyy-MM-dd" required="true"> 
           </ace:dateTimeEntry></td> 
          <td><h:message styleClass="error" for="birthday" /></td> 
         </tr> 
         <tr> 
          <td><h:outputLabel value="#{msg.activated}" /></td> 
          <td><h:selectBooleanCheckbox id="activated" value="#{userInformationController.tUser.activated}"/></td> 
          <td><h:message styleClass="error" for="activated" /></td> 
         </tr> 
         <tr> 
          <td><h:outputLabel value="#{msg.pesel}" /></td> 
          <td><h:inputText id="pesel" 
            value="#{userInformationController.tUser.number}"> 
            <f:validator validatorId="DefaultStringNumberValidator" /> 
           </h:inputText></td> 
          <td><h:message styleClass="error" for="pesel" /></td> 
         </tr> 
         <tr> 
          <td colspan="3"> 
           <!-- 
           <h:commandButton styleClass="buttonGeneral" value="#{msg.update}"> 
            <f:ajax event="click" render="form1" listener="#{userInformationController.updateMain}"/> 
           </h:commandButton> 
           --> 
           <h:commandButton styleClass="buttonGeneral" value="#{msg.update}" action="null"> 
            <f:ajax listener="#{userInformationController.updateMain}"/> 
           </h:commandButton> 
           <h:commandButton styleClass="buttonGeneral" value="#{msg.update}"> 
            <f:ajax render="@form" execute="@all" listener="#{userInformationController.updateMain}"/> 
           </h:commandButton> 
           <h:commandButton styleClass="buttonGeneral" value="#{msg.update}"> 
            <f:ajax render="@all" execute="@form" listener="#{userInformationController.updateMain}"/> 
           </h:commandButton> 
           <h:commandButton styleClass="buttonGeneral" value="#{msg.update}"> 
            <f:ajax render="@all" execute="@all" listener="#{userInformationController.updateMain}"/> 
           </h:commandButton> 
          </td> 
         </tr> 
        </table> 
       </h:form> 
       <!-- 
       <h:form> 
       <hr/> 
        <center><h:outputLabel value="#{msg.permissions}"/></center> 
        <center> 
        <table class="tableGeneral"> 
         <tr> 
          <td><h:outputLabel value="#{msg.adding_project}" /></td> 
          <td><h:selectBooleanCheckbox value="#{userInformationController.tUser.userPermissions.addProjects}" /></td> 
         </tr> 
         <tr> 
          <td><h:outputLabel value="#{msg.removing_project}" /></td> 
          <td><h:selectBooleanCheckbox value="#{userInformationController.tUser.userPermissions.removeProjects}" /></td> 
         </tr> 
         <tr> 
          <td><h:outputLabel value="#{msg.closing_project}" /></td> 
          <td><h:selectBooleanCheckbox value="#{userInformationController.tUser.userPermissions.closeProjects}" /></td> 
         </tr> 
         <tr> 
          <td><h:outputLabel value="#{msg.checking_project}" /></td> 
          <td><h:selectBooleanCheckbox value="#{userInformationController.tUser.userPermissions.checkProjects}" /></td> 
         </tr> 
         <tr> 
          <td><h:outputLabel value="#{msg.evaluating_project}" /></td> 
          <td><h:selectBooleanCheckbox value="#{userInformationController.tUser.userPermissions.evaluateProjects}" /></td> 
         </tr> 
         <tr> 
          <td><h:outputLabel value="#{msg.adding_people}" /></td> 
          <td><h:selectBooleanCheckbox value="#{userInformationController.tUser.userPermissions.addPeople}" /></td> 
         </tr> 
         <tr> 
          <td><h:outputLabel value="#{msg.removing_people}" /></td> 
          <td><h:selectBooleanCheckbox value="#{userInformationController.tUser.userPermissions.removePeople}" /></td> 
         </tr> 
         <tr> 
          <td><h:outputLabel value="#{msg.checking_people}" /></td> 
          <td><h:selectBooleanCheckbox value="#{userInformationController.tUser.userPermissions.checkPeople}" /></td> 
         </tr> 
         <tr> 
          <td><h:outputLabel value="#{msg.evaluating_people}" /></td> 
          <td><h:selectBooleanCheckbox value="#{userInformationController.tUser.userPermissions.evaluatePeople}" /></td> 
         </tr> 
        </table> 
        <h:commandButton styleClass="buttonGeneral" value="#{msg.update}" > 
          <f:ajax event="click" execute="@form" listener="#{userInformationController.updatePermissions}"/> 
        </h:commandButton> 
        </center> 
       </h:form> 
       --> 
    </ui:define> 
</ui:composition> 
+0

參考文獻''中的'execute =「@ all」'(或'render =「@ all」')受到嚴重阻礙(真的非常罕見或者可能沒有任何理智所有的用例都是這樣)。儘量避免它。這就像是發送一個純粹的同步請求。 – Tiny 2015-01-04 20:14:53

回答

0

EL不支持 「教科書」 超載(設計)。從this blog post關於這個問題的摘錄:

的方法的默認調用的行爲是由類javax.el.BeanELResolver處理。使用反射,它查找bean類中的方法。目前不支持重載方法,並且EL表達式中指定的參數數量必須與方法簽名中指定的參數數量相匹配。 這是因爲我們不想在EL規範中指定複雜的重載解析規則。

POST將繼續提供某種基本的選擇,在這裏你可以指定參數類型,也EL,以便在具體的實現你感興趣的方法的暗示的。 從同一個博客帖子:

然而,方法ELResolver.invoke具有可用於爲方法指定的正式參數類型paramTypes說法。如果此參數非空,則將選擇具有相同名稱和參數類型的方法調用。儘管EL語法不允許在方法調用中指定參數類型,但可以使用擴展語法輕鬆完成。從Javascript借款(我認爲)一個可以寫,例如

#{trader['buy(java.lang.String)']('JAVA')} 以指示方法的調用與字符串參數購買。

這是什麼可以表明你的情況是,你可能能夠與

<f:ajax render="@form" execute="@all" listener="#{userInformationController['updateMain(javax.faces.event.AjaxBehaviorEvent)']()}"/> 

我還沒有嘗試過就完事了;我不推薦它。 EL沒有正式支持方法超載

+0

這是一種魔術......那麼如何在Ajax中正常使用監聽器呢?它甚至有可能嗎?爲什麼bellow代碼工作? \t \t \t \t \t \t Jacek 2015-01-06 11:06:33

+0

我可以在這個頁面上一定是錯誤的 – Jacek 2015-01-06 11:30:10

+0

從所有的指示來看,重載後臺bean方法的行爲是未定義的。在支持bean @Jacek中'findAll'是否被重載?不能使用行動完全是另外一回事;爲此發表一個不同的問題 – kolossus 2015-01-06 16:15:27