HTML and CSS Reference
In-Depth Information
public void findFactorial(ActionEvent event) {
result = 1;
for (int i = 1; i <= number; i++) {
result = result * i;
}
}
}
Adding to using the default action listener with either action method or action listener method, you can
write your own custom action listener. This can be done by creating your action listener class, which implements
ActionListener interface. Listing 4-5 shows CalcActionListener , which implements ActionListener. As shown,
processAction() gets the current input number and then calculates the corresponding factorial of this number, and
finally it sets the output in the result attribute of the Calc managed bean.
Listing 4-5. CalcActionListener Custom Action Listener
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
public class CalcActionListener implements ActionListener {
@Override
public void processAction(ActionEvent event) throws AbortProcessingException {
FacesContext context = FacesContext.getCurrentInstance();
Calc calc = context.getApplication().evaluateExpressionGet(context,
"#{calc}",
Calc.class);
long result = 1;
for (int i = 1; i <= calc.getNumber(); i++) {
result = result * i;
}
calc.setResult(result);
}
}
In order to attach the custom action listener to the UICommand component, you can use the <f:actionListener>
tag inside the UICommand component. Listing 4-6 shows the updates on the form mentioned in Listing 4-3 with the
custom action listener update.
Search WWH ::




Custom Search