GeeCON Prague 2014*
The moose likes JSF and the fly likes A question on ability to have the server write text back to inputText components on the page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "A question on ability to have the server write text back to inputText components on the page" Watch "A question on ability to have the server write text back to inputText components on the page" New topic
Author

A question on ability to have the server write text back to inputText components on the page

Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
It has been QUITE a while since I have bee to the Ranch! I finally got my SCJP 6 certification AND a job in the Java world. I find myself assigned a task at the moment that I am not certain how to handle and thought I might at least ask for ideas or direction here.

The application is a legacy app here at the center, uses JSF 1.2 straight up. ONe of the pages here has two inputText components the upper is a barcode box, the lower is a description of the box. What they want to see happen is: when a barcode is entered into the upper inputText box a handler is executed that detects the change in barcode, attempts to fetch it from the database and, if it is there, it tries to grab the existing description and writes it to the property referenced by the existing "description inputText box" as a hint. I then get the context, get the currentInstance and render the page but, the text never appears in the box on the page. ( I hope you can muster some pity, I really don't know JSF ... I was just assigned to try this and am doing the best I can to learn at lightning speed!).

Using "onblur" I got the handler part working. I can actually debug step into the handler, see it take the barcode, see it fetch the existing tray, see it grab the description and put it in the property. THAT much I have working.

Is there anything that I can do from the server that will get that description text string from the database written into that inputText box. THAT way the users know that the tray exists and they will need to add new samples/primers to it.

Is there a way to do that?

Just like Gene Wilder in "Blazing Saddles" I will appreciate "all the help I can get!!!". Thanks!!!

Bob


------------------------
Bob
SCJP - 86% - June 11, 2009
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16095
    
  21

In JSF2, this is a straight-up AJAX function. How you do it in JSF1 depends on what resources you have. If the app is engineered with RichFaces, PrimeFaces, or one of the other AJAX-supporting extension tagsets, it's also very easy. If not, then things get more challenging.

Here's how the AJAX-based solution works:

1. You assign an AJAX handler to the barcode input control. When a value is entered into that control, the AJAX client fires. It submits a request to the webapp server. This is typically a partial form request.

2. The AJAX event listener method on the server operates according to the standard JSF lifecyle, which means that the barcode value property in the backing bean will have been updated from the request by JSF automatically.

3. Therefore, the AJAX event listener method can lookup that barcode in the database and update the backing bean's property for the descriptive text with the result. You can also, of course, update other properties as well, in case you want to display unit price, quantity on hand, or whatever.

4. When the AJAX event listener completes, JSF's "render response" lifecycle phase posts the updated backing bean properties back to the user's webpage. And that's all there is.

You can do all of the above without AJAX, but in that case, you'd have to submit the entire form and the entire webpage would be updated. Which is basic page operation.


Customer surveys are for companies who didn't pay proper attention to begin with.
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Thanks for the response. If I may, let me ask one more question that was prompted by these two statements:
{
3. Therefore, the AJAX event listener method can lookup that barcode in the database and update the backing bean's property for the descriptive text with the result. You can also, of course, update other properties as well, in case you want to display unit price, quantity on hand, or whatever.

4. When the AJAX event listener completes, JSF's "render response" lifecycle phase posts the updated backing bean properties back to the user's webpage. And that's all there is.
}

those two statements seem to indicate that, if I were in a "listener" handler method on the server and could get the domain object and updated properties in the backing bean AND THEN executed a "RenderResponse" , that the new properties would show up in their associated components on the page. I can verify with debugger that a) I am grabbing the domain object and updating the property in the backing bean for the "description text" AND am executing a "FacesContext.getCurrentInstance().renderResponse();" the question is..... "is that enough!!!". Should this (if properly done) work or does the art of "getting text from the server code stuffed into an input box as a hint" REQUIRE client side support of some kind?

I hope that I am being clear, but I probably sound like an idiot!!! Oh well........

But thanks for looking....
Bob
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16095
    
  21

Is that enough? No, it's TOO MUCH!

You do not have to get the FacesContext for anything. The Render Response phase is part of the JSF lifecycle processing and it is executed automatically. All you need is POJO code that takes the backing bean's bar code value property, looks it up in the database and sets the backing bean's description property.

Everything else is automatically done for you.
 
GeeCON Prague 2014
 
subject: A question on ability to have the server write text back to inputText components on the page