Jsf calling bean method from input text when pressing enter

前端 未结 1 1680
抹茶落季
抹茶落季 2020-12-13 20:17

JSF 2.0, Mojarra 2.0.1, PrimeFaces 3.4.1

Here is a p:inputText component which is expected to call a backing bean method when the enter key is

相关标签:
1条回答
  • 2020-12-13 21:04

    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).

    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; }"/>
    

    and in backing bean:

    public void test() {
     System.out.println("Pressed enter!");
    }
    
    0 讨论(0)
提交回复
热议问题