• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using @ManagedProperty to inject Map or HashMap

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to inject a Map or HashMap with @ManagedProperty.
I am getting an error like following -

javax.el.ELException: Cannot convert #{userBean.refs of type class java.lang.String to interface java.util.Map

I am unable to get the reason. Please Help.

Details -
1. I am using an action from demo.xhtml like -
<h:commandButton value="SHOW" action="#{hello.show }" />

2. in show.xhtml, I am access the value of map as
<h:outputText value="Refs" />
<h:outputText value="#{hello.refs }"/>

3. The managed beans are as -
@ManagedBean(name="hello")
@SessionScoped
public class HelloBean {
@ManagedProperty(value="#{userBean.refs")
private Map<String,Long> refs;
//Setters & Getters

&

@ManagedBean(name="userBean")
@SessionScoped
public class TestBean {
private Map<String,Long> refs;
public TestBean() {
refs = new HashMap<String,Long>();
refs.put("aaa",new Long(111));
refs.put("bbb",new Long(222));
refs.put("ccc",new Long(333));
refs.put("ddd",new Long(444));
refs.put("eee",new Long(555));
}
//Setters & Getters


But, this is generating error. Output of console is like -
com.sun.faces.mgbean.ManagedBeanCreationException: Unable to set property refs for managed bean hello
...........
..........
at java.lang.Thread.run(Unknown Source)
Caused by: javax.el.ELException: Cannot convert #{userBean.refs of type class java.lang.String to interface java.util.Map
at org.apache.el.lang.ELSupport.coerceToType(ELSupport.java:424)
at org.apache.el.ExpressionFactoryImpl.coerceToType(ExpressionFactoryImpl.java:47)
at com.sun.faces.el.ELUtils.coerce(ELUtils.java:536)
at com.sun.faces.mgbean.BeanBuilder$Expression.evaluate(BeanBuilder.java:592)
at com.sun.faces.mgbean.ManagedBeanBuilder$BakedBeanProperty.set(ManagedBeanBuilder.java:606)
... 51 more

Mar 01, 2014 1:13:09 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/JSF06ManagedBeans] threw exception [Unable to set property refs for managed bean hello] with root cause
javax.el.ELException: Cannot convert #{userBean.refs of type class java.lang.String to interface java.util.Map
at org.apache.......................Unknown Source)
at java.lang.Thread.run(Unknown Source)

 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is simple.

HTML is text (string) based. You cannot send a Java binary object to a string-based rendering system without some sort of converter that will render the object as a string.

JSF allows you to create such converters. It has also has some converter built into the basic JSF framework, but none for Maps.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic