JSF组件的动态值绑定 [英] Dynamic value binding of JSF component

查看:115
本文介绍了JSF组件的动态值绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行时动态绑定某个组件的值?
例如,我有以下组件标记,

How do I bind a value of certain component dynamically at runtime? For example, I have the following component tag,

<h:inputText value="#{bean.someProp}" />

在我的情况下,#{bean.someProp}仅在运行时才知道。

In my case, "#{bean.someProp}" is only known at runtime.

实现此目的的最佳策略是什么?

What's the best strategy to implement this?

我应该遍历组件树并以编程方式设置值绑定吗?如果是,我应该在哪个JSF生命周期阶段进行遍历?

Should I traverse the component tree and set the value binding programmatically? If yes, at which JSF lifecycle phase should I do the traversing?

推荐答案

您可以将其绑定到 Map< String,Object> bean属性,其中 String 键小于或大于动态属性名称。您可以通过以下方式访问EL中的地图值:

You can bind it to a Map<String, Object> bean property where the String key is less or more the dynamic property name. You can access map values in EL the following way:

<h:inputText value="#{bean.map.someProp}" />

<h:inputText value="#{bean.map['someProp']}" />

甚至可以在 someVar 实际上解析为 String someProp

which can even be done a tad more dynamically where someVar actually resolves to a String value of "someProp":

<h:inputText value="#{bean.map[someVar]}" />

您只需要确保地图在bean初始化期间创建,否则JSF无法访问映射值。 EL即不会为您预先创建嵌套属性。因此,例如直接实例化:

You only need to ensure that the Map is created during bean initialization, otherwise JSF can't access the map values. EL namely won't precreate "nested properties" for you. Thus, do e.g. direct instantiation:

public class Bean {
    private Map<String, Object> map = new HashMap<String, Object>();
}

..或在构造函数内或 @PostConstruct 如果你愿意的话。

.. or inside a Constructor or @PostConstruct if you like.

这篇关于JSF组件的动态值绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆