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.
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.
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");
...
}
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
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