5

I have a JSF page. My CommandButton action method value is dependent on the bean variable value. Example: Bean headerBean has varaible actionValue with value "someBean.doAction1()"

When I use , It says headerBean.actionValue is not a method which is right.

How can I get the action value as "someBean.doAction1" instead of headerBean.actionValue.

Thanks,

mahesh
  • 51
  • 2

1 Answers1

6

You can use the brace notation for that.

<h:commandButton value="submit" action="#{someBean[headerBean.actionValue]}" />

When the #{headerBean.actionValue} returns a String of for example doAction1, then this will effectively invoke #{someBean.doAction1}.

If the bean name to be called is currently actually in the actionvalue (headerBean.actionValue returning someBean.doAction1), you need to split it into a field that returns the bean name and one that returns the method name and then use

<h:commandButton value="submit" action="#{requestScope[headerBean.beanName][headerBean.actionValue]}" />

If headerBean.beanName returns 'someBean' and headerBean.actionValue returns doAction1 the above will call #{somebean.doAction1}.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hi, Thanks for replying. My headerBean.actionValue returns someBean.doAction1. I think using your solution makes it [code]#{someBean.someBean.doAction1}[code]. The value of headerBean.actionValue also return the bean name. – mahesh Jun 02 '11 at 21:34
  • that's interesting -- i didn't realize that syntax works. thanks for the info. – Dave Maple Jun 02 '11 at 22:09
  • wow. interesting syntax. I usually do something this similar with reflection, like generate the method name at runtime and use reflection to invoke it. Nice to know that I can do it in JSF as well. +1 – Thang Pham Jun 03 '11 at 17:19
  • 1
    @mahesh: you should split that on the period and then use something like (assuming that bean is request scoped) `#{requestScope[headerBean.beanName][headerBean.beanAction]}`. @Harry: I think you may find this example useful: http://stackoverflow.com/questions/5713718/how-to-make-a-grid-of-jsf-composite-component/5716633#5716633 – BalusC Jun 03 '11 at 17:19