aspose file tools*
The moose likes JSF and the fly likes Update a region with new content from Ajax response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Update a region with new content from Ajax response" Watch "Update a region with new content from Ajax response" New topic
Author

Update a region with new content from Ajax response

V Chad
Greenhorn

Joined: Sep 28, 2010
Posts: 2
Hi Guys,

I am relatively new to JSF and I am using JSF2.0 with Primefaces on Tomcat with Netbeans6.9.

I have a page with registration form and on submit of the form using AJAX, I want to replace the form area with a new different form. I am using p:commandButton to submit the form.

Right now I am getting just the components existing on the page updated with new values after the submit. If I set ajax="false", I get the new form in the browser with the full page refresh. I need directions so that the AJAX response just replaces a specific region on the page with HTML from the AJAX response.

Thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

Welcome to the Javaranch, "V"!

JSF AJAX support isn't really about updating HTML. In fact, JSF isn't really about HTML at all. JSF is a general Model/View/Controller framework that uses pluggable renderers to output results in a format appropriate for the target. Most often, this target is a web browser, so the renderers produce HTML.

AJAX itself is about sending requests and responses to a server, so it, too isn't really about dynamic replacement of HTML.

OK, so much for what things aren't.

Because JSF internally works with its own view display structures, simply generating HTML forms by brute force isn't going to be much fun. And generating their JSF equivalents in Java code can be rather tedious.

On the other hand, a common practice for cases where you have multiple forms that you progress through is to hide all the forms except the one that the user currently needs. In the case of AJAX, this can be done by setting a property indicating which form to display and referencing it using the "rendered" attribute of the form container element. If the "rendered=" expression returns false, that form will be hidden and if it returns true, that form will be displayed. Because forms are self-contained, the presence of other hidden forms on the view is not a problem.

Of course, there's one more critical element that has to be allowed for. When you're working with JSF AJAX doing partial page rendering, you have to indicate which parts of the view are to be updated. If you use the rendered attribute to make a form visible, but the form isn't part of one of the areas to be re-rendered after the AJAX request, then the form's visibility in the browser window won't change.


Customer surveys are for companies who didn't pay proper attention to begin with.
V Chad
Greenhorn

Joined: Sep 28, 2010
Posts: 2
Hi Tim,

Thanks for the detailed explaination. I got the stuff working with the tricks mentioned by you. Actually the problem was that I expected (or may be over expected) that if the update is set to true for a block, the AJAX response (whatever it is) will be pasted in that block magically. I now realize that doing this would create problem if we want to update more than one blocks after the AJAX request.

Thanks.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

V Chad wrote:Hi Tim,

Thanks for the detailed explaination. I got the stuff working with the tricks mentioned by you. Actually the problem was that I expected (or may be over expected) that if the update is set to true for a block, the AJAX response (whatever it is) will be pasted in that block magically. I now realize that doing this would create problem if we want to update more than one blocks after the AJAX request.

Thanks.


Actually, it's not. I haven't played with the JSF2 AJAX stuff yet, but that's a function that was implemented in the RichFaces tagset quite some time ago and I slice and dice partial page updates all the time. I just specify the IDs of the areas of the page to be updated. The rest of the page remains unchanged.
 
Don't get me started about those stupid light bulbs.
 
subject: Update a region with new content from Ajax response