Miles to go …

October 17, 2008

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

Filed under: web2.0 — arungupta @ 9:46 am

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 .

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

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. TOTD #48: Converting a JSF 1.2 application to JSF 2.0 – Facelets and Ajax
  2. TOTD #129: Managed Beans 1.0 in Java EE 6 – What and How ?
  3. TOTD #94: A simple Java Server Faces 2.0 + JPA 2.0 application – Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3
  4. TOTD #120: Deployment Descriptor-free Java EE 6 application using JSF 2.0 + EJB 3.1 + Servlets 3.0
  5. TOTD #42: Hello JavaServer Faces World with NetBeans and GlassFish

10 Comments »

  1. just so so

    Comment by vieri122 — October 17, 2008 @ 10:28 pm

  2. 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.

    Comment by Lars Tackmann — October 19, 2008 @ 3:27 am

  3. i just want to say it’s cool!!11

    Comment by Anonymous — October 24, 2008 @ 3:42 am

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

    Comment by vieri122 — October 24, 2008 @ 5:11 am

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

    But, what i do to configure an property HashMap?

    Comment by Fred — October 25, 2008 @ 2:15 pm

  6. noriu zaisti runescape.com

    Comment by arnasarnas — October 29, 2008 @ 11:02 am

  7. Lars, follow up to your comment:

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

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

    Comment by Arun Gupta — December 5, 2008 @ 6:01 pm

  8. very very thankss

    Comment by çiçekçi — January 16, 2009 @ 5:03 am

  9. I think there is more bugs than written here.

    Comment by Football Pictures — March 8, 2009 @ 2:20 pm

  10. thanks

    Comment by sinema izle — March 13, 2009 @ 2:17 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress