问题描述
有没有正确的方法来重写JSF从表达式语言访问bean字段的方式?这个想法是为了访问一个 Map< String,?>
值,模拟这个行为,其中bean字段将是地图键。
#{beanContainingNestedMap.keyOfSaidNestedMap}
,就像 keyOfSaidNestedMap
是 beanContainingNestedMap
的字段? 如果没有,还有什么其他解决方案?
示例:
Holder.java
public class Holder {
private Map< String,Object> objects = new HashMap< String,Object>();
public void add(String key,Object value){
objects.put(key,value);
}
public Object getObject(String key){
return objects.get(key);
}
}
ExampleBean.java
public class ExampleBean {
private Holder holder =新持有人();
public ExampleBean(){
holder.add(foo,42);
holder.add(bar,'X');
}
public Holder getHolder(){
return holder;
}
}
example.xhtml
< c:out value =#{exampleBean.holder.foo} /> <! - 应该打印42 - >
< c:out value =#{exampleBean.holder.bar}/> <! - 应该打印X - >
如果我能做某事喜欢(一种伪代码,因为我不知道这样的方法是否存在;)
):
@Override //覆盖JSF的(如果有的话)
public Object resolveEl(String el){
try {
super.resolveEl(el);
} catch(ElException e){
Object bean = e.getBean();
String fieldName = e.getFieldName();
if(bean instanceof Holder){
持有者=(持有者)bean;
对象值= holder.getObject(fieldName);
if(value == null){
throw e;
} else {
返回值;
}
}
}
}
您可以直接使用地图EL。
Holder.java
public class Holder {
private Map< String,Object> objects = new HashMap< String,Object>();
public void add(String key,Object value){
objects.put(key,value);
}
public Map< String,Object> getObjectsMap(){
return objects;
}
}
EL
#{exampleBean.holder.objectsMap [your-key]}
Is there any proper way to override the way JSF accesses the beans fields from an Expression Language? The idea is to mimic this behavior in order to access a Map<String, ?>
values, where the bean fields would be the map keys.
In other words, is it possible anyhow to use #{beanContainingNestedMap.keyOfSaidNestedMap}
, just as if keyOfSaidNestedMap
were a field of the beanContainingNestedMap
?
If not, what other solution may I have?
Example:
Holder.java
public class Holder {
private Map<String, Object> objects = new HashMap<String, Object>();
public void add(String key, Object value) {
objects.put(key, value);
}
public Object getObject(String key) {
return objects.get(key);
}
}
ExampleBean.java
public class ExampleBean {
private Holder holder = new Holder();
public ExampleBean() {
holder.add("foo", 42);
holder.add("bar", 'X');
}
public Holder getHolder() {
return holder;
}
}
example.xhtml
<c:out value="#{exampleBean.holder.foo}" /> <!-- should print "42" -->
<c:out value="#{exampleBean.holder.bar}" /> <!-- should print "X" -->
What would be great is if I could do something like (kind of pseudo-code since I don't know if such a method exists ;)
):
@Override // override JSF's (if any...)
public Object resolveEl(String el) {
try {
super.resolveEl(el);
} catch (ElException e) {
Object bean = e.getBean();
String fieldName = e.getFieldName();
if (bean instanceof Holder) {
Holder holder = (Holder) bean;
Object value = holder.getObject(fieldName);
if (value == null) {
throw e;
} else {
return value;
}
}
}
}
You can directly use map by EL.
Holder.java
public class Holder {
private Map<String, Object> objects = new HashMap<String, Object>();
public void add(String key, Object value) {
objects.put(key, value);
}
public Map<String, Object> getObjectsMap() {
return objects;
}
}
EL
#{exampleBean.holder.objectsMap[your-key]}
这篇关于使JSF访问Map< String,?>来自EL而不是一个bean字段的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!