簡體   English   中英

JSF中的Ajax:在CommandButton中調用Ajax

[英]Ajax in JSF: calling ajax in commandbutton

我在JSF中閱讀了許多有關ajax的頁面和代碼,但是我仍然對此有疑問。 第一次命令按鈕調用ajax,第二次不起作用。 我不明白如何使用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>

EL不支持“教科書”重載(根據設計)。 這篇博客文章摘錄的主題:

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

該文章的確提供了一些基本的替代方法,您可以在EL中指定自變量類型,以暗示您感興趣的方法的特定實現。

但是, 方法ELResolver.invoke具有paramTypes參數,可用於指定該方法的形式參數類型。 如果此參數為非null,則將選擇具有相同名稱和參數類型的方法進行調用 盡管EL語法不允許在方法調用中指定參數類型,但是使用擴展語法可以很容易地完成。 例如,從Java語言借用(我認為)是可以寫的

#{trader['buy(java.lang.String)']('JAVA')}表示使用String參數調用了方法buy。

在您的情況下,這可能表明您可能能夠擺脫困境

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

我還沒試過 我不推薦。 EL並未正式支持方法重載

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM