3

How do I call method with variable parameters in JSF?

I tried something like this:

<h:commandButton value="Send" action="#{myBean.checkPIN(someOtherBean.PIN)}" />

However, this doesn't work.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mateusz Gaweł
  • 673
  • 1
  • 8
  • 22

3 Answers3

5

If you are using EL 2.2+, it's possible.

If you are using older version ot EL, you can use do the following:

<h:commandButton value="Send" action="#{myBean.checkPIN}" />
   <f:param name="parameter" value="123" />
</h:commandButton>

In the managed bean you can retrieve it like:

public void checkPIN() {
   ...
   Map<String, String> parameterMap = (Map<String, String>) externalContext.getRequestParameterMap();
   String param = parameterMap.get("parameter");
   ...
}
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
4

Yes it is possible if you are using > EL 2.2 which is part of Servlet 3.0.

See @BalusC's suggetions here Invoke direct methods or methods with arguments / variables / parameters in EL

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

It does work with EL 2.2. Which is probably the version you're using, since you're using JSF 2 (Even though it might not be the case).

You can do a very simple test. You can have an OtherMB such as this:

@ManagedBean(name = "otherMB")
public class OtherMB{

    public String getValue(){
        return "Other Managed Bean Value";
    }

}

And a method in your MainMB like this:

@ManagedBean(name = "mainMB")
public class MainMB{

    public void method(String str){
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(str));
    }

}

And in your xhtml you can just invoke the function using a button:

<h:commandButton action="#{mainMB.method(otherMB.value)}" value="Click Me!" />

Just remember that the h:commandButton needs to be inside an h:form, and that you need a component to show the message. Or you can just change the implementation to print the message in the console

Rodrigo Sasaki
  • 7,048
  • 4
  • 34
  • 49
  • The feature is not specific to JSF 2.x. It's specific to EL 2.2. So using JSF 2.x does in no way imply that EL 2.2 also been used. Carefully read the last paragraph of http://stackoverflow.com/a/3284328/ – BalusC May 10 '13 at 16:31
  • I know @BalusC. I have carefully read your answer (which was very enlightning btw), and it has that's why I said in the answer things like: "probably" and "even though it might not be the case" – Rodrigo Sasaki May 10 '13 at 17:17