Java 单击某个按钮时调用托管 bean 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20877952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:56:46  来源:igfitidea点击:

Calling a managed bean method when some button is clicked

javajsfprimefacesbacking-beans

提问by onlinenaman

I am new to JSF and want to have a button, which when clicked, will call a method of the backing bean. Is this possible or I have to use AJAX or something? I tried things like

我是 JSF 的新手,想要一个按钮,单击该按钮时将调用支持 bean 的方法。这是可能的还是我必须使用 AJAX 之类的?我试过这样的事情

<button onclick="#{myManagedBean.myMethod()}">MyButton</button>

But they didn't work. What's the normal way of doing it?

但他们没有工作。通常的做法是什么?

Edit:
That button is created by JqueryUI, and I am unable to change the button's type to commandButton. I am able to do only two customizations-
1. call javascript functions from that button
2. change the target of the form (inside which the button resides)
So, how can I call the backing bean method from these two ways, like from Javascript or on form submit?

编辑:
该按钮是由 JqueryUI 创建的,我无法将按钮的类型更改为 commandButton。我只能进行两个自定义 -
1. 从该按钮调用 javascript 函数
2. 更改表单的目标(按钮所在的内部)
那么,我如何从这两种方式调用支持 bean 方法,例如从 Javascript或在表格提交?

回答by Syam Kumar S

you have to use jsf command button component

你必须使用 jsf 命令按钮组件

<h:commandButton value="MyButton" action="#{myManagedBean.myMethod()}"></h:commandButton>

回答by thiyaga

In JSF you have action parameter for the JSF componenents which can be bound to a method in your managed bean. using this method binding you can invoke the method of your managed bean.

在 JSF 中,您可以将 JSF 组件的 action 参数绑定到托管 bean 中的方法。使用此方法绑定,您可以调用托管 bean 的方法。

<h:commandButton value="click" action="#{managedbean.method}"/>

回答by Adarsh

onClick is used to invoke a client-side scripting function like java script. In order to invoke a method on the server side(backing bean), you need to use the action attribute of the commandButton component. JSF provides a component h:commandButton for this purpose. The syntax for using the commandButton to invoke a method in the backing bean is

onClick 用于调用客户端脚本函数,如 java 脚本。为了调用服务器端的方法(支持 bean),您需要使用 commandButton 组件的 action 属性。JSF 为此提供了一个组件 h:commandButton。使用 commandButton 调用支持 bean 中的方法的语法是

 <h:commanButton value="button" action="#{myBean.methodName()}"/>

The method that you are invoking should be of the form String methodName(), where the String value returned represents the outcome used by the navigation handler to determine what page to display next.

您正在调用的方法应该采用 形式String methodName(),其中返回的字符串值表示导航处理程序使用的结果,以确定接下来要显示的页面。

EDIT:

编辑:

In order to call a backing bean method from your button, use the onClick attribute of your button to call a javascript function. You would need to create a hidden button in your form which invokes the required backing bean method. In the javascript function, use the below code to click the hidden button and thereby invoke the backing bean method.

为了从按钮调用支持 bean 方法,请使用按钮的 onClick 属性来调用 javascript 函数。您需要在表单中创建一个隐藏按钮来调用所需的支持 bean 方法。在 javascript 函数中,使用以下代码单击隐藏按钮,从而调用支持 bean 方法。

  document.getElementById('form:button').click();

回答by Lampard

Actually, Adarsh already answer very well, what I want to add here is an optional choice to calling the JSF backing bean function with jQuery, which is also convenient and you can add more complex logic when click button with help of jQuery.

其实Adarsh已经回答的很好了,我这里想补充的是用jQuery调用JSF backing bean函数的一个可选选项,这也很方便,你可以在jQuery的帮助下单击按钮时添加更复杂的逻辑。

You can use JSF h:commandButton, additionally, you can add an actionattribute in element which calling your JSF backing bean function.

您可以使用 JSF h:commandButton,此外,您可以action在元素中添加一个属性来调用您的 JSF 支持 bean 函数。

E.g <h:commandButton value="Update" id="update" action="#{myPageBean.updateMyPage}"></h:commandButton>

例如 <h:commandButton value="Update" id="update" action="#{myPageBean.updateMyPage}"></h:commandButton>

You may already notice the onClick()or similar event not shown in above example, actually this part job will handle by jQuery as event handling and process.

您可能已经注意到onClick()上面示例中未显示的或类似的事件,实际上这部分工作将由 jQuery 处理作为事件处理和处理。

E.g Find your button with var updateButton = $(document.getElementById("myPage:update"));or var updateButton = $(#"myPage\\:update"));Then register click event on your button with jQuery click()function as updateButton.click(function() {...do whatever you want here...});

例如,使用var updateButton = $(document.getElementById("myPage:update"));或找到您的按钮,var updateButton = $(#"myPage\\:update"));然后使用 jQueryclick()函数在您的按钮上注册单击事件作为 updateButton.click(function() {...do whatever you want here...});

The final effect should be when you click your JSF page button, it will call JSF backing bean method as updateMyPage, hope it will help you.

最终的效果应该是当您单击 JSF 页面按钮时,它会调用 JSF 支持 bean 方法 as updateMyPage,希望对您有所帮助。