struts2 autocompleter标签 动态下拉菜单

,autocompleter标签具有自动完成功能的下拉框,能根据所填写的内容筛选下拉框的内容。使用autocompleter标签必须使用Ajax主题,因为它用到了DOJO的JavaScript库

autocompleteAjaxTag.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="struts"%>




	
	
	
	

autocompleteAjaxData1.jsp

获取前缀,找到前缀匹配项,封装成JSONArray,然后回传。

<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import=" net.sf.json.JSONArray"%>
<%@page import=" java.io.IOException"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%
	out.clear();//清空输出数据
	request.setCharacterEncoding("UTF-8");//设置request编码
	response.setHeader("Pragma", "no-cache");//禁止缓存
	response.setHeader("cache-control", "no-cache");
	response.setDateHeader("Expires", 0);

	List countries = new ArrayList();
	countries.add("Afghanistan");
	countries.add("Aland Islands");
	countries.add("Albania");
	countries.add("Algernia");
	countries.add("American Samno(USA)");
	countries.add("Andorra");

	String countryPrefix = request.getParameter("country");//获取AutoCompleter已经填写的内容
	if (null == countryPrefix) {
		countryPrefix = "";
	}
	List buffer = new ArrayList();

	for (int i = 0; i < countries.size(); i++) {
		if (countries.get(i).toLowerCase()
				.startsWith(countryPrefix.toLowerCase())) {
			buffer.add(countries.get(i));
		}
	}
	JSONArray jsonArray = JSONArray.fromObject(buffer);
	Thread.sleep(500);//当前线程睡眠0.5秒,演示indicator
	try {
		response.setContentType("aplication/json;charset=UTF-8");
		out.print(jsonArray);
	} catch (IOException e) {
		e.printStackTrace();
	}
%>

struts2 autocompleter标签 动态下拉菜单_第1张图片

你可能感兴趣的:(JavaEE)