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