Skip to main content

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

Posted by arungupta on October 17, 2008 at 9:52 AM PDT



This is a follow up to href="http://blogs.sun.com/arungupta/entry/totd_48_converting_a_jsf">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 href="https://javaserverfaces.dev.java.net/servlets/ProjectDocumentList?folderID=9814&expandFolder=9814&folderID=9814">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 style="font-family: monospace;"><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:


style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="2" cellspacing="2">
<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


style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="2" cellspacing="2">
@Entity

@Table(name = "cities")

@ManagedBean(name="cities", scope="request")

@NamedQueries({@NamedQuery(...)})

public class Cities implements Serializable {



and


style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="2" cellspacing="2">
@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. Section 11.5.1 of href="http://jcp.org/aboutJava/communityprocess/edr/jsr314/index2.html">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 href="https://javaserverfaces.dev.java.net/issues/enter_bug.cgi?issue_type=DEFECT">here
using "2.0.0 EDR1" version and ask your questions on href="mailto:webtier@glassfish.dev.java.net">webtier@glassfish.dev.java.net.



Please leave suggestions on other TOTD ( style="font-weight: bold;">Tip style="font-weight: bold;">Of style="font-weight: bold;">The style="font-weight: bold;">Day) that
you'd like to see.
An archive of all the tips is available href="http://blogs.sun.com/arungupta/tags/totd">here.




Technorati: totd
javaserverfaces
glassfish
mojarra
netbeans

Related Topics >>

Comments

Arun, thanks for the tip. Now that we know a little bit more about how to use @ManagedBean and @ManagedProperty, how will we do navigation with JSF 2.0 ? I read that faces-config.xml will be optional. That means that navigation could also be done using annotations. Any tip about that feature ?