投稿‎ > ‎

a4j:jsFunction

posted Mar 16, 2013, 9:40 PM by Zhang Wenxu   [ updated Jul 19, 2013, 9:03 AM ]

The <a4j:jsFunction> component allows to perform Ajax requests directly from JavaScript code, invoke server-side data and return it in a JSON format to use in a client JavaScript calls.

JavascriptからAjaxリクエストを直接発行できので、とても便利。戻り値はJSONフォーマットでデータであるため、使い易い。

As the component uses Ajax request to get data from server it has all common Ajax Action attributes. Hence, "action" and "actionListener" can be invoked, and reRendering some parts of the page fired after calling function.

AjaxリクエストのAction属性を使える、普通に"action"、"actionListener"を呼び出したり、reRenderしたりできる。

When using the <a4j:jsFunction> it's possible to initiate the Ajax request from the JavaScript and perform partial update of a page and/or invoke the JavaScript function with data returned by Ajax response.

<a4j:jsFunction>を使えば、JavascriptからAjaxリクエストを発行し、ページの一部だけを更新する、または、Ajaxのレスポンスデータを解析するができる。


...
<body onload="callScript()">
      <h:form>
             ...
            <a4j:jsFunction name="callScript" data="#{bean.someProperty1}" reRender="someComponent" oncomplete="myScript(data.subProperty1, data.subProperty2)">
                  <a4j:actionparam name="param_name" assignTo="#{bean.someProperty2}"/>
            </a4j:jsFunction>
            ...
      </h:form>
      ...
</body>
...

The <a4j:jsFunction> allows to use <a4j:actionparam> or pure <f:param> for passing any number of parameters of the JavaScript function into Ajax request.<a4j:jsFunction> is similar to <a4j:commandButton> , but it could be activated from the JavaScript code. It allows to invoke some server-side functionality and use the returned data in the JavaScript function invoked from "oncomplete" attribute. Hence it's possible to use <a4j:jsFunction> instead of <a4j:commandButton> . You can put it anywhere, just don't forget to use <h:form> and </h:form> around it.

<a4j:actionparam> または純粋な <f:param> を使って、<a4j:jsFunction>  がJascriptからサーバーへ任意個数のパラメータを渡せる。<a4j:jsFunction><a4j:commandButton> に似ているが、Javascriptから直接発行できる。"oncomplete" 属性から、Ajaxの戻り値を使って、さらにJavascriptの処理を行える。

<a4j:commandButton> の替わりに、<a4j:jsFunction> を利用できる。<h:form> と </h:form> で囲めば、どこにでも記述できる。




Google+
By Zhang Wenxu
Comments