Dynamic choice of bean in EL

前端 未结 2 1519
北海茫月
北海茫月 2021-01-14 07:33

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

2条回答
  •  孤独总比滥情好
    2021-01-14 08:31

    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}"
    

    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.

    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}"
    

提交回复
热议问题