views:

65

answers:

3

Hi, I have a website ( based on JSP/Servlets ,using MVC pattern), and I want to support AJAX-based website and basic HTML-based website. website visitors should be able to change the surfing mode from Ajax to basic HTML and vise versa, - as it applies in Google-mail.

The Questions :

  • What is the best way to achieve this goal easily?
  • Should I design two views for each page?

I use JQuery and JSON as the result of this answer.

+5  A: 

Think of the html version as the foundation. Build this first.

Then overlay the additional ajax functionality as an optional layer on top of this, intercepting the default html behaviours as necessary. No need for two views, just a single view that gradually adds enhanced funtionality depnding on available technology and/or user preference.

graphicdivine
+4  A: 

You need Unobtrusive Javascript, which is part of Progressive Enhancement.

First, start creating a fully functional webapplication without any line of Javascript. Once you got it to work, then start writing Javascript code which "takes over" the raw HTML work without changing any line of HTML/CSS. In the server side code you need to add logic which recognizes whether the request has been fired by JavaScript or not and return response accordingly. You can test both cases by en/disabling JavaScript in the webbrowser. In Firefox it's easy with Web Developer Toolbar.

For example, you have a list of mails with all HTML links which should show the mail body:

<a href="mail/show/1" class="show">Message title</a>

Without JavaScript, this would fire a HTTP request to the servlet which loads the mail identified by 1, forwards the request to a JSP which hides the message list in the view and shows the mail in the view.

With JavaScript/jQuery, you need to write code which does exactly the same with help of Ajax, e.g.:

$('a.show').click(function() {
    $.getJSON(this.href, callbackFunctionWhichHidesListAndShowsMail);
    return false;
});

In the server side you have to distinguish between normal requests and ajax requests so that you can return response accordingly.

boolean ajax = "XMLHttpRequest".equals(request.getHeader("x-requested-with"));

// ...

if (ajax) {
    writeJson(response, mail);
} else {
    request.setAttribute("mail", mail);
    request.getRequestDispatcher("/WEB-INF/mail.jsp").forward(request, response);
}

Finally, to give the user an option to switch between the modes manually, you have to set a cookie or preferably (since cookies are disableable) pass some information in URL (pathinfo or request parameter) which forces the server to disable emitting the <script> lines.

BalusC
… and then to let a user pick the basic HTML version, have a cookie setting that just doesn't serve up the JS that does Ajax (or anything that depends on it).
David Dorward
[Progressive enhancement](http://en.wikipedia.org/wiki/Progressive_enhancement) is another term that illustrates what you want to achieve ... build the basics without ajax first and then layer extra functionality on top of that to enhance the user experience.
ChrisR
You are both right, I updated the answer accordingly.
BalusC
Thanks guys. But what about the links (href content) in any <a> element,is it mandatory to change them from href="#" to href="my_link" if the version is basic html and vise versa if it is Ajax version? or I just leave them with their link and the javascript code will ignore the "href content" if the version is Ajax by handle the event onClick?
BugKiller
Do certainly **not** change any line of HTML. The `return false;` in Javascript function already takes care about blocking the link's default action. Seeing your skills as far, I strongly recommend to learn the technologies one by one, step by step. It's otherwise going to be confusing and opaque.
BalusC
OK,I'll do.. Thanks guys -specially BalusC- for your helps.
BugKiller
You're welcome.
BalusC
+2  A: 

You are quite sensibly attempting Progressive Enhancement. There is an excellent article here A List Apart: Understanding Progressive Enhancement which I must give credit to this SO Answer for; Graceful Degredation when to consider. I consider Graceful Degredation to be the more negative way of looking at the problem of supporting different browser capabilities; What is the difference between Progressive Enhancement and Graceful Degradation?

Dave Anderson