4

In my JSF 1.2 project, I have created a facelet tag file and defined an inputText that has actionListener attribute to which I need to pass the backing bean method name. I tried defining a variable actionListener="#{actionListener}" in the tag file. In my xhtml where I call the component, when I pass the value as

actionListener="#{myBean.preFillData}"

tag file treats it as a property and errors out indicating no property 'preFillData' found. If I change it to

actionListener="#{myBean.preFillData()}"

then there is a parse error in the tag file because it doesnot like parenthesis to indicate method name.

How do we pass method name to the tag file?

Thanks PT

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
phewataal
  • 1,107
  • 4
  • 12
  • 23

1 Answers1

4

Passing method expressions is not supported in tag files. Only since JSF 2.0 it's possible with so-called composite components.

What you can do is to separate the bean reference and the method name so that you can use the brace notation to invoke the method. I'm only not sure if that works out for an actionListener, you normally don't use that to invoke actions, but it should definitely work for an action.

E.g.

<my:tag ... bean="#{myBean}" actionMethod="preFillData" />

with inside tag.xhtml

<h:commandButton ... action="#{bean[actionMethod]}" />

Only if you happen to use JSF 2.0 on Facelets, then you can use <o:methodParam> to pass a method expression to a tag file. See also a.o. Dynamic ui include and commandButton.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks @Balusc. Good to know the limitation JSF 1.2 and one mroe reason to upgrade:) – phewataal May 03 '12 at 14:11
  • About your comment on actionListener typically not used to invoke actions, I have a case where 2 textboxes' value (zipcode and extension) needs to be consolidated into a single property in my domain object before calling save as my action. Should this consolidation be also done in an action method instead of actionListener? – phewataal May 03 '12 at 14:20
  • Ah, that makes sense. But why don't you declare it directly in the tag file then? It seems to be a reuseable `ActionListener` implementation which doesn't necessarily need to refer a bean method. – BalusC May 03 '12 at 14:23
  • I am sorry I did not get you when you said "why don't you declare it directly in the tag file then". Can you please elaborate? – phewataal May 03 '12 at 14:25
  • @BalusC in this example what if `bean` or `actionMethod` have no values - the null values? Is there a way to analyze that on client side? – cbhogf Oct 12 '16 at 16:01