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

Ajax using XMLHttpRequest and Struts

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3196 更新时间:2009/4/23 11:30:49
Notice again the nearly identical script elements in the <head>.  Here we are actually making a request to a Struts Action.  This action returns the actual HTML for the table, sorted appropriately.  There are other ways to accomplish this that don’t require you to generate HTML in the Action, but this is a quick-and-dirty approach that works quite well.  We make an initial call to the Action when the page loads so that we have a table to begin with.  Clicking on any of the column headers will result in the table being sorted and redrawn.

 

Let’s take a look at another example from the webapp, this time the RSS feed parser:

 

<html>

<head>

<title>Example 6</title>

</head>

 

<script>

 

  var req;

  var which;

 

  function retrieveURL(url) {

    if (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

        // We''''re going to get a list of all tags in the returned XML with the

        // names title, link and description.  Everything else is ignored.

        // For each that we find, we''''ll constuct a simple bit of HTML for

        // it and build up the HTML to display.  When we hit a title,

        // link or description element that isn''''t there, we''''re done.

        xml = req.responseXML;

        i = 0;

        html = "";

        while (i >= 0) {

          t = xml.getElementsByTagName("title")[i];

          l = xml.getElementsByTagName("link")[i];

          d = xml.getElementsByTagName("description")[i];

          if (t != null && l != null && d != null) {

            t = t.firstChild.data;

            l = l.firstChild.data;

            d = d.firstChild.data;

            html += "<a href=\"" + l + "\">" + t + "</a><br>" + d + "<br><br>";

            i++;

          } else {

            i = -1;

          }

        }

        document.getElementById("rssData").innerHTML = html;

      } else {

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

      }

    }

  }

 

</script>

 

<body>

 

<h1>Example 6</h1>

RSS example.<hr>

<p align="right"><a href="home.do">Return home</a></p><br>

This example is a more real-world example.  It retrieves an RSS feed from one

of three user-selected sources, parses the feed and displays the headlines

in clickable form.  This demonstrates retrieving XML from a server and

dealing with it on the client.

<br><br>

<b>Note that the RSS feed XML is actually stored as files within this

webapp.  That is because some browsers will not allow you to retrieve

content with XMLHttpRequest outside the domain of the document trying to

do the call.  Some browsers will allow it with a warning though.</b>

<br><br>

<form name="rssForm">

  <select name="rssFeed" onChange="retrieveURL(this.value);">

    <option value=""></option>

    <option value="cnn_rss.xml">CNN Top Stories</option>

    <option value="slashdot_rss.xml">Slashdot</option>

    <option value="dans_rss.xml">Dan''''s Data</option>

  </select>

</form>

<hr><br>

<span id="rssData"></span>

<br>

 

</body>

</html>

 

The first thing to note is that the RSS feed XML files are actually local files included with the webapp.  A true RSS reader using XMLHttpRequest wouldn’t be possible because it would be dealing with domains outside its own.  However, one way around this would be to write an Action that retrieves the feed from the true URL and then returns it to the page, and that is demonstrated in example 7.  Other than the Action acting as the proxy to retrieve the RSS feed, the code for the page itself is the same.

 

In any case, the example works very similarly to all the others except that we see an example of XML parsing here in the event handler fu

上一页  [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……
    咸宁网络警察报警平台