11 July 2009

How to submit InputText value with Enter Key in ADF Faces

My requirement was to have a input text box value submitted with enter key - without clicking any

button, invoke business logic and stay on the same page.
This is how it can be achieved


Firstly, I added valueChangeListener to the input box and associated it with a method in a managed

bean

Page XML:

add these

attributes to inputText tag

valueChangeListener="#{myBean.myMethod}"
onkeypress="return submitme()"

Secondly, I added JavaScript to the page, for submitting the form on click of enter key,
I have to name the form as myform in order to make this script work.

JavaScript

function submitme() {
if (window.event.keyCode == 13)
{
document.myform.submit();
} else {
return false;
}
}


And finally, this is the backing bean code. Here I am calling gobutton_action method, which will take

care of the business logic that needed to be invoked on submit. I can bind it with the a method of

the Application Module. You can write a code that invokes an AppModule method.


myBean.java

public void myMethod(ValueChangeEvent valueChangeEvent)
{
System.out.println("I am in Bean");
gobutton_action();
}



Now I can enter value in the textbox and hit enter key to submit the value.