Jsf inputText onkeyup event handler

In PrimeFace's inputText, I need to call function ‍‍‍f1() when any key is pressed.

To implement this, in xhtml file I have:

<p:inputText id="userName" onkeyup="#{myBean.f1()}" >

And in my MyBean.java i have declared this function:

public void f1()
{
// somecode
}

‌But this code throws an exception:

javax.faces.FacesException: javax.el.ELException: /index.xhtml @103,191 onkeyup="#{MyBean.f1()}": java.lang.NullPointerException

Upvotes: 3

Views: 20218

Answers (1)

Daniel
Daniel

Reputation: 37051

If you want to call a java method you should use p:ajax event instead of onkeyup attribute

<p:inputText id="userName">
     <p:ajax event="keyup" listener="#{myBean.f1}"></p:ajax>
</p:inputText>

onkeyup : Client side callback to execute when a key is released over input element.

in other words onkeyup is for calling js functions like onkeyup="alert('hello')"

about the exception you got : its cause you page tried to execute the f1 method on page load and not on keyup event - for example if you f1 method would have return a string that string would replace the #{myBean.f1()} and your generated page would look like this:

... onkeyup="string value returned from f1 method" ...

Upvotes: 8

Related Questions