Java EE 8 By Example
  • Introduction
  • Overview
    • Example Codes
  • JSF 2.3
    • Activating CDI in JSF 2.3
    • Run applications in JSF 2.2 compatible mode
    • CDI alignment
    • CDI compatible @ManagedProperty
    • Inject support in Converter, Validator and Behavor
    • Websocket support
    • UIData improvements
    • Class level bean validation with f:valdiateWholeBean
    • Java 8 DateTime support
    • PostRenderViewEvent: publising events after view is rendered
    • Search expression framework
  • CDI 2.0
    • Java SE support
    • Event Priority
    • Async Events
    • Register Beans dynamicially
    • Configurators and Intercept Producers
  • JPA 2.2
    • Java 8 Datetime support
    • Return Stream based result from Query
    • More CDI Alignments
  • JSON-B 1.0
  • JSON-P 1.1
  • Bean Validation 2.0
  • JAXRS 2.1
    • Async improvements
    • Server Sent Event
    • Reactive Client
  • Java EE Security API 1.0
    • HttpAuthenticationMechanism
    • IdentityStore
    • SecurityContext
  • Servlet 4.0
    • Server Push
    • Runtime Discovery of Servlet Mappings
    • Http Trailer
  • MVC 1.0
    • Getting started with MVC
    • Handling form submission
    • Exception handling and form validation
    • Processing PUT and DELETE methods
    • Page navigation
    • MVC and CDI
    • Security
    • Bean parameter conversion
    • View engine
Powered by GitBook
On this page

Was this helpful?

  1. JSF 2.3

CDI compatible @ManagedProperty

PreviousCDI alignmentNextInject support in Converter, Validator and Behavor

Last updated 4 years ago

Was this helpful?

In JSF 2.3, the built-in scope annotations are deprecated, there are some alternatives provided in CDI, eg. ApplicationScoped, SessionScope, RequestScoped, etc.

In JSF 2.2, JSF itself provided a CDI compatible ViewScoped, to view more details.

There is an exception, we can not find an alternative for the legacy @ManagedProperty, there is a describe how to created a CDI compatible @ManagedProperty from scratch.

Fortunately, JSF 2.3 provides a built-in CDI implementation.

Let's create a sample to try it.

Create a simple backing bean.

import javax.faces.annotation.ManagedProperty;
//...other imports

@Model
public class BackingBean {

    @Inject
    @ManagedProperty("#{fooBean.bar}")
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

NOTE, @ManagedProperty is from package javax.faces.annotation, which is newly added in JSF 2.3.

fooBean is a CDI bean, which has a property named bar.

@Named
public class FooBean {

    private String bar = "bar from FooBean";

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }
}

In the facelets template, display the message of BackingBean.

<div>
    CDI managed property: #{backingBean.message}    
</div>

Run this application on Glassfish v5, open your browser and navigate to .

Grab the from my GitHub account, and have a try.

go here
useful blog entry
http://localhost:8080/jsf-managedproperty/managedproperty.faces
source codes
jsf cdi managedproperty