How to use <a4j:jsFunction><a4j:actionparam>

谁说胖子不能爱 提交于 2019-12-06 01:44:42

问题


I'm trying to use:

<script type="text/javascript">
      function myfunc() {
         var param = 4;
         alert("OK");
      }
</script>

I call the function like this:

<a4j:jsFunction name="myfunc">
    <a4j:actionparam name="param" assignTo="#{MyBean.myfield}"/>
</a4j:jsFunction>

But it does not work. In what may be the reason?


回答1:


You misunderstood the purpose of <a4j:jsFunction>. It autogenerates a JavaScript function which you can then call from any JavaScript code in your view.

Your example,

<a4j:jsFunction name="myfunc">
    <a4j:actionparam name="param" assignTo="#{MyBean.myfield}"/>
</a4j:jsFunction>

will autogenerate the following function

<script>
    function myfunc(param) {
        // Here some specific JSF Ajax script which assigns "param"
        // to a managed bean property #{MyBean.myfield}
    }
</script>

You do not need to define it yourself. You only need to invoke it yourself from some JavaScript code elsewhere. For example,

<span onclick="myfunc(4)">click here to set 4 in MyBean.myfield</span>

or

<script>
    function someOtherFunction() {
        var param = 4;
        myfunc(param);
    }
</script>

which is in turn to be used like

<span onclick="someOtherFunction()">click here to call someOtherFunction() which will in turn set 4 in MyBean.myfield</span>

See also:

  • <a4j:jsFunction> component reference
  • <a4j:jsFunction> showcase example



回答2:


<a4j:jsFunction 

is not used to call an function, it is used to define an function.

So, if MyBean.myfield is an int-field you can set the value 2 using:

<script>myfunc(2);</sript>



回答3:


There's a bunch of different ways to call that function.

Two you will find particularly useful are:

This:

<body onload="myfunc();">

Example: http://ultimatemmo.webege.com/Test.html

and this:

<a href="#" onclick="myfunc();">Click here to execute function</a>

Example: http://ultimatemmo.webege.com/Test2.html

Edit: added examples.




回答4:


According you snippet of code, you have never called your function. Add myfunc(); within your script tag.



来源:https://stackoverflow.com/questions/7580703/how-to-use-a4jjsfunctiona4jactionparam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!