注意:要事先将相应的jar包和读依赖的样式表、图片和js文件放到相应的目录下
1.User.java
package autocomplete;
import net.sourceforge.ajaxtags.xml.AjaxXmlBuilder;
public class User implements AjaxXmlBuilder.PropertyReader{
private String userName;//用户姓名
private String department;//所属部门名称
//定义构造函数
public User(String department,String userName){
this.department = department;
this.userName = userName;
}
public String getName() {
return userName;
}
public String getValue() {
return department;
}
public boolean isCData() {
return false;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
2.Department.java
package autocomplete;
import java.util.*;
@SuppressWarnings("unchecked")
public class Department {
//定义一个静态的部门用户列表
static final List users = new ArrayList();
//为静态的部门用户列表添加用户
static {
users.add(new User("研发中心", "张三"));
users.add(new User("研发中心", "张四"));
users.add(new User("研发中心", "李三"));
users.add(new User("管理中心", "李四"));
users.add(new User("管理中心", "王五"));
users.add(new User("管理中心", "王六"));
}
//根据传入的用户姓名找出其所属部门
public List getDepartmentByUser(String userName){
List userList = new ArrayList();
String tempUserName = null;
for (Iterator it = users.iterator(); it.hasNext();) {
User user = (User) it.next();
tempUserName = user.getUserName();
//列出以传入姓名开头的所有用户
if (tempUserName.startsWith(userName)) {
userList.add(user);
}
}
//返回结果集
return userList;
}
//返回所有用户数据
public List getAllUser(){
return users;
}
}
3.AutoCompleteServlet.java
package autocomplete;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sourceforge.ajaxtags.servlets.BaseAjaxServlet;
import net.sourceforge.ajaxtags.xml.AjaxXmlBuilder;
@SuppressWarnings("unchecked")
public class AutocompleteServlet extends BaseAjaxServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public String getXmlContent(HttpServletRequest request, HttpServletResponse response) {
//获得客户端ajax:autocomplete标签提交的user参数
String user = request.getParameter("user");
//新建一个Department对象
Department department = new Department();
//列出以user参数指定内容开头的所有用户
List list = department.getDepartmentByUser(user);
//通过使用ajaxtags提供的AjaxXmlBuilder对象产生一个客户端ajax:autocomplete标签能解析的响应结果
String result =new AjaxXmlBuilder().addItems(list).toString();
//返回响应结果
return result;
}
}
4.Process.java
package autocomplete;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Process extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获得客户端表单中的user参数
String user = request.getParameter("user");
user = new String(user.getBytes("ISO8859-1"),"gb2312");
//获得客户端表单中的department参数
String department = request.getParameter("department");
department = new String(department.getBytes("ISO8859-1"),"gb2312");
//向客户端返回响应结果
response.setContentType("text/html");
response.setCharacterEncoding("gb2312");
PrintWriter out = response.getWriter();
out.println(department+"的"+user+"用户数据处理完成!");
out.flush();
out.close();
}
}
5.autoComplete.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="http://ajaxtags.sourceforge.net/tags/ajaxtags" prefix="ajax"%>
<html>
<head>
<title>Autocomplete标签示例</title>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/scriptaculous/scriptaculous.js"></script>
<script type="text/javascript" src="js/overlibmws/overlibmws.js"></script>
<script type="text/javascript" src="js/ajaxtags.js"></script>
<link rel="stylesheet" type="text/css" href="css/ajaxtags.css" />
</head>
<body>
<h2>Autocomplete标签示例</h2><br>
<hr>
<form action="Process" class="basicForm" method="post">
请输入用户姓名:
<input id="user" name="user" type="text">
<span id="waitIcon" style="display:none;">
<img src="images/indicator.gif">
</span>
<br>
该用户所属部门:
<input id="department" name="department" type="text"><br>
<input type="submit" name="submit" value="提交">
</form>
<ajax:autocomplete
source="user"
target="department"
baseUrl="AutocompleteServlet"
className="autocomplete"
indicator="waitIcon"
minimumCharacters="1"/>
</body>
</html>
6.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>ResponseOverrideFilter</filter-name>
<filter-class>org.displaytag.filter.ResponseOverrideFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ResponseOverrideFilter</filter-name>
<url-pattern>/displaytag.jsp</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>AutocompleteServlet</servlet-name>
<servlet-class>autocomplete.AutocompleteServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Process</servlet-name>
<servlet-class>autocomplete.Process</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AutocompleteServlet</servlet-name>
<url-pattern>/AutocompleteServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Process</servlet-name>
<url-pattern>/Process</url-pattern>
</servlet-mapping>
</web-app>