我正在读一本涵盖JSF的Java EE书,作者正在使用ActionEvent调用JSF中的Ajax函数,但是如果没有ActionEvent,Ajax函数也可以正常工作,所以我的问题是,是否有理由将ActionEvent用于Ajax函数在JSF中?请参见下面的示例。
index.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h2>JSF Ajax Demo</h2>
<h:form>
<h:panelGrid columns="2">
<h:outputText value="First Operand:" />
<h:inputText id="first" value="#{controller.firstOperand}" size="3" />
<h:outputText value="Second Operand:" />
<h:inputText id="second" value="#{controller.secondOperand}" size="3" />
<h:outputText value="Total:" />
<h:outputText id="sum" value="#{controller.total}" />
<h:commandButton actionListener="#{controller.calculateTotal}" value="Calculate Sum">
<f:ajax execute="first second" render="sum" />
</h:commandButton>
</h:panelGrid>
</h:form>
</h:body>
Controller.java
@Named(value="controller")
@ViewScoped
public class Controller implements Serializable {
private int firstOperand;
private int secondOperand;
private int total;
public Controller() {
}
// author's method
// public void calculateTotal(ActionEvent actionEvent) {
// total = firstOperand + secondOperand;
// }
// no ActionEvent method (works fine)
public void calculateTotal() {
total = firstOperand + secondOperand;
}
// getter and setter here
}