EL中bean的动态选择 [英] Dynamic choice of bean in EL
问题描述
有没有一种方法可以指定运行时而不是设计时在EL中使用的托管bean?例如.
actionListener="#{myBean.method}"
,使得myBean
部分可以具有不同的值,具体取决于调用代码的JSF页面(条件是使用的任何对象而不是myBean均来自实现方法"的公共父级).
Is there a way to specify a managed bean used in EL at runtime and not design time? E.g.
actionListener="#{myBean.method}"
such that myBean
part can have different values depending on the JSF page the code is invoked from (on condition that any object used instead of myBean is derived from a common parent implementing the "method").
推荐答案
如果您事先知道bean的作用域并且已经事先创建了bean,那么可以,可以使用以下语法:
If you know the bean scope beforehand and the bean is already created beforehand, then yes, it's possible using the following syntax:
actionListener="#{requestScope[beanName].method}"
其中#{requestScope}
表示请求范围映射(视图,会话和应用程序范围可使用相同的映射),而#{beanName}
表示保存托管bean名称的变量.
where #{requestScope}
represents the request scope map (same maps are available for view, session and application scopes) and #{beanName}
represents the variable holding the managed bean name.
如果您事先不知道bean的作用域和/或未事先创建bean的时间,则需要创建一个实用的EL函数,该函数执行
If you don't know the bean scope beforehand and/or when the bean isn't created beforehand, then you'd need to create an utility EL function which does an Application#evaluateExpressionGet()
, so that it can be used something like as follows:
actionListener="#{util:getBean(beanName).method}"
这篇关于EL中bean的动态选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!