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
Comments:

just so so

Posted by vieri122 on October 17, 2008 at 03: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 18, 2008 at 08:27 PM PDT #

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

Posted by guest on October 23, 2008 at 08:42 PM 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 23, 2008 at 10:11 PM 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 07:15 AM PDT #

noriu zaisti runescape.com

Posted by arnasarnas on October 29, 2008 at 04: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 10:01 AM PST #

very very thankss

Posted by çiçekçi on January 15, 2009 at 09:03 PM PST #

I think there is more bugs than written here.

Posted by Football Pictures on March 08, 2009 at 07:20 AM PDT #

thanks

Posted by sinema izle on March 13, 2009 at 07:17 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today