Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I update a bean value from an AJAX call?  RSS feed

 
Grae Cullen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

How do I update a bean value from an AJAX call? I suppose I could write servlet to do it. However, this seems like a common problem, so I was wondering if there was some built-in solution.

Thanks,
Grae
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends in part on how you're doing AJAX. RichFaces provides AJAX support integrated into its JSF tagset.

On that platform, AJAX does a partial page submit, optionally responded to with a partial page refresh. Meaning that the response may be for the whole page or just indicated sections of the page. You can limit the set of bean properties that will be updated and likewise for the parts of the display that will be updated. The actual backing bean has no special code in it, although on occasion it might need to allow for the fact that the entire property set might not be updated the way a non-AJAX form request would do it.
 
Grae Cullen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway,

Thanks, for the responce. I don't think were totally on the same page. My knowledge of AJAX is kind of limited. I know about a POST or GET request, thats about it. I could make a post request to a sevlet to change a bean value. However, I thought people must want to change one bean value now and then, and JSF might have a built in servlet, or something like that.

I guess I don't want to submit the whole form, but have JavaScript/AJAX update a variable in the bean value.

Thanks,
Grae
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSF does have a builtin servlet. FacesServlet. However, it's designed to do basic JSF. Add-ons such as RichFaces can be used to fine-tune how it works in order to work with only part of a page instead of the entire page.

AJAX isn't some magical new transport. It's ordinary GET and PUT requests. The difference is that since it's done by JavaScript instead of by browser URL or form request, it can be done while a page is displayed as a communication between the JavaScript and the server instead of as a whole-page request/response. The exact content of the AJAX request/response depends on what mechanisms are helping you and what standards you employ. For example, a lot of "brute force" AJAX uses JSON, since it's easy for JavaScript to parse.

You pretty much have to have assistence when updating JSF beans via AJAX, because JSF's postback payload is fairly complex. The alternative would be to do an "out of band" request that accessed the backing bean through a custom servlet that you wrote that would then access the JSF backing bean in its role as a POJO request, session, or application attribute. People do that, but the more complex the app, the more work it requires. AJAX-aware JSF extensions eliminate a lot of the tedium.

BTW, JSF2 adds some AJAX support into the JSF core. JSF1 is completely unaware of AJAX, so the only way in JSF1 to use AJAX is to employ a JSF-aware tagset such as RichFaces (or IceFaces, etc.) or alternatively, to do it by brute force.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!