You can use the following “hack” to force JS to send information to JSF.
Create an invisible JSF form using <f:ajax> hook:
<h:form prependId="false" style="display:none;"> <h:inputText id="input" value="#{bean.input}"> <f:ajax event="change" execute="@form" listener="#{bean.process}" render=":something" /> </h:inputText> </h:form>
Let JS update the input field and fire the change event:
<script> function somefunction() { var input = document.getElementById('input'); input.value = 'your JSON string'; input.onchange(); } </script>
Running a Java / JSF job in the listener method:
private String input;
Put the desired JSF markup in <h:someComponent id="something"> , which will be re-displayed <f:ajax render=":something"> when the listener completes its task:
<h:panelGroup id="something"> The JSON string was: <h:outputText value="${bean.input}" /> </h:panelGroup>
However, I would prefer to make the webservice call in the constructor or managed bean action method, rather than in JS. Your approach is literally devious.
source share