How to initialize Map property in Managed Mean?

July 2, 2008

JSF

Map property in Managed Mean

JSF Managed Beans can initialize its Map properties in the faces-config.xml. There is a property in the faces-config.xml as map-entries. This cane be used for initializing the values and can be accessed directly through the Managed Beans in any JSP pages. You also can directly use the List as Managed Beans.

JSP File (index.jsp)

1
2
3
4
5
6
7
8
9
10
11
12
13
<%@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:outputText value="#{jsfBean.mapValues['1']}"/>
                 <h:outputText value="#{jsfBean.mapValues['2']}"/>
                 <h:outputText value="#{jsfBean.mapValues['3']}"/>
            </h:form>
        </f:view>
    </body>
</html>

JavaBean (JavaBeatJsfBean.java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package javabeat.net.jsf;
 
import java.util.Map;
 
/**
 * source : www.javabeat.net
 */
public class JavaBeatJsfBean {
 
    private Map mapValues;
 
    public Map getMapValues() {
        return mapValues;
    }
 
    public void setMapValues(Map mapValues) {
        this.mapValues = mapValues;
    }
 
}

faces-config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?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>mapValues</property-name>
            <map-entries>
                <map-entry>
                    <key>1</key>
                    <value>One</value>
                </map-entry>
                <map-entry>
                    <key>2</key>
                    <value>Two</value>
                </map-entry>
                <map-entry>
                    <key>3</key>
                    <value>Three</value>
                </map-entry>
            </map-entries>
        </managed-property>
    </managed-bean>
    <navigation-rule>
        <navigation-case>
            <from-outcome>success</from-outcome>
            <to-view-id>/index.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>
email

Comments

comments