Search code examples
javajsfannotationsjsf-2faces-config

JSF 2.0 Problem (faces-config)


We have faces-config.xml in JSF 1.0 where we entry about managed-beans, dependencies & navigations etc.

I was developing a sample project using JSF 2.0. But, as I don't know annotation, I need to include face-config.xml externally. Please, provide the solution for it, as in JSF 2.0 we don't need to include it. What is reason behind it? How do we set a bean as managed-bean. What is annotation? How is it used?


Solution

  • (...) in JSF 2.0 we don't need to include it. What is reason behind it?

    In three words: ease of development. There is just less code to write -- boilerplate code is removed, defaults are used whenever possible, and annotations are used to reduce the need for deployment descriptors.

    How do we set a bean as managed-bean. What is annotation? How is it used?

    Managed beans are identified using the @ManagedBean annotation. The scope of the bean is also specified using annotations (@RequestScoped, @SessionScoped, @ApplicationScoped, etc).

    So the following in JSF 1.0:

    <managed-bean>
      <managed-bean-name>foo</managed-bean-name>
      <managed-bean-class>com.foo.Foo</managed-bean-class>
      <managed-bean-scope>session</managed-bean>
    </managed-bean>
    

    Can be rewritten as such in JSF 2.0:

    @ManagedBean
    @SessionScoped
    public class Foo {
        //...
    }
    

    And referred like this in a Facelet page:

    <h:inputText label="eMailID" id="emailId" 
    value="#{foo.email}" size="20" required="true"/>
    

    (By default, the name of the managed bean will be the name of the annotated class, with the first letter of the class in lowercase.)

    See also