Dynamic choice of bean in EL

前端 未结 2 1485
北海茫月
北海茫月 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:18

    If your question refers to generating a set of pages for a number of subclasses with common elements you can also use page composition to achieve this.

    For example if Resident and NonResident are managed beans for subclasses of Person:

    eg. in 'non-resident.xhtml'

    <ui:define name="body">
        <ui:param name="Controller" value="#{NonResident}"/>
        <ui:include src="anyone.xhtml"/>
    </ui:define>
    

    and in 'resident.xhtml'

    <ui:define name="body">
        <ui:param name="Controller" value="#{Resident}"/>
        <ui:include src="anyone.xhtml"/>
    </ui:define>
    

    then 'anyone.xhtml' can define the common elements using 'Controller' for the bean name.

    This approach also enables you to eliminate duplicate page definition.

    0 讨论(0)
  • 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}"
    
    0 讨论(0)
提交回复
热议问题