按下回车键时 Jsf 从输入文本调用 bean 方法 [英] Jsf calling bean method from input text when pressing enter
问题描述
JSF 2.0、Mojarra 2.0.1、PrimeFaces 3.4.1
这是一个 p:inputText
组件,它应该在按下 Enter 键时调用支持 bean 方法.
Here is a p:inputText
component which is expected to call a backing bean method when the enter key is pressed.
<p:inputText id="commentInput" rendered="#{status.haveComment}"
value="#{statusBean.newComment}"
onkeypress="if (event.keyCode == 13) { onchange(); return false; }">
<f:ajax event="change" listener="#{statusBean.test}" />
</p:inputText>
虽然backing bean有如下方法:
While backing bean has the method of:
public void test(AjaxBehaviorEvent event) {
System.out.println("Pressed enter!");
}
它是按下回车键时的调用方法,但它不止于此;意外行为案例:
It's calling method when enter key is pressed but it has more than this; unexpected behaviour case:
--Click input text
----Type some letters
------Click somewhere else in the page
--------CONSOLE: Pressed enter!
我认为 ajax event=change
以某种方式检测到更改并调用该方法.如何将这个 p:inputText
组件转换为合适的评论接收器组件,如 Facebook 或其他公司拥有的?
I think ajax event=change
detects a change somehow and calls the method. How to convert this p:inputText
component into a proper comment taker component like Facebook or others has?
推荐答案
这就是 onchange
事件在 HTML 中的工作方式.当输入元素中的文本发生更改时会发生这种情况,但会在组件失去焦点时触发(在您的情况下,这是您单击页面中其他位置的那一刻).
This is the way how onchange
event works in HTML. It is happening when text in input element is changed, but is fired when component loses focus (in your case that is the moment when you click somewhere else in the page).
您可以为 test
方法定义 p:remoteCommand
并只写:
You can define p:remoteCommand
for test
method and just write:
<p:remoteCommand name="test" actionListener="#{statusBean.test}"/>
<p:inputText id="commentInput" rendered="#{status.haveComment}"
value="#{statusBean.newComment}"
onkeypress="if (event.keyCode == 13) { test(); return false; }"/>
并在支持 bean 中:
and in backing bean:
public void test() {
System.out.println("Pressed enter!");
}
这篇关于按下回车键时 Jsf 从输入文本调用 bean 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!