2

Due to some custom components which expect a bean name (NOT the bean instance) in their attributes I need to pass the actual bean name between pages. As the bean itself is also used by non-custom components, I would like to avoid using additional ui:param (like described here Passing action in <rich:modalPanel>) since it will essentially specify the same bean.

Is it possible to specify component's action using bean name provided with ui:param?

Basically I am trying to achieve the following:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/template.xhtml">

   <ui:param name="beanName" value="sessionBean"/>
   ...

</ui:composition>

and template.xhtml is

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
template="/someothertemplate.xhtml">

  </ui:define name="somename">
    <h:form>
        <a4j:commandButton value="test" action="#{beanName.delete}"/>
    </h:form>
  </ui:define>
</ui:composition>

Although delete method is properly defined (verified with action="#{sessionBean.delete}") the above code gives me

javax.faces.FacesException: #{beanName.delete}: javax.el.MethodNotFoundException: /template.xhtml @201,89 action="#{beanName.delete}": Method not found: sessionBean.delete()

Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
orom
  • 851
  • 1
  • 10
  • 22

2 Answers2

5

You should be able to reference the bean via its scope:

 <a4j:commandButton value="test"
      action="#{sessionScope[beanName].delete}"/>
McDowell
  • 107,573
  • 31
  • 204
  • 267
  • One caveat here: the bean must have already been instantiated and placed in scope - this expression won't create a managed bean. – McDowell Nov 23 '09 at 14:47
  • Hi :) very interesting. But what is 'delete' is not a tag param but a constant? I mean should it be the expression as `"#{sessionScope[beanName]'.delete'}"` or how to combine the expression in this case? – user390525 Aug 16 '17 at 18:36
2
<a4j:commandButton value="test" action="#{bean[action]}" />

The params to pass

<ui:param name="bean" value="#{sessionBean}" />
<ui:param name="action" value="delete" />

you can use #{bean['delete']} if your action name is fixed.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • That's exactly what I would like to avoid - define additional param with bean instance. The "Method not found: sessionBean.delete()" gave me a suspicion that it's possible somehow to use bean name (not instance!) for action method since the beanName in my case is obviously being resolved correctly. – orom Nov 23 '09 at 14:48