I'm builiding a scheduling application using JSF 2.0 where users can add items to a calendar then edit those objects.
I'm using AJAX quite extensively to keep the page from refreshing.
The problem I am having is to get a return value from the function that is called using AJAX.
<!-- ... form to fill -->
<h:commandButton value="Insert">
<f:ajax execute="formField1 formField2..."
listener="#{myBean.insert()}" onevent="processSave"/>
</h:commandButton>
This successfully calls my JavaScript function processSave()
.
myBean.insert()
returns the id
of the newly inserted row in the database
public String insert() {
//... inserting data into database
id = {id from database is obtained here}
System.out.println("I have the ID here : " id); //this works
return id;
}
I've been trying to get this from the response object within JavaScript in the processSave()
function.
processSave(data) {
if(data.status == "begin") {
// ation done here
} else if(data.status == "complete") {
// more actions done here
} else if(data.status == "success") {
//I am trying to get the ID here
//I've tried looking into data.responseXML, but to no avail.
}
}
Is what I am trying possible using the current technologies?
I think it would be possible to have a field in the page updated using the render
component of the AJAX call, then use Javascript to go get the value. But I'm thinking this wouldn't be as clean?
Thank you!
It is possible. And more easily done with primefaces.
Follow this example, you may find something useful there.