I have a variable in js, and I need to send it to my ManagedBean, what is the most correct way to do this?
I have a variable in js, and I need to send it to my ManagedBean, what is the most correct way to do this?
You can manipulate the DOM to do what you need, here is an example:
<h:form id="formId">
<h:inputHidden id="x" value="#{bean.x}" />
<h:inputHidden id="y" value="#{bean.y}" />
<h:commandButton value="submit" onclick="getVars()" action="#{bean.method}" />
</h:form>
Your Javascript function:
function getVars() {
// ...
var x = 10;
var y = 20;
document.getElementById("formId:x").value = x;
document.getElementById("formId:y").value = y;
}
In your bean:
private int x;
private int y;
public void method() {
System.out.println("x: " + x);
System.out.println("y: " + y);
// ...
}