views:

278

answers:

2

Hi folks

I have read about passing parameters from jsf page to managedbean through actionListener. Is it also possible to pass a parameter to a simple action method?

Thank you for reading...


Thank you both for your advices! I would be lost without you :-)

Following worked for me:

<h:commandLink id="link" action="#{overviewController.showDetails}" >
   <f:setPropertyActionListener target="#{overviewController.show_id}" value="#{project.id}" />
   <h:outputText value="#{project.title}" />
</h:commandLink>

So now who deserves the green tick? :-P can I give two of them?

+2  A: 

Yes. Either:

action="#{bean.method(param)}"

Or

<h:commandButton .. >
    <f:setPropertyActionListener
         target="#{bean.targetProperty}" value="#{param}" />
</h:commandbutton>

(and use the bean property in the method)

Bozho
Congrats with silver JSF badge by the way!! Now I am not alone anymore :)
BalusC
@BalusC thanks :) but soon you're going to be alone with the gold one ;)
Bozho
@Bozho Congrats
org.life.java
So the right answer tick goes to Bozho, so that it's more exciting about who gets the gold batch first :-P
Sven
@Sven thanks. though accepting answers doesn't count towards the badges, and anyway BalusC already has a huge lead :)
Bozho
+2  A: 

You're talking about parameters in this form?

<h:commandButton action="#{bean.action(param)}" />

That depends on the EL implementation. Only JBoss EL and JSP 2.2 EL is capable of doing this. How to install JBoss EL is described in this answer.

Alternatively, you can also just use f:param. The f:param used to work with h:commandLink only, but since JSF 2.0 it also works on h:commandButton. E.g.

<h:commandButton action="#{bean.action}">
    <f:param name="foo" value="bar" />
</h:commandButton>

with a @ManagedProperty which sets the parameter as managed bean property:

@ManagedProperty("#{param.foo}")
private String foo;

With this you're however limited to standard types (String, Number, Boolean). An alternative is the f:setPropertyActionListener:

<h:commandButton action="#{bean.action}">
    <f:setPropertyActionListener target="#{bean.foo}" value="#{otherBean.complexObject}" />
</h:commandButton>

That said, there are more ways as well, but this all depends on the sole functional requirement and the bean scopes. Probably you don't need to pass a "parameter" at all after all.

BalusC