当按下回车键时,jsf从输入文本中调用bean方法 [英] Jsf calling bean method from input text when pressing enter

查看:113
本文介绍了当按下回车键时,jsf从输入文本中调用bean方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSF 2.0,Mojarra 2.0.1,PrimeFaces 3.4.1

这是一个p:inputText组件,预计在按下Enter键时将调用backing 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>

后备豆具有以下方法:

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:

public void test() {
 System.out.println("Pressed enter!");
}

这篇关于当按下回车键时,jsf从输入文本中调用bean方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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