Arun Gupta, Miles to go ...

Arun Gupta is a technology enthusiast, a passionate runner, and a community guy who works for Sun Microsystems.
« "Using Comet to... | Main | SOAP and REST - both... »

http://blogs.sun.com/arungupta/date/20081017 Friday October 17, 2008

TOTD #49: Converting a JSF 1.2 application to JSF 2.0 - @ManagedBean


This is a follow up to TOTD #48 which showed how to convert a JSF 1.2 application to use new features of JSF 2.0. In this blog, we'll talk about a new annotation added to the JSF 2.0 specification - @ManagedBean.

@ManagedBean is a new annotation in the JSF 2.0 specification. The javadocs (bundled with the nightly) clearly defines the purpose of this annotation:

The presence of this annotation on a class automatically registers the class with the runtime as a managed bean class. Classes must be scanned for the presence of this annotation at application startup, before any requests have been serviced.

Essentially this is an alternative to <managed-bean> fragment in "faces-config.xml". This annotation injects a class in the runtime as a managed bean and then can be used accordingly.

Using this annotation, the following "faces-config.xml" fragment from our application:

<managed-bean>
        <managed-bean-name>cities</managed-bean-name>
        <managed-bean-class>server.Cities</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>dbUtil</managed-bean-name>
        <managed-bean-class>server.DatabaseUtil</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
        <managed-property>
            <property-name>cities</property-name>
            <value>#{cities}</value>
        </managed-property>
    </managed-bean>

is simplified to

@Entity
@Table(name = "cities")
@ManagedBean(name="cities", scope="request")
@NamedQueries({@NamedQuery(...)})
public class Cities implements Serializable {

and

@ManagedBean(name="dbUtil", scope="request")
public class DatabaseUtil {

    @ManagedProperty(value="#{cities}")
    private Cities cities;

The specification defines that managed bean declaration in "faces-config.xml" overrides the annotation.

A worthy addition to this annotation is "eager" attribute. Specifying this attribute on the annotation as @ManagedProperty(..., eager="true") allows the class to be instantiated when the application is started. In JSF 1.2 land, developers write their own ServletContextListeners to perform this kind of task. And this can of course be specified in "faces-config.xml" as <managed-bean eager="true">.

Section 11.5.1 of JSF 2.0 EDR2 specification defines several similar annotations that can be used to simplify "faces-config.xml".

Have you tried your JSF 1.2 app on Mojarra 2.0 ? Drop a comment on this blog if you have.

File JSF related bugs here using "2.0.0 EDR1" version and ask your questions on webtier@glassfish.dev.java.net.

Please leave suggestions on other TOTD (Tip Of The Day) that you'd like to see. An archive of all the tips is available here.

Technorati: totd javaserverfaces glassfish mojarra netbeans

del.icio.us | furl | simpy | slashdot | technorati | digg |
|
Comments:

just so so

Posted by vieri122 on October 17, 2008 at 10:28 PM PDT #

Nice - but why is the scope declaration (in the @ManagedBean annotation) a string ?. Would it not be better with a ScopeType enum ?.

Anyway looking forward to cut my faces-config down to a reasonable size.

Posted by Lars Tackmann on October 19, 2008 at 03:27 AM PDT #

i just want to say it's cool!!11

Posted by 218.27.205.70 on October 24, 2008 at 03:42 AM PDT #

there is a another jsf framework AOM .it is much btter than this.
online demo
http://www.operamasks.org/rcdemos/index.jsf

Posted by vieri122 on October 24, 2008 at 05:11 AM PDT #

This is cool!
Really annotations help us improve and increase the time development.

But, what i do to configure an property HashMap?

Posted by Fred on October 25, 2008 at 02:15 PM PDT #

noriu zaisti runescape.com

Posted by arnasarnas on October 29, 2008 at 11:02 AM PDT #

Lars, follow up to your comment:

Following javax annotations have been added in the spec and Mojarra:

- @RequestScoped
- @SessionScoped
- @ApplicationScoped
- @UnScoped

Posted by Arun Gupta on December 05, 2008 at 06:01 PM PST #

very very thankss

Posted by çiçekçi on January 16, 2009 at 05:03 AM PST #

I think there is more bugs than written here.

Posted by Football Pictures on March 08, 2009 at 02:20 PM PDT #

thanks

Posted by sinema izle on March 13, 2009 at 02:17 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
« "Using Comet to... | Main | SOAP and REST - both... »

Valid HTML! Valid CSS!

This is a personal weblog, I do not speak for my employer.