转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
Ajax using XMLHttpRequest and Struts         ★★★★

Ajax using XMLHttpRequest and Struts

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3197 更新时间:2009/4/23 11:30:49
nction.  This is a simplified example in that we’re simply ignoring any tags except those that describe each headline.  In a true example (i.e., one that was retrieving more complex XML), the parsing code would be accordingly more complex, but I leave that as an exercise to the reader.

 

Let’s finish up by examining the script for the example that demonstrates submitting data with a request.  Just the script in the <head> is:

 

  var req;

  var which;

 

  function submitData() {

    // Construct a CSV string from the entries.  Make sure all fields are

    // filled in first.

    f = document.theForm.firstName.value;

    m = document.theForm.middleName.value;

    l = document.theForm.lastName.value;

    a = document.theForm.age.value;

    if (f == "" || m == "" || l == "" || a == "") {

      alert("Please fill in all fields first");

      return false;

    }

    csv = f + "," + m + "," + l + "," + a;

    // Ok, so now we retrieve the response as in all the other examples,

    // except that now we append the CSV onto the URL as a query string,

    // being sure to escape it first.

    retrieveURL("example5Submit.do?csv=" + escape(csv));

  }

 

  function retrieveURL(url) {

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

      req = new XMLHttpRequest();

      req.onreadystatechange = processStateChange;

      try {

        req.open("GET", url, true);

      } catch (e) {

        alert(e);

      }

      req.send(null);

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

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

      if (req) {

        req.onreadystatechange = processStateChange;

        req.open("GET", url, true);

        req.send();

      }

    }

  }

 

  function processStateChange() {

    if (req.readyState == 4) { // Complete

      if (req.status == 200) { // OK response

        document.getElementById("theResponse").innerHTML = req.responseText;

      } else {

        alert("Problem: " + req.statusText);

      }

    }

  }

 

In this example we are constructing a simple comma-separated string based on the inputs of the user.  You can of course construct an XML document and send that, and in fact that is the more common example.  But that is part of the reason I DIDN’T do it: I wanted to show you that you do not have to send XML when using XMLHttpRequest.  In fact, this example does nothing more than append the CSV string onto the URL.  There are numerous examples on the web for constructing a true XML document and submitting that using the XMLHttpRequest.send() method, and I highly recommend looking those up if you intend to use that approach.

 

I hope that this rather short article and accompanying webapp have given you a good starting point from which to explore what the XMLHttpRequest object provides.  Before I close I want to also point out that the Ajax concept itself does NOT require you to use XMLHttpRequest.  You can get very much the same basic effect in other ways, including code in a hidden frame taking the place of XMLHttpRequest.  This is in fact the technique I used five years ago in the project I spoke of at the beginning.  However, XMLHttpRequest does make the underlying Ajax concept far easier to implement, and more standard.  Just look to Google as a great example of the power of this technique.

 

However, I caution anyone from thinking this is the way all web apps should be developed.  I do not for a second advocate this as the One True Solution (for all you LOTR fans out there!).  It is a good approach in many cases, but will not be appropriate in others.  If reaching the maximum possible audience is your goal, you would want to stay away from this.  If a user disabling scripting in their browser might be a concern (and your site wouldn’t be any good without it), this probably isn’t a good answer either.  There are other reasons to stay away from it in some situations, but the bottom line is treat it like any other tool in your toolbox: it will be right for some jobs, maybe not so for others.  After all, you don’t drive a nail with a glue gun, right??

 

In any case, I hope this has given you some food for thought.  Have fun!

 

About the author

Frank W. Zammetti is a Web Architect Specialist for a leading worldwide financial institution during the day and a PocketPC developer at night.  He is the Founder and Chief Software Architect of Omnytex Technologies.  He has over 10 years of professional development experience, and nearly 15 more of “amateur” experience, and he has been developing web-based applications (Intranet applications mostly) almost exclusively for nearly 7 years.  Frank holds numerous certifications including SCJP, MCSD, CNA, i-Net+, A+, CIW, MCP and numerous BrainBench certifications.  He is a contributor to a number of open-source projects, including DataVision, as well as having started two, including the Struts Web Services Enablement Project (search strutsws on SourceForge, or http://sourceforge.net/projects/strutsws/).  Frank’s resume is available online at http://www.zammetti.com/resume.

 

Sample webapp: http://www.omnytex.com/articles/xhrstruts/xhrstruts.zip

 

PDF copy of this article: http://www.omnytex.com/articles/xhrstruts/xhrstruts.pdf

上一页  [1] [2] [3] [4] 


[Web开发]AJAX来判断dedecms用户是否登录  [系统软件]Explanation of UFT-8 and Unicode
[系统软件]Using dllimport and dllexport in C++ Classes  [常用软件]神奇 我家的照片会唱歌 照片会唱歌
[VB.NET程序]VB.NET and C# 语法比较手册  [Delphi程序]IntraWeb And ActiveForms
[Delphi程序]different between BPL and DLL  [VB.NET程序]How to setup a basic Struts project using Ecli…
[网页制作]Classes and Objects in PHP5  [网页制作]新手解读:认识XML,AJAX,SNS,Tag
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台