Home >Web Front-end >JS Tutorial >How Can I Trigger a JSF Managed Bean from a Native JavaScript HTML DOM Event?
Manipulating JavaServer Faces managed beans during HTML DOM events using native JavaScript can be achieved through various methods. While jQuery offers an easy solution with its $(document).ready() function, JSF provides different approaches that integrate seamlessly with its framework.
The
<h:form> <h:commandScript name="commandName" action="#{bean.action}" render=":results" autorun="true" /> </h:form>
PrimeFaces provides
<h:form> <p:remoteCommand name="commandName" action="#{bean.action}" update=":results" autoRun="true" /> </h:form>
If your application utilizes OmniFaces, you can substitute
<o:form> <o:commandScript name="commandName" action="#{bean.action}" render=":results" autorun="true" /> </o:form>
Another option involves a "hidden form" trick, where a form containing a command button is rendered on the page but hidden using CSS. The button can be triggered using JavaScript's onclick() method.
<h:form>
document.getElementById("form:button").onclick();
For advanced scenarios, you can create a custom UIComponent extending UICommand that generates JSF's native jsf.ajax.request() call. This approach provides full customization and control over the Ajax behavior.
The above is the detailed content of How Can I Trigger a JSF Managed Bean from a Native JavaScript HTML DOM Event?. For more information, please follow other related articles on the PHP Chinese website!