基于Struts的AJAX

          首先,我們?nèi)匀皇菍?duì)Struts應(yīng)用來做配置,仍然是在struts-config,xml文件里做配置,如下:

           <action type="com.ajax.CheckAction"
               scope="request" path="/ajax/check">
               <forward name="success" path="/check.jsp"/>
           </action>


          跟普通的Struts應(yīng)用的配置一樣,只是沒有ActionForm的配置。下面是Action類:

           package com.ajax;
           
           import java.io.PrintWriter;
           
           import javax.servlet.http.HttpServletRequest;
           import javax.servlet.http.HttpServletResponse;
           
           import org.apache.struts.action.Action;
           import org.apache.struts.action.ActionForm;
           import org.apache.struts.action.ActionForward;
           import org.apache.struts.action.ActionMapping;
           import org.apache.struts.action.DynaActionForm;
           
           /**
            * @author Administrator
            *
            * TODO To change the template for this generated type comment go to
            * Window - Preferences - Java - Code Style - Code Templates
            */
           public class CheckAction extends Action
           {
            public final ActionForward execute(ActionMapping mapping, ActionForm form,
                     HttpServletRequest request,
                     HttpServletResponse response)
             throws Exception
             {
            System.out.println("haha...............................");
            String username= request.getParameter("username");
            System.out.println(username);
            String retn;
            if("educhina".equals(username)) retn = "Can't use the same name with the old use,pls select a difference...";
            else retn = "congraducation!you can use this name....";
            PrintWriter out=response.getWriter();
                     out.write(retn);
                     out.close();
            return mapping.findForward("success");
             }
            public static void main(String[] args)
            {
            }
           }

          我們可以看到里面的邏輯跟上例中Servlet里的邏輯一樣。最后,我們來看看JSP:

           <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
           <html>
           <head>
           <title>Check.html</title>
           
           <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
           <meta http-equiv="description" content="this is my page">
           
           <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
           
           </head>
           <script type="text/javascript">
            var http_request = false;
            function send_request(url) {//初始化、指定處理函數(shù)、發(fā)送請(qǐng)求的函數(shù)
             http_request = false;
            
          file://開始初始化XMLHttpRequest對(duì)象
             if(window.XMLHttpRequest) {
          file://Mozilla 瀏覽器
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {//設(shè)置MiME類別
             http_request.overrideMimeType('text/xml');
            }
             }
             else if (window.ActiveXObject) { // IE瀏覽器
            try {
             http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
             try {
              http_request = new ActiveXObject("Microsoft.XMLHTTP");
             } catch (e) {}
            }
             }
             if (!http_request) { // 異常,創(chuàng)建對(duì)象實(shí)例失敗
            window.alert("不能創(chuàng)建XMLHttpRequest對(duì)象實(shí)例.");
            return false;
             }
             http_request.onreadystatechange = processRequest;
             // 確定發(fā)送請(qǐng)求的方式和URL以及是否同步執(zhí)行下段代碼
             http_request.open("GET", url, true);
             http_request.send(null);
            }
            // 處理返回信息的函數(shù)
             function processRequest() {
                 if (http_request.readyState == 4) { // 判斷對(duì)象狀態(tài)
                     if (http_request.status == 200) { // 信息已經(jīng)成功返回,開始處理信息
                         alert(http_request.responseText);
                     } else {
          file://頁面不正常
                         alert("您所請(qǐng)求的頁面有異常。");
                     }
                 }
             }
            function userCheck() {
             var f = document.forms[0];
             var username = f.username.value;
             if(username=="") {
            window.alert("The user name can not be null!");
            f.username.focus();
            return false;
             }
             else {
            send_request('ajax/check.do?username='+username);
             }
            }
           
           </script>
           <body>
            <form name="form1" action="" method="post">
           User Name:<input type="text" name="username" value="">&nbsp;
           <input type="button" name="check" value="check" onClick="userCheck()">
           <input type="submit" name="submit" value="/oblog312/submit">
           </form>
           </body>
           </html>

          我們可以看到,JSP基本是一樣的,除了要發(fā)送的url:ajax/check.do?username="+username。 

          posted on 2007-08-13 17:24 劉錚 閱讀(288) 評(píng)論(0)  編輯  收藏 所屬分類: Struts


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導(dǎo)航

          統(tǒng)計(jì)

          留言簿(1)

          文章分類(141)

          文章檔案(147)

          搜索

          最新評(píng)論

          主站蜘蛛池模板: 那坡县| 石林| 汉沽区| 铁力市| 张家港市| 深水埗区| 凤翔县| 六安市| 雅安市| 阳信县| 阳高县| 珠海市| 汾阳市| 桂林市| 特克斯县| 海伦市| 阿拉善盟| 阳城县| 遵义市| 东源县| 瑞昌市| 石楼县| 银川市| 台山市| 沭阳县| 青铜峡市| 瑞安市| 麻栗坡县| 邵阳县| 石城县| 丘北县| 鸡东县| 合水县| 长丰县| 靖边县| 南平市| 南宁市| 阿拉尔市| 南昌县| 柳江县| 阿瓦提县|