Managed Beans are the Plain Old Java Objects (POJO) having set of properties and operations which are available to the JavaServer Faces components directly through the Unified Expression Language(EL).

The two types of methods that can be written in a JSF managed bean class are:
  • Standard methods
  • JSF specific methods

Standard Methods

These are simple getter and setter methods of the properties that are available with the bean. Generally we do not allow the client to access the properties directly by name, instead provide the intermediatery methods to access or set the values of the properties.

The access modifier of these methods should be public so that they can be accessible anywhere. The name of the getter method normally starts with "get" and appended with the name of the variable with first letter of the variable in capital letter.

private String name;

public String getName(){

Similarly, the setter method starts with the "set" and follows the same conventions as above. The setter methods will not return a value and are void type methods as they do not require any value to be returned.

JSF Specific Methods

There are four different types of JSF specific methods that can be found in the managed beans.
  • Action Listener Methods
  • Value Change Event Listener Methods
  • Navigation Handler Methods
  • Validation Methods

Action Listener Methods

These are the methods we write to handle the specific events that can be triggered by invoking the actions on the JSF components like Submit buttons, Command Links etc. When an action event handler is associated with such component and the action is triggered by the user, the corresponding method in the managed bean will automatically gets executed by the Faces Controller before the actual action is performed.

Say for example, your application wanted to invoke a business method which require the prior information to process, the action event handled will be useful in making that information available before the actual business methods starts execution.

To be able to qualify a method in managed bean as an action event handler method, it needs to have javax.faces.event.ActionEvent object passed as a parameter to the method and the return type should be void.

public void myActionListener(ActionEvent e){

Now you can register this event with a component in a JSF page by using the actionListener property of the component.

actionListener="#{MyBean.myActionListener}" ....

Value Change Listener Methods

Value change event listener methods are used associate the UIInput components which accepts the user input for its value such as text boxes, check boxes etc. When the value of these components changed, the registered value change listeners on these components will automatically gets executed when the form on page gets submitted.

These are very useful in situations like examining the new data entered by the user before our business logic acts on that data. You can manipulate the old data and new data before the data gets applied on to the business model.

Similar to the action listener, the value change listener method of backing bean should have defined a parameter called ValueChangeEvent and return void.
public void myValueChangeListener(ValueChangeEvent e){
The event can be registered on a JSF input component by using the valueChangeListener parameter of the component.

<h:inputText valueChangeListener="#{MyBean.myValueChangeListener}" ...>

Navigation Handler Methods

Navigation Handler, also called as Action Handler, used to handle the user actions/requests. These methods accepts no parameters and returns String. The return value should be one of the navigation rule name that will render the associated view. If the return value is null or empty then the same page will be rendered.

These methods generally used as a handler methods to accept the user request and invoke the actual business logic. However these can contain the actual business logic as well.

The Navigation Handler methods can be referenced in a JSF page using the Unified Expression Language in a action parameter of the component.

<h:commandButton action="#{MyBean.myAction}" ...>

Validation methods

Validation methods are used to register a user defined validation criteria on a JSF UIInput component value. The validation method must accept FacesContext, UIInput and Object objects as parameters. The Object parameter is the value that gets validated and to be typecasted in order to validate it.

public void validateEmail(FacesContext context, 
UIInput component, Object value){
( ( UIInput ) component ).setValid( false );

The setValid method on the UIInput component can be used to set the validation failed flag so that the same page will render back with the error messages set if any.

Like it on Facebook, +1 on Google, Tweet it or share this article on other bookmarking websites.

Written by
  • No comments found