JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

Lists and Maps as Managed Beans in JSF

July 12, 2008 by Krishna Srinivasan Leave a Comment

This tips explain how to use List and Map classes directly as Managed Beans. Here notice that you cannot use List and Map interfaces directly as Managed Beans, you can use only the implementations classes like ArrayList, Map, etc. There is no way to call constructor in the interfaces if you use List or Map directly.

also read:

  • Introduction to JSF
  • JSF Interview Questions
  • Request Processing Lifecycle phases in JSF

The following example program initialize the values in the faces-config.xml and stored as managed beans. You can use that beans directly in the JSP pages. Note that the sample program uses ‘none’ as the scope for the bean. This is because the resulted managed bean is created as per the request and will not be stored anywhere in the scopes.

JSP File (index.jsp)

[code lang=”html”]
<%@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="#{listBean}">
<h:column>
<h:outputText value="#{loc}" />
</h:column>
</h:dataTable>
<h:outputText value="#{mapBean[‘Apple’]}"/>
<h:outputText value="#{mapBean[‘Google’]}"/>
<h:outputText value="#{mapBean[‘Reliance’]}"/>
</h:form>
</f:view>
</body>
</html>
[/code]

faces-config.xml

[code lang=”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>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>
<managed-bean>
<managed-bean-name>mapBean</managed-bean-name>
<managed-bean-class>java.util.HashMap</managed-bean-class>
<managed-bean-scope>none</managed-bean-scope>
<map-entries>
<map-entry>
<key>Apple</key>
<value>Steve Jobs</value>
</map-entry>
<map-entry>
<key>Google</key>
<value>Larry Page and Sergy Brin</value>
</map-entry>
<map-entry>
<key>Reliance</key>
<value>Anil Ambani</value>
</map-entry>
</map-entries>
</managed-bean>
</faces-config>
[/code]

Filed Under: JSF Tagged With: Managed Bean in JSF

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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved