title: JSF 2.0 AJAX: Call a bean method from javascript with jsf.ajax.request (or some other way)
body: <p>Some background: I am building a custom JSF component. The component is basically a text editor and it should have a "Save" -button for saving the content string of the editor. As I am using the <a href="http://codemirror.net/" rel="nofollow noreferrer">CodeMirror</a> library, I need to fetch the content (string) from the editor with javascript and send that to the server. Therefore, in this case I cannot use XML-based JS invocation such as <code>f:ajax</code>.</p>
  
  <p>The question: I was planning to send the string with <code>jsf.ajax.request</code>, but it doesn't directly support calling methods on beans. How can I invoke a method in a bean with JSF in AJAX manner?</p>
  
  <p>There at least two ways to get around this:</p>
  
  <ul>
  <li>Include a hidden form to page with hidden inputfield. Update that inputfield from javascript and then call jsf.ajax.request to post that form. Custom actions can be invoced in the property's getter or setter if needed.</li>
  <li>Do the request with raw <code>XMLHttpRequest</code> (or maybe with help from some other JS library). Create a servlet and call that.</li>
  </ul>
  
  <p>Both ways are clumsy and the latter also breaks out of JSF scope.</p>
  
  <p>Am I missing something? How do you do these?</p>
  
  <p>There is a <a href="https://stackoverflow.com/questions/2690461/jsf-2-0-ajax-jsf-ajax-request-to-call-method-not-only-rerender-an-area-of-page">quite similar question</a>, but the answers given only refer to XML-based AJAX invocations. There is also <a href="https://stackoverflow.com/questions/3581499/invoke-backing-bean-method-from-javascript-jsf">another similar question</a>, but that refers to XML-based AJAX calls as well.</p>