首页 - Ajax专区 - 实例应用

用XMLHttpRequest和Struts实现Ajax的在线实例

发布时间: 2007-03-19 12:35    作者: 未知    来源: 未知    浏览:    评论

上一页 1 2 3 4 下一页

Frank W. Zametti有一篇文章,向我们展示了对Ajax(Asynchronous Javascript+XML)技术的应用,尤其是Struts的应用。他认为在应用Ajax中有很多有趣的事情,包括Ajax怎样工作,为什么Ajax会被用到等等。
文章内容如下:

Ajax using XMLHttpRequest and Struts

Frank W. Zammetti

 

About five years ago I worked on a web app project where one of the primary requirements was that it should look, work and feel like a Windows-based fat client.  Now, ignoring the question of why it was not in fact done as a Windows-based fat client in the first place, that can be a rather tough requirement in the world of web development, certainly it was five years ago when not very many examples of such a thing existed.

 

As a result of a number of proof-of-concepts, I stumbled upon some techniques for doing things that were at the time rather atypical approaches to web development.  The end result was an application that, to this day, many people cannot even tell is web-based, aside from the fact that you access it with a browser!

 

Little did I know that only a few years later the basic concepts behind what I had done would re-emerge in the world as something called Ajax.  Ajax is a term coined by the folks at Adaptive Path and is shorthand for Asynchronous Javascript + XML. 

 

This just goes to show, patenting every idea you ever have is indeed a good way to riches!  If only I would have thought what I did was anything special!  But I digress…

 

Google is doing it.  So are many others.  But what is it?  In a nutshell, the Ajax concept, which is not a concrete technology implementation but rather a way of thinking and a set of techniques that go along with the mindset, is concerned with the idea that rebuilding a web page for every user interaction is inefficient and should be avoided.

 

For instance, say you have a web page with two .

 

The Ajax concept is based around something called the XMLHttpRequest component.  All you Microsoft haters get ready to yell because this was a Microsoft creation!  Yes, Microsoft got something right, and did so before anyone else!  Microsoft first implemented the XMLHttpRequest object in Internet Explorer 5 for Windows as an ActiveX object (ok, so they didn’t get it QUITE right!). The Mozilla project later implemented a native version with the release of Mozilla 1.0, and by extension, Netscape 7. Apple has now done the same as of Safari 1.2.  Opera is now following suite with their version 7.60.  All released versions of Firefox contain it as well.

 

Let’s just cut to the chase and get to some code, shall we?

 

The XMLHttpRequest component, being a client-side component, must be instantiated via scripting before it can be user.  Thankfully, the process is as simple as can be… For IE, use the following Javascript:

 

var req = new ActiveXObject("Microsoft.XMLHTTP");

 

…for any other browser, use:

 

var req = new XMLHttpRequest();

 

You will of course want to perform some branching logic in your code.  There are a couple of ways to do it, but I prefer a simple approach, which to me is just an object existence check:

 

var req;

if (window.XMLHttpRequest) { // Non-IE browsers

  req = new XMLHttpRequest();

} else if (window.ActiveXObject) { // IE

  req = new ActiveXObject("Microsoft.XMLHTTP");

}

 

However you choose to do it, after this code executes you will find that the variable req is now a reference to an XMLHttpRequest object.  This object has a number of properties and methods, which are summarized below:

 

上一页 1 2 3 4 下一页

TAG

Smile Big Smile Surprise Stick out tongue Wink Sad Tongue Tied Indifferent Crying Embarrassed Cool Angry Angel Devil [8-|] [:#] [:-*] [:^)] [<:o)] [|-)] Yes Beer Left Hug Music Star Time Snail Pizza Automobile Umbrella Computer Storm [mo] [8o|] [^o)] [+o(] [*-)] [8-)] Coffee No Drinks [Z] Right Hug Cake Broken Heart Gift Wilted Flower Movie Dog Idea Sleep Email Travel Paradise
呢称:

加粗 斜体 下划线 链接 图片 代码 邮件地址 引用 列表

最多只能输入100个字符