Managed Beans configuration In JSF 2.0 | Techartifact

In JSF 2.0, Java bean that is accessed from JSF page is called a Managed Bean. The managed bean can be a normal Java bean, which contains the getter and setter methods, business logic or even a backing bean (a bean contains all the HTML form value).

There are two ways to configure the managed bean :

-> Configure Managed Bean with faces-config.xml

It is same as we used to do JSF 1.1 .We can defined managed bean in faces-config.xml .

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
    <managed-bean>
	  <managed-bean-name>helloBean</managed-bean-name>
	  <managed-bean-class>com.vinay.common.HelloBean</managed-bean-class>
	  <managed-bean-scope>session</managed-bean-scope>
     </managed-bean>
</faces-config>

Using Dependency Injection

-> Configure Managed Bean with Annotation

You can annotated a Managed Bean with new @ManagedBean annotation in JSF 2.0 .JSF is a basic Dependency Injection (DI) container and we can use annotations to inject objects. JSF offers setter method injection – this means the object will be passed into the setter. It is also static injection – meaning, the injection will happen only during bean creation (as opposed to Seam, where static and dynamic injection is possible).

package com.vinay.common;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
 
@ManagedBean
@SessionScoped

public class HelloBean implements Serializable {
 
	private static final long serialVersionUID = 1L;
 
	private String name;
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
}


@ManagedBean – marks this bean to be a managed bean with the name specified in name attribute. If the name attribute in @ManagedBean is not specified, then the managed bean name will default to class name portion of the fully qualified class name.Try to set the scope (request) into which this bean will be placed. If scope is not specified then bean will default to request scope.

@ManagedBean also has eager attribute (new in JSF 2). If eager=”true” and scope is application, then this bean must be created when the application starts and not during the first reference to the bean. In other words, the creation and storing of the bean instance must happen before any requests are serviced.

@ManagedBean(name="vinayBean", eager=true)
@ApplicationScoped
public class Vinay{
 ...
}

If eager is true but scope is not application, then regular “lazy” initialization used. If eager is not defined or missing, then “lazy” initialization is used as well.

You can also pass the value of variable using annotation.

@ManagedProperty(value="vinay")
private String name;

When the bean is created, ‘vinay’ will be passed to setName(..) method.

Happy New year guys.This is first post of this year.
Happy coding with Techartifact.

Programmatic Navigation in JSF/ADF | Techartifact

Requirement-How to programmatically navigate to next jsf page in taskflow.

Solution-

Following code show the sample code for navigating one jsf to another using code.

FacesContext context = FacesContext.getCurrentInstance();
context.getApplication().getNavigationHandler().handleNavigation(context,null,"controlflowcase");

Package: javax.faces.application.NavigationHandler

public abstract void handleNavigation(FacesContext context, String fromAction, String outcome)

Perform navigation processing based on the state information in the specified FacesContext,plus the outcome string returned by an executed application action.
Parameters:
context – The FacesContext for the current request.
fromAction – The action binding expression that was evaluated to retrieve the specified outcome,or null if the outcome was acquired by some other means.
outcome – The logical outcome returned by a previous invoked application action (which may be null)

happy coding with techartifact . 🙂

Q. Explain briefly the life-cycle phases of JSF?


1. Restore View :
A request comes through the FacesServlet controller. The controller examines the request and extracts the view ID, which is determined by the name of the JSP page


2. Apply request values:
The purpose of the apply request values phase is for each component to retrieve its current state. The components must first be retrieved or created from the FacesContext object, followed by their values.Values provided in components that hold a value (such as input fields) have their values applied to their counterparts in the view tree. All events such as ValueChangeEvents (VCE) or ActionEvents (AE) are queued. A VCE means that a value has changed for a specific UI component. An AE means that a button (or any UI component that is a source of an action) was clicked. If a component has its immediate attribute set to true, then validation, conversion, and events associated with the component are processed during this phase.

3. Process validations: In this phase, each component will have its values validated against the application’s validation rules.Conversion and validation logic is executed for each component. This means both built-in validation/data conversion and custom validation/conversion are added onto the components. If a validation error is reported, an exception is thrown. The life cycle halts and the response is rendered with validation error messages. At the end of this phase, new component values are set, any validation or conversion error messages and events are queued on FacesContext, and any value change events are delivered.

4. Update model values: In this phase JSF updates the actual values of the server-side model ,by updating the properties of your backing beans.The component’s validated local values are moved to the model and the local copies are discarded. If you are using a backing bean for a JSF page to manage your UI components, any managed bean properties that are value-bound to the UI component using the value attribute are updated with the value of the component

5. Invoke application: In this phase the JSF controller invokes the application to handle Form submissions.

6. Render response: In this phase JSF displays the view with all of its components in their current state.Any action bindings for command components or events are
invoked. Navigation is handled here depending on the outcome of action method (if any).