HTML and CSS Reference
In-Depth Information
If you do not need a navigation in your action method, then you can use action listener methods instead. Listing 4-3
shows a form that contains a
CommandButton
with an action listener method in order to calculate the factorial number of
an input field.
Listing 4-3.
An Example of an Action Listener Method
<h:form>
<h:outputText value="Enter Number:"/>
<h:inputText value="#{calc.number}">
<f:validateLongRange minimum="0" maximum="25"/>
</h:inputText>
<br/>
<h:commandButton value="Calculate Factorial"
actionListener="#{calc.findFactorial}">
</h:commandButton>
<br/>
<h:outputText value="Result is: #{calc.result}" rendered="#{calc.result ne 0}"/>
<h:messages/>
</h:form>
An action listener method can be attached to
UICommand
component using its
actionListener
attribute. As
indicated in the preceding, an action listener method executes an action without returning any outcome for the JSF
navigation, so the action listener method returns
void
and has
ActionEvent
as a parameter. Listing 4-4 shows
Calc
managed bean, which includes
findFactorial
action listener.
Listing 4-4.
Calc Managed Bean
@ManagedBean
@RequestScoped
public class Calc implements Serializable {
private int number;
private long result;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public long getResult() {
return result;
}
public void setResult(long result) {
this.result = result;
}