JSF EL Integrated Key Access Card

In the backup bean, I defined the property Map<Integer,String> . When I try to access the map from the EL inside the xhtml file, I get nothing.

 <h:outputLabel value="#{bean.myMap[0]}"> 

does not return a value for key 0. Using a string key, it works.

It works with List<String> , but I want the Map to have some kind of sparse array (not all indexes matter)

+6
source share
2 answers

EL interprets your literal number 0 as long . Try Map<Long,String> instead of Map<Integer,String> .

Here is what you do:

 myMap.put(Integer.valueOf(0), "SomeValue"); 

This is what EL does to return a value:

 String value = myMap.get(Long.valueOf(0)); 
+7
source

I had the same problem and found this when I was looking for a solution to solve the problem. Changing the map was not really an option for me, because it was an automatically generated code, so here's what I did.

I created a managed bean:

 package my.bean.tool; import javax.faces.bean.ManagedBean; import javax.faces.bean.ApplicationScoped; @ManagedBean @ApplicationScoped public class Caster { public Caster() { } public int toInt(long l) { return (int) l; } } 

Then I just did what in your case would be:

 <h:outputLabel value="#{bean.myMap.get(caster.toInt(0))}"> 
+2
source

Source: https://habr.com/ru/post/947433/


All Articles