码迷,mamicode.com
首页 > 其他好文 > 详细

struts2 autocompleter 动态下拉菜单

时间:2014-08-14 16:52:48      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:struts2   autocompleter   dojo   

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

autocompleteAjaxTag.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="struts"%>
<struts:head theme="ajax" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
	<struts:url id="dataUrl" value="/autocompleteAjaxData1.jsp" />
	<!-- 数据URL -->
	<struts:autocompleter name="country" theme="ajax" href="%{dataUrl}"
		loadOnTextChange="true" loadMinimumCount="0" autoComplete="false"
		showDownArrow="true" indicator="indicator" />
	<img id="indicator" src="images/loading.gif">
</body>
</html>

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<String> countries = new ArrayList<String>();
	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<String> buffer = new ArrayList<String>();

	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();
	}
%>

bubuko.com,布布扣

struts2 autocompleter 动态下拉菜单,布布扣,bubuko.com

struts2 autocompleter 动态下拉菜单

标签:struts2   autocompleter   dojo   

原文地址:http://blog.csdn.net/arcticfoxhan/article/details/38558129

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!