Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using @ManagedProperty to inject Map or HashMap  RSS feed

 
Bajrang Kar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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)

 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!