18

Is it possible to call bean methods & directly pass parameters to them from the view instead of requiring to first set the bean properties and then call methods without arguments using the commandButton or similar ?

I have a list of items with each item having a list of actions. To reduce the state, I am using a single primefaces remoteCommand, in place of several commandButton(s). On getting a action trigger from the view, I would call the remoteCommand from javascript but since the remoteCommand is one but used for multiple items thus I need to pass the id of the item as well. I am wondering if there is a way to pass the id of the item to the bean method directly as an argument instead of first setting it as a bean property ? Is there any way to do so ?

Actually I am looking at a better way to deal with multiple commandButtons on a page when there's a long list of items on the page.

Suggestions ? Thanks.


Using JSF 2.1.6 Mojarra with Primefaces 3.0RC1

Tiny
  • 27,221
  • 105
  • 339
  • 599
Rajat Gupta
  • 25,853
  • 63
  • 179
  • 294

3 Answers3

24

Passing method arguments is supported since EL 2.2 which is part of Servlet 3.0. So if your webapp runs on a Servlet 3.0 compatible container (Tomcat 7, Glassfish 3, etc) with a web.xml declared conform Servlet 3.0 spec (which is likely true as you're using JSF 2.1 which in turn implicitly requires Servlet 3.0), then you will be able to pass method arguments to bean action methods in the following form:

<h:commandButton value="Submit" action="#{bean.submit(item.id)}" />

with

public void submit(Long id) {
    // ...
}

You can even pass fullworthy objects along like as:

<h:commandButton value="Submit" action="#{bean.submit(item)}" />

with

public void submit(Item item) {
    // ...
}

If you were targeting a Servlet 2.5 container, then you could achieve the same by replacing the EL implementation by for example JBoss EL which supports the same construct. See also Invoke direct methods or methods with arguments / variables / parameters in EL.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • is this also possible inside tags or components? When I pass a bean to the component/tag I call the method on the attribute that holds the bean, e.g. `action="#{myBean['actionToCall']}"`. From the top of my head I wouldn't know how to pass a parameter in brackets syntax other than setting it with a `f:setPropertyActionListener`, and we need brackets over dots in this case, right? – Louise Aug 24 '12 at 15:46
  • 1
    @Louise: just use `action="#{myBean['actionToCall'](someParam)}"`. Note that this syntax had a bug in older Tomcat and Glassfish versions. Only since Tomcat 7.0.22 and Glassfish 3.1.2 it should work properly. – BalusC Aug 24 '12 at 15:53
  • Where this "item" (or "item.id") comes from? Can it be something like the id of the selected p:tab in a p:tabView (primefaces)? – Miklos Jakab Oct 27 '17 at 12:48
11

Yes, it is.

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

See this http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/

user219882
  • 15,274
  • 23
  • 93
  • 138
9

You can call ManagedBean methods with arguments like this.

<h:commandButton actionListener="#{stateBean.delete(row.stateID)}" 
 value="Delete" id="btnDeleteS">

   <f:ajax event="action" execute="@form" render="@form"/>
</h:commandButton>

The corresponding ManagedBean would be like this.

@ManagedBean
@RequestScoped
public class StateBean
{
    @EJB
    private RemoteInterface obj=null;

    public void delete(String stateID)
    {
        //Code stuff here.
    }
}

You can also directly set the value of ManagedBean properties using <f:setPropertyActionListener></f:setPropertyActionListener> like this.

<h:commandButton value="Delete" id="btnDeleteS">

     <f:setPropertyActionListener target="#{stateBean.someProperty}"
       value="#{someValue}"/>
     <f:ajax event="action" execute="@form" render="@form"/>
</h:commandButton>
Lion
  • 18,729
  • 22
  • 80
  • 110