Join the DZone community and get the full member experience.Join For Free
you can read the introduction to sitemesh article if you're interested in learning more about sitemesh's basic features. by default, sitemesh decorates text/html responses and ignores any other content type (e.g. image/gif). it also contains an <excludes> configuration element that allows you to turn off decoration when a url matches a certain pattern. for example, the following allows you to disable decoration when "ajax=true" is passed in the url.
to optimize the loading of an application so the common elements aren't loaded for each page, it should be possible to create an ajaxified body where the primary content area (pca) of the site is loaded via ajax rather than refreshing the whole page. the header, footer and navigation bar often stays the same from page-to-page, so it doesn't seem to make a whole lot of sense to load them each time the page loads. the image below shows the pca (of an appfuse light application) as a grey square with a red border.
implementing an ajaxified body consists of the following steps:
- adding sitemesh and moving common elements to a decorator.
- remove common elements from each individual page (if you're using includes).
- configure sitemesh so decoration is disabled when the requested url contains "ajax=true".
- add xhr success handling to replace the pca with the loaded content.
- add xhr error handling to go to the url normally when response.status != 200.
- inspect the response html for <title> element and replace document.title if exists.
- inspect the response html for <head> element and append to current if exists.
as a proof of concept, i created a prototype using appfuse light (prototype/scriptaculous for ajax). you can see a demo at the following url. you can also download a patch or the source for this project.
below are a number of things i discovered while writing this prototype:
- the hardest part of implementing this seems to be coding the exceptions. it's possible you'll have some links with existing onclick handlers and you may have to disable "ajaxifying links" for those links.
- a progress indicator is important or the page might load so fast that the user doesn't visually detect it changed. this can lead to a worse user experience because they don't see the flash of the blank page they're used to when a page load occurs.
- while forms can be submitted via ajax, there's no harm in leaving existing form behavior in place where the full site is reloaded after submitting a form.
i'm sure my implementation can be improved, but i'm also curious to see what you think of this idea. i know it's not revolutionary, but it's something i'm considering adding by default to appfuse and appfuse light. do any ajax frameworks do something like this out-of-the-box?
Opinions expressed by DZone contributors are their own.