Showing posts with label ManagedBean. Show all posts
Showing posts with label ManagedBean. Show all posts

Wednesday, April 4, 2012

JSF Managed Bean

MangedBean in JSF:

Managed Beans are the mere POJO that represents model of the JSF application and are managed by the JSF container. Managed Beans are to be registered in the JSF container through Faces Config xml in JSF1.2 and via annotations in JSF2.0 onwards. By registering it in annotation entry in the faces Config can be avoided. But still one can register managed bean in Faces Config without using annotations in JSF2.0. The criteria for managed bean are same as java bean conventions.

Registering a managed bean in Faces Config (JSF 1.2) :

<managed-bean>
                           <managed-bean-name>testBean</managed-bean-name>
                           <managed-bean-class>com.test.TestBean</managed-bean-class>
                           <managed-bean-scope>session</managed-bean-scope>
              </managed-bean>

Sample Managed Bean:

               public class TestBean {

                       private int id;
                       Private String name;

                      /** Getters and setters for Properties **/
                     
                 }


Registering in JSF2.0 :

@ManagedBean

              @RequestScoped
              public class TestBean {
              -------------------
             ---------------------------

Binding Managed Bean in XHTML:
               <h:inputText value="#{testBean.name}"></h:inputText>


Tuesday, April 3, 2012

Features in JSF2.0

New Features in JSF2.0


JSF2.0 has introduced many new features in align with its design goals. Few of the major new features are listed below.

 1. Avoid entries to Faces-Config.xml with annotations. There are new annotations added in JSF2 for declaring managed beans, scopes for managed bean. Few annotations are listed below
    • @javax.faces.bean.ManagedBean
    • @javax.faces.bean.RequestScoped
    • @javax.faces.bean.SessionScoped
    • @javax.faces.bean.ApplicationScoped
    • @javax.faces.bean.ViewScoped
    • @javax.faces.bean.NoneScoped
    • @javax.faces.validator.FacesValidator
    • @javax.faces.convert.FacesConverter  
2. Bean names are assigned with default if not explicitly stated in the @ManagedBean annotation with name attribute. In the below code, the ManagedBean annotation is not explicitly named and hence the managedbean name shall be defaulted with testBean (javaBean naming convention) 
                         @ManagedBean
                         @RequestScoped
                         public class TestBean {--------
                          -------------------------------------- 
3. Facelets are the default view technology for the JSF from JSF2.0 and JSP view technology for JSF is deprecated from JSF2.0. Facelets provides a powerful Templating for the application
4. Creating a custom component is made much easier in JSF2.0 compared to its earlier version and JSF2 brings in an enhanced resource handling

5. JSF has support to Groovy and hence all the managed bean, validators, converters, renderers shall be a based on groovy

6. Default Navigation mappings using the result of the action method. The page navigation shall be defaulted using the result of the action method. In the below piece of action method, the next page is defaulted to welcome.xhtml. By this way the navigation configuration in the Faces xml shall be avoided 

 /* * on the call of this action method, the user name is placed in the flash scope
   * and redirected to the new page.
   * @return welcome page string
   */
    public String save() {
         Flash flash=FacesContext.getCurrentInstance().getExternalContext().getFlash();
         flash.put("name",this.name);
         return "welcome";
     }

7. Ajax is enabled in JSF2.0 architecture and thus it is not necessary to depend on third party API for ajax features. Ajax shall be used using the f:ajax tag.

       <h:commandButton action="#{testBean.save}" value="submit">
                  <f:ajax execute="@form" render="@displayPanel" event="click"/>
       </h:commandButton>

8. Creating a custom component is made much easier in JSF2.0 compared to its earlier version and JSF2 brings in an enhanced resource handling.

9. JSF has support to Groovy and hence all the managed bean, validators, converters, renderers shall be a based on groovy