Skip to main content

JSF Tip #30 - Migrate your ManagedBean to Named annotations

Posted by mriem on October 30, 2013 at 5:38 AM PDT

Since JSF 2.2 we consider the old style @ManagedBean annotation deprecated, but what now you would say? Well it is pretty straightforward.

1. Change @ManagedBean(name="xxx") to @Named("xxx")
2. Change the package imports for your @XXXScoped annotations to the CDI equivalents.

From To
javax.faces.bean.ApplicationScoped javax.enterprise.context.ApplicationScoped
javax.faces.bean.RequestScoped javax.enterprise.context.RequestScoped
javax.faces.bean.SessionScoped javax.enterprise.context.SessionScoped
javax.faces.bean.ViewScoped javax.faces.view.ViewScoped

Note JSF 2.2 supports more scopes, but they are new so they are not mentioned in the table above. Future blog entries will touch on those new scopes.

And that is it.

Enjoy!

Comments

Coincidentally, I was working on a refactoring yesterday ...

Coincidentally, I was working on a refactoring yesterday doing exactly that, but the application stopped working. After some debugging I realized that the annotation @ManagedProperty was not working anymore, since it needed the @ManagedBean in place. I'm using it to get values from request parameters in the URL (i.e. ?id=987).

After making some research, I realized there is no annotation alternative to get request parameters in my classes, in a way that the values are available for the @PostConstruct method.

Have a look at the code: https://github.com/htmfilho/architect/blob/master/src/main/java/com/hild.... The line 74 never executes.

A future blog entry will deal specifically with this ...

A future blog entry will deal specifically with this problem. So stay tuned!

Nice article! :) Getting request parameters that ...

mriem wrote:
A future blog entry will deal specifically with this problem. So stay tuned!

Nice article! :)

Getting request parameters that should be available in @PostConstruct via an annotation is something that can be done with OmniFaces' @Param. I specifically created it for this very use case. See http://showcase.omnifaces.org/cdi/Param for some more details.