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.

2
  • What JSF version are you using? Commented May 10, 2013 at 15:18
  • I use the 2.0 version Commented May 10, 2013 at 15:19

3 Answers 3

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");
   ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

it worked.It must be something wrong with the version. 2.2+ is much clearer to use. Thanks
@MateuszGaweł looks like the problem is in your web application server. Which one are you using?
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

1 Comment

@MateuszGaweł Glad to help you.Mark it as answer if its helpful(tick mark left side).
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

2 Comments

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 stackoverflow.com/a/3284328
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"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.