通过javascript在控制器管理的bean中调用操作方法 [英] Call action methods in controller managed beans through javascript

查看:53
本文介绍了通过javascript在控制器管理的bean中调用操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了减少状态,我尝试减少网页上的commandButtons数量.(我在20个项目的长列表(6 * 20 = 120 commandButtons)下显示了很多commandButtons).因此,我试图找出一种通过javascript传递参数&的方法.调用ManagedBean控制器类中的操作方法.有什么方法可以从javascript&中调用操作方法吗?向他们传递参数?

In order to reducing the state I am trying to reduce the no of commandButtons on my webpages.(I had large no of commandButtons shown under a long list of 20 items (6*20 = 120 commandButtons)). Thus I am trying to figure out a way, through javascript by which I can pass parameters & call the action methods in the ManagedBean controller classes. Is there any way to call action methods from javascript & pass them parameters ?

推荐答案

Richfaces 3.2.0.GA和XHTML作为标记

Richfaces 3.2.0.GA and XHTML as mark up

您可以使用javascript调用a4j:js方法,该方法又从托管Bean调用操作方法.您可以将需要传递的参数设置为一个隐藏变量,设置该变量时会将其值设置为bean中的java变量.

You can use javascript to call an a4j:js method, which inturn calls action method from managed bean. The param which you need to pass can be set to a hidden variable which when set will set the value to the java variable in your bean.

<script>
function onButtonClick(){
    $("#yourValue").val("value");
    actionListenerMethod();
}
</script>

<a4j:jsFunction name="actionListenerMethod" 
    actionListener="#{yourManagedBean.actionMethod}"
    oncomplete="scriptOnComplete();">
</a4j:jsFunction> 

<h:inputHidden id="yourValue"
    value="#{yourManagedBean.yourValue}" />

托管bean:

public void actionMethod(ActionEvent event){
    if(yourValue == "something"){
        /*your action goes here*/
    }
}

否则,您可以使用操作参数绕过此隐藏变量

Otherwise you can by-pass this hidden variable by use of action param

<script>
    function onButtonClick(){
        actionListenerMethod("value");
    }
</script>

<a4j:jsFunction name="actionListenerMethod" 
    actionListener="#{yourManagedBean.actionMethod}"
    oncomplete="scriptOnComplete();">
    <a4j:actionparam name="param1"
        assignTo="#{yourManagedBean.yourValue}" />
</a4j:jsFunction>

在后一种情况下,只有在经过操作的bean完成后才能设置操作参数,在这种情况下,您可以使用Action属性来调用您的操作方法,而不是操作侦听器.这将帮助您设置参数,然后调用action方法.

In the later case the action param might get set only after the manged bean gets completed, in this case you can use Action atribute to call your action method instead of an action listener. This will help you to set the param and then call the action method.

<a4j:jsFunction name="actionListenerMethod" 
    action="#{yourManagedBean.actionMethod}"
        oncomplete="scriptOnComplete();">
        <a4j:actionparam name="param1"
            assignTo="#{yourManagedBean.yourValue}" />
</a4j:jsFunction>

这篇关于通过javascript在控制器管理的bean中调用操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆