JSF Configure problem

Discussions

Web tier: servlets, JSP, Web frameworks: JSF Configure problem

  1. JSF Configure problem (2 messages)

    i am developing a web application using Java server faces, i have some question :

    i configure a managed bean in faces-fconfig.xml file as follow:
    ---------------------------------------------------------
    <managed-bean>
        <managed-bean-name>nameBean</managed-bean-name>
        <managed-bean-class>demo.NameBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
        <managed-property>
           <property-name>testBean</property-name>
          <property-class>demo.TestBean</property-class>
          <value>#{applicationScope.testBean}</value>
        </managed-property>
    </managed-bean>
     -------------------------------------------------------------


     testBean is a property of the class NameBean, it is a client class demo.TestBean. in order to refereced the testBean objecet, i configure a referenced bean in faces-config.xml
     as follow:

     -----------------------------------------------------------
    <referenced-bean> <referenced-bean-name>testBean</referenced-bean-name>
    <referenced-bean-class>demo.TestBean</referenced-bean-class
    </referenced-bean>
    -------------------------------------------------------------
    my NameBean's code is
    ------------------------------------------------------------------------
    public class NameBean {
    private String userName;

    private TestBean testBean;

    /**
    * @return User Name
    */
    public String getUserName() {
    return userName;
    }

    /**
    * @param User
    * Name
    */
    public void setUserName(String name) {
    userName = name;
    }

    public String login() {
    FacesContext context = FacesContext.getCurrentInstance();

    Application application = context.getApplication();

    TestBean testBean = (TestBean) application.createValueBinding(
    "#{applicationScope.testBean}").getValue(context);


    return "greeting";
    }

    public TestBean getTestBean() {
    return testBean;
    }

    public void setTestBean(TestBean testBean) {
    this.testBean = testBean;
    }

    }

    --------------------------------------------------------------------
    the TestBean code as follow:
    -------------------------------------------------------------------
    public class TestBean implements Serializable {
    private String loginName = "willow";

    public TestBean() {

    }

    public String getLoginName() {
    return loginName;
    }

    public void setLoginName(String loginName) {
    this.loginName = loginName;
    }

    }
    ---------------------------------------------------------------------
    but when i excute in NameBean's login Method, i want to get the refrence of TestBean by follow code:
    TestBean testBean = (TestBean) application.createValueBinding(
    "#{applicationScope.testBean}").getValue(context);


    but i got testBean is null, i have configure referenced bean in faces-config.xml file,
    --------------------------------------
    <referenced-bean> <referenced-bean-name>testBean</referenced-bean-name> <referenced-bean-class>demo.TestBean</referenced-bean-class
    </referenced-bean>
    -----------------------------------------
    why testBean is null? what's the problem?

    Threaded Messages (2)

  2. JSF Configure problem[ Go to top ]

    <referenced-bean> indicates that the object will be created externally from JSF and is not managed by JSF. As such getting a value binding to it would not force an instance of demo.TestBean to get created.
    If you change that to be a <managed-bean> then it will be implictly be created when any EL referrs to it.
  3. JSF Configure problem[ Go to top ]

    Also remember that when you set up dependencies between managed beans which is what you'll have here, they can't be interdependent on each other. Cyclical dependencies are not supported by JSF's managed bean facility.