基于Struts的AJAX

首先,我们仍然是对Struts应用来做配置,仍然是在struts-config,xml文件里做配置,如下:

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


跟普通的Struts应用的配置一样,只是没有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) {//初始化、指定处理函数、发送请求的函数
   http_request = false;
  
file://开始初始化XMLHttpRequest对象
   if(window.XMLHttpRequest) {
file://Mozilla 浏览器
  http_request = new XMLHttpRequest();
  if (http_request.overrideMimeType) {//设置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) { // 异常,创建对象实例失败
  window.alert("不能创建XMLHttpRequest对象实例.");
  return false;
   }
   http_request.onreadystatechange = processRequest;
   // 确定发送请求的方式和URL以及是否同步执行下段代码
   http_request.open("GET", url, true);
   http_request.send(null);
  }
  // 处理返回信息的函数
   function processRequest() {
       if (http_request.readyState == 4) { // 判断对象状态
           if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
               alert(http_request.responseText);
           } else {
file://页面不正常
               alert("您所请求的页面有异常。");
           }
       }
   }
  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基本是一样的,除了要发送的url:ajax/check.do?username="+username。 

posted on 2007-08-13 17:24 刘铮 阅读(280) 评论(0)  编辑  收藏 所属分类: Struts


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

留言簿(1)

文章分类(141)

文章档案(147)

搜索

最新评论