HTML and CSS Reference
In-Depth Information
Listing 4-6. An Example of Custom Action Listener in the XHTML Page
<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">
<f:actionListener type="com.jsfprohtml5.factorial.model.CalcActionListener"/>
</h:commandButton>
<br/>
<h:outputText value="Result is: #{calc.result}" rendered="#{calc.number ne 0}"/>
<h:messages/>
</h:form>
A best practice to follow is to use action methods for executing business actions, which may also include navigation
to new pages, and to use action listener methods (or custom action listeners) to do some initialization work for actions
(such as logging the actions, for example) before executing the actual business action. It is important to know that
action listener methods (or custom action listeners) always execute before action methods in the same order that they
are declared in the view and attached to the ActionSource2 component. Listing 4-7 shows the factorial calculation
form mentioned in Listing 4-3 with an update which combines both an action method and an action listener on the
"Calculate Factorial" command button component.
Listing 4-7. Action Method and Action Listener Combination on the “Calculate Factorial” Command Component
<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.logFindFactorial}"
action="#{calc.findFactorial}">
</h:commandButton>
<br/>
<h:outputText value="Result is: #{calc.result}" rendered="#{calc.number ne 0}"/>
<h:messages/>
</h:form>
Search WWH ::




Custom Search