How to use Managed Bean reference inside faces-config.xml?

SHARE & COMMENT :

Managed Bean in JSF can refer another managed bean inside the faces-config.xml. There is no restriction for the type of bean can be refered. But, JSF does not support the cyclic dependencies for Managed Bean reference.

also read:

For example, you cannot refer bean B from bean A while bean B also refers to bean A. The following simple program demonstrates how to refer a listBean inside a normal JSF Managed Bean. For simplicity purpose this example use List as managed bean. You can use any type of bean for the reference.

JSP File (index.jsp)

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<html>
    <body>
        <f:view>
            <h:form>
                <h:dataTable var="loc" value="#{jsfBean.listValues}">
                    <h:column>
                        <h:outputText value="#{loc}" />
                    </h:column>
                </h:dataTable>
            </h:form>
        </f:view>
    </body>
</html>

JavaBean (JavaBeatJsfBean.java)

package javabeat.net.jsf;

import java.util.List;

/**
 * source : www.javabeat.net
 */
public class JavaBeatJsfBean {
    private List listValues;

    public List getListValues() {
        return listValues;
    }

    public void setListValues(List listValues) {
        this.listValues = listValues;
    }
}

faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="1.2"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
    <managed-bean>
        <managed-bean-name>jsfBean</managed-bean-name>
        <managed-bean-class>javabeat.net.jsf.JavaBeatJsfBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
        <managed-property>
            <property-name>listValues</property-name>
            <value>#{listBean}</value>
        </managed-property>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>listBean</managed-bean-name>
        <managed-bean-class>java.util.ArrayList</managed-bean-class>
        <managed-bean-scope>none</managed-bean-scope>
        <list-entries>
            <value>Steve Jobs</value>
            <value>Sergy Brin</value>
            <value>Larry Page</value>
            <value>Anil Ambani</value>
        </list-entries>
    </managed-bean>
</faces-config>

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better