Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF2 Action parameter

Tags:

jsf

action

jsf-2

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?

like image 418
Sven Avatar asked Aug 30 '10 11:08

Sven


3 Answers

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)

like image 124
Bozho Avatar answered Sep 24 '22 09:09

Bozho


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.

like image 24
BalusC Avatar answered Sep 20 '22 09:09

BalusC


The new spec. JSF2 allows that the action method receives a param so you be able to do

<h:commandButton action="#{bean.action(otherBean.complexObject)}">

at the ManagedBean the method will be:

public String action(Object complexObject)

*Note: make sure you include the “el-impl-2.2.jar” *

like image 39
Nicolas Avatar answered Sep 22 '22 09:09

Nicolas