Immediate and deferred value expressions

Immediate and deferred value expressions

In JavaServer Faces (JSF), immediate and deferred value expressions are two types of EL (Expression Language) expressions used to handle how and when component values are processed during the JSF lifecycle. Here’s an explanation followed by a Java example illustrating immediate and deferred value expressions:

Immediate and deferred value expressions

Explanation

  • 1. Immediate Value Expressions:
    • Purpose:  Immediate value expressions evaluate and process component values during the “Apply Request Values” phase of the JSF lifecycle.
    • Syntax: Typically uses the `${}` syntax (though this is more common in JSP/Facelets EL rather than JSF EL).
    • Usage:  Suitable for components where input should be processed immediately upon form submission, skipping validation and conversion phases.
    • Syntax:  Enclosed within {} directly binds to a property or invokes a method, executing immediately upon form submission.
    • Example:  <h:inputText value=”{userBean.username}” immediate=”true” />
  • 2. Deferred Value Expressions:
    • Purpose:  Deferred value expressions evaluate and process component values during the “Process Validations” phase of the JSF lifecycle.
    • Usage: Typically used for components where
      input needs to undergo validation and conversion before updating backend data
      or displaying results.
    • Syntax:  Enclosed within {} binds to a property or invokes a method, allowing for validation and conversion to occur before processing.
    • Example:  <h:outputText value=”{userBean.fullName}” />

Java Example:

Here’s an example of using deferred value expressions in JSF, as immediate value expressions are rarely used in JSF:

Managed Bean (UserBean.java)
java
package com.example.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class UserBean {

    private String username;
    private String fullName;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getFullName() {
        // Simulated method to fetch full name based on username
        if (username != null && !username.isEmpty()) {
            return username.toUpperCase(); // Simulated logic
        }
        return "Unknown";
    }
}

JSF Page (userInfo.xhtml)
xml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">

<h:head>
    <title>User Information Page</title>
</h:head>
<h:body>
    <h3>User Information</h3>

    <h:form>
        <p>Username: <h:inputText value="{userBean.username}" immediate="true" /></p>

        <h:outputLabel for="fullName" value="Full Name:" />
        <h:outputText id="fullName" value="{userBean.fullName}" />
    </h:form>
</h:body>
</html>

Explanation of the Example:

  • Managed Bean (UserBean.java):
    • Defines a managed bean UserBean with properties username and fullName.
    • Provides getter and setter methods for username to interact with <h:inputText> component in the JSF page.
    • Includes a getFullName() method to simulate retrieving the full name based on the username input.
  • JSF Page (userInfo.xhtml):
    • Uses <h:inputText> component with immediate=”true” attribute to bind the username property using immediate value expression.
    • <h:outputText> component binds to fullName property using deferred value expression, displaying the full name fetched from UserBean after validation and conversion phases.

Summary

Immediate and deferred value expressions in JSF provide flexibility in how component values are processed during different phases of the JSF lifecycle. Immediate value expressions are processed early, skipping validation and conversion phases, suitable for rapid data submission scenarios. Deferred value expressions undergo full validation and conversion, ensuring data integrity and correctness before updating backend data or displaying results. Understanding and utilizing these expressions appropriately can enhance the performance, usability, and reliability of JSF-based web applications.

Homepage

Readmore