所有的JS都跟上一个例子一样,不同的只是对返回值的操作,这次是用alert来显示:alert(http_request.responseText);
我们来看处理逻辑JSP:
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%
String username= request.getParameter("username");
if("educhina".equals(username)) out.print("用户名已经被注册,请更换一个用户名。");
else out.print("用户名尚未被使用,您可以继续。");
%>
非常简单,先取得参数,然后作处理,最后将结果打印在out里。
我们的第三个例子仍然以这个唯一性校验为例子,这次结合Struts开发框架来完成AJAX的开发。
四、基于Struts的AJAX
首先,我们仍然是对Struts应用来做配置,仍然是在struts-config,xml文件里做配置,如下:
跟普通的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:
我们可以看到,JSP基本是一样的,除了要发送的url:ajax/check.do?username='+username。
最后,我们来看一个基于Struts和AJAX的复杂一些的例子,如果不用AJAX技术,UI的代码将十分复杂。
五、一个复杂的实例
这是一个比较复杂的级联:一共八个列表框,三个下拉框。从第一个列表框里选择到第二个列表框里后,第一个选择框里的选项是第二个列表框的选择;然后,在第一个选择框里选择以后,与选择值关联的一些选项出现在第三个列表框里。从第三个列表框里选择选项到第四个列表框里,同样,第二个选择框的选项也是第四个列表框的选项;如果对第二个选择框进行选择后,与选择值关联的一些选项出现在第六个列表框里,依次类推……
这个UI的逻辑就比较复杂,但使用了AJAX使得我们实现起来就简单多了,这个例子我们除了使用Action类,还要用到POJO类和Business类,然后我们扩展的话,可以通过Business类和数据库连接起来。
我们还是先看配置文件:
然后看看Action类:
/*
/**
* Title : Base Dict Class
* Description : here Description is the function of class, here maybe multirows
* Copyright: Copyright (c) 2004
* @company Freeborders Co., Ltd.
* @Goal Feng
* @Version 1.0
*/
package com.ajax;
import java.io.PrintWriter;
import java.util.List;
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;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SelectAction extends Action
{
public final ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
String type = request.getParameter("type");
String id = request.getParameter("id");
System.out.println(id);
StringBuffer sb=new StringBuffer("");
PrintWriter out=response.getWriter();
out.write(sb.toString());
out.close();
System.out.println(sb.toString());
return mapping.findForward("success");
}
public static void main(String[] args)
{
}
}
POJO类和Business类:
package com.ajax;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SelectForm
{
private String text;
private String value;
/**
* @return Returns the text.
*/
public String getText()
{
return text;
}
/**
* @param text The text to set.
*/
public void setText(String text)
{
this.text = text;
}
/**
* @return Returns the value.
*/
public String getValue()
{
return value;
}
/**
* @param value The value to set.
*/
public void setValue(String value)
{
this.value = value;
}
public static void main(String[] args)
{
}
}
package com.ajax;
import java.util.ArrayList;
import java.util.List;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SelectBusiness
{
public List getData(String id)
{
ArrayList list = new ArrayList();
for(int i=1;i<6;i++)
{
SelectForm form = new SelectForm();
form.setText(id+i);
form.setValue(id+i);
list.add(form);
}
return list;
}
public static void main(String[] args)
{
}
}