Passing JS variable to ManagedBean

0

I have a variable in js, and I need to send it to my ManagedBean, what is the most correct way to do this?

    
asked by anonymous 03.04.2018 / 14:21

1 answer

1

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); 
// ... 
}
    
03.04.2018 / 14:36